GeorgeJahad commented on code in PR #4695: URL: https://github.com/apache/ozone/pull/4695#discussion_r1190464875
########## hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/ratis_snapshot/TestOmRatisSnapshotProvider.java: ########## @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.ozone.om.ratis_snapshot; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.hadoop.hdds.conf.MutableConfigurationSource; +import org.apache.hadoop.hdds.utils.LegacyHadoopConfigurationSource; +import org.apache.hadoop.hdfs.web.URLConnectionFactory; +import org.apache.hadoop.ozone.OzoneConsts; +import org.apache.hadoop.ozone.om.helpers.OMNodeDetails; +import org.apache.hadoop.security.authentication.client.AuthenticationException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import static java.net.HttpURLConnection.HTTP_OK; +import static org.apache.hadoop.ozone.OzoneConsts.MULTIPART_FORM_DATA_BOUNDARY; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Unit tests for {@link OmRatisSnapshotProvider}. + */ +public class TestOmRatisSnapshotProvider { + + private OmRatisSnapshotProvider omRatisSnapshotProvider; + private OMNodeDetails leader; + private String leaderNodeId; + private static final String CR_NL = "\r\n"; + public static final String CONTENT_DISPOSITION = + "Content-Disposition: form-data; name=\"" + + OzoneConsts.OZONE_DB_CHECKPOINT_REQUEST_TO_EXCLUDE_SST + "[]\"" + + CR_NL + CR_NL; + private StringBuilder sb; + private File targetFile; + + @BeforeEach + public void setup(@TempDir File snapshotDir, + @TempDir File downloadDir) throws IOException { + targetFile = new File(downloadDir, "newfile"); + + MutableConfigurationSource conf = + mock(LegacyHadoopConfigurationSource.class); + when(conf.get(eq("ozone.http.policy"), any())).thenReturn("HTTP_ONLY"); + when(conf.get("ozone.om.http.auth.type", "simple")) + .thenReturn("no-kerberos"); + + Map<String, OMNodeDetails> peerNodesMap = new HashMap<>(); + leaderNodeId = "1"; + leader = mock(OMNodeDetails.class); + peerNodesMap.put(leaderNodeId, leader); + + omRatisSnapshotProvider = + new OmRatisSnapshotProvider(conf, snapshotDir, peerNodesMap); + + sb = new StringBuilder(); + sb.append("--" + MULTIPART_FORM_DATA_BOUNDARY + CR_NL); + sb.append(CONTENT_DISPOSITION); + } + + @Test + public void testDownloadSnapshot() throws IOException, + AuthenticationException { + URL omCheckpointUrl = mock(URL.class); + when(leader.getOMDBCheckpointEndpointUrl(anyBoolean(), anyBoolean())) + .thenReturn(omCheckpointUrl); + + URLConnectionFactory connectionFactory = + mock(URLConnectionFactory.class); + omRatisSnapshotProvider.setConnectionFactory(connectionFactory); + + HttpURLConnection connection = mock(HttpURLConnection.class); + when(connectionFactory.openConnection(any(URL.class), anyBoolean())) + .thenReturn(connection); + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + when(connection.getOutputStream()).thenReturn(outputStream); + when(connection.getResponseCode()).thenReturn(HTTP_OK); + InputStream inputStream = + new ByteArrayInputStream(outputStream.toByteArray()); + when(connection.getInputStream()).thenReturn(inputStream); + + omRatisSnapshotProvider.downloadSnapshot(leaderNodeId, targetFile); + + sb.append("--" + MULTIPART_FORM_DATA_BOUNDARY + "--" + CR_NL); + Assertions.assertEquals(sb.toString(), + new String(outputStream.toByteArray(), StandardCharsets.UTF_8)); + } + + @Test + public void testWriteFormDataWithSstFile() throws IOException { + HttpURLConnection connection = mock(HttpURLConnection.class); + List<String> sstFiles = new ArrayList<>(); + String fileName = "file1.sst"; + sstFiles.add(fileName); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + when(connection.getOutputStream()).thenReturn(outputStream); + + + OmRatisSnapshotProvider.writeFormData(connection, sstFiles); + + sb.append(fileName).append(CR_NL); Review Comment: please use 2 files -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
