anuragp010 commented on code in PR #12213:
URL: https://github.com/apache/ozone/pull/12213#discussion_r4765737066


##########
hadoop-ozone/cli-shell/src/test/java/org/apache/hadoop/ozone/shell/keys/TestPutKeyHandler.java:
##########
@@ -0,0 +1,168 @@
+/*
+ * 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.shell.keys;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import com.google.common.collect.ImmutableMap;
+import java.io.ByteArrayOutputStream;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import org.apache.hadoop.hdds.client.OzoneStoragePolicy;
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import org.apache.hadoop.ozone.client.ObjectStore;
+import org.apache.hadoop.ozone.client.OzoneBucket;
+import org.apache.hadoop.ozone.client.OzoneClient;
+import org.apache.hadoop.ozone.client.OzoneVolume;
+import org.apache.hadoop.ozone.client.io.OzoneOutputStream;
+import org.apache.hadoop.ozone.client.protocol.CreateKeyOptions;
+import org.apache.hadoop.ozone.shell.OzoneAddress;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.junit.jupiter.params.provider.ValueSource;
+import org.mockito.ArgumentCaptor;
+import picocli.CommandLine;
+
+/**
+ * Unit tests for the {@code --storage-policy} option of PutKeyHandler.
+ */
+public class TestPutKeyHandler {
+
+  private static final byte[] CONTENT = "put key 
content".getBytes(StandardCharsets.UTF_8);
+
+  private PutKeyHandler cmd;
+  private OzoneBucket bucket;
+  private OzoneAddress address;
+  private Path dataFile;
+  private ByteArrayOutputStream written;
+
+  @BeforeEach
+  public void setup(@TempDir Path tempDir) throws Exception {
+    // Override getConf() so execute() can be called directly without going
+    // through Handler.call(), which normally sets the OzoneConfiguration.
+    cmd = new PutKeyHandler() {
+      @Override
+      public OzoneConfiguration getConf() {
+        return new OzoneConfiguration();
+      }
+    };
+    address = new OzoneAddress("o3://ozone1/vol/bucket/key");
+    dataFile = tempDir.resolve("data.txt");
+    Files.write(dataFile, CONTENT);
+
+    written = new ByteArrayOutputStream();
+    bucket = mock(OzoneBucket.class);
+    when(bucket.getMetadata()).thenReturn(ImmutableMap.of());
+    when(bucket.createKey(anyString(), any(CreateKeyOptions.class)))
+        .thenReturn(new OzoneOutputStream(written, null));
+  }
+
+  @ParameterizedTest
+  @CsvSource({"HOT,HOT", "WARM,WARM", "COLD,COLD", "hot,HOT", "cold,COLD"})
+  public void testStoragePolicyIsPassedToCreateKey(String argument, 
OzoneStoragePolicy expected) throws Exception {
+    parseArgs("--storage-policy", argument);
+
+    cmd.execute(buildClient(), address);
+
+    CreateKeyOptions options = captureCreateKeyOptions();
+    assertThat(options.getStoragePolicy()).isEqualTo(expected);
+    assertThat(options.getSize()).isEqualTo(CONTENT.length);
+    assertThat(written.toByteArray()).isEqualTo(CONTENT);
+  }
+
+  @Test
+  public void testShortOptionName() throws Exception {
+    parseArgs("-s", "COLD");
+
+    cmd.execute(buildClient(), address);
+
+    
assertThat(captureCreateKeyOptions().getStoragePolicy()).isEqualTo(OzoneStoragePolicy.COLD);
+  }
+
+  @Test
+  public void testNoStoragePolicyOptionLeavesPolicyUnset() throws Exception {
+    parseArgs();
+
+    cmd.execute(buildClient(), address);
+
+    CreateKeyOptions options = captureCreateKeyOptions();
+    assertThat(options.getStoragePolicy()).isNull();
+    assertThat(options.getSize()).isEqualTo(CONTENT.length);
+    assertThat(written.toByteArray()).isEqualTo(CONTENT);
+  }
+
+  @ParameterizedTest
+  @ValueSource(strings = {"null", "NULL", ""})
+  public void testNullStoragePolicyLeavesPolicyUnset(String argument) throws 
Exception {
+    parseArgs("--storage-policy", argument);
+
+    cmd.execute(buildClient(), address);
+
+    assertThat(captureCreateKeyOptions().getStoragePolicy()).isNull();
+  }
+
+  @Test
+  public void testInvalidStoragePolicyIsRejected() throws Exception {

Review Comment:
   Done. Have added unit tests for the same. 
   Note: This tests would be removed when their respective feature tickets are 
merged.



-- 
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]

Reply via email to