testing common-app-api utils YamlToObjectConverter 85/94685/3
authorBartosz Gardziejewski <bartosz.gardziejewski@nokia.com>
Fri, 30 Aug 2019 13:50:41 +0000 (15:50 +0200)
committerTomasz Golabek <tomasz.golabek@nokia.com>
Mon, 2 Sep 2019 07:29:13 +0000 (07:29 +0000)
Issue-ID: SDC-2326
Signed-off-by: Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com>
Change-Id: I10fc2d7a35150e97b90a1c69c4faac949e439db3

common-app-api/src/test/java/org/openecomp/sdc/common/util/YamlToObjectConverterTest.java [new file with mode: 0644]

diff --git a/common-app-api/src/test/java/org/openecomp/sdc/common/util/YamlToObjectConverterTest.java b/common-app-api/src/test/java/org/openecomp/sdc/common/util/YamlToObjectConverterTest.java
new file mode 100644 (file)
index 0000000..49fc553
--- /dev/null
@@ -0,0 +1,176 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2019 Nokia. All rights reserved.
+ * ================================================================================
+ * Licensed 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.
+ * ============LICENSE_END=========================================================
+ */
+package org.openecomp.sdc.common.util;
+
+import org.apache.commons.codec.binary.Base64;
+import org.junit.Before;
+import org.junit.Test;
+import org.openecomp.sdc.be.config.Configuration;
+import org.openecomp.sdc.common.http.client.api.HttpClient;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+
+import static junit.framework.TestCase.assertEquals;
+import static junit.framework.TestCase.assertFalse;
+import static junit.framework.TestCase.assertNull;
+import static junit.framework.TestCase.assertTrue;
+
+public class YamlToObjectConverterTest {
+
+    private final String filePath = "./src/test/resources/config/common/";
+    private final String fileName = "configuration.yaml";
+
+    private final String testYaml = "--- \n" +
+            "object: \n" +
+            "  key: value";
+
+    private YamlToObjectConverter yamlToObjectConverter;
+
+    @Before
+    public void setUp() {
+        yamlToObjectConverter = new YamlToObjectConverter();
+    }
+
+    @Test
+    public void validateIsValidYamlReturnsTrueIfGivenYamlIsValid() {
+        boolean result = yamlToObjectConverter.isValidYaml(testYaml.getBytes());
+
+        assertTrue(result);
+    }
+
+    @Test
+    public void validateIsValidYamlReturnsFalseIfGivenYamlIsNotValid() {
+        final String testNotYaml = "testString;";
+        boolean result = yamlToObjectConverter.isValidYaml(testNotYaml.getBytes());
+
+        assertFalse(result);
+    }
+
+    @Test
+    public void validateIsValidYamlEncoded64ReturnsTrueIfGivenYamlIsEncoded64() {
+        boolean result = yamlToObjectConverter.isValidYamlEncoded64(Base64.encodeBase64(testYaml.getBytes()));
+
+        assertTrue(result);
+    }
+
+    @Test
+    public void validateIsValidYamlEncoded64ReturnsFalseIfGivenYamlIsNotEncoded64() {
+        boolean result = yamlToObjectConverter.isValidYamlEncoded64(testYaml.getBytes());
+
+        assertFalse(result);
+    }
+
+    @Test
+    public void validateConvertWithFullFilePathReturnsValidObjectCreatedFromYaml() {
+        Configuration result = yamlToObjectConverter.convert(filePath+fileName, Configuration.class);
+
+        assertThatCreatedObjectIsValid(result);
+    }
+
+    @Test
+    public void validateConvertWithFullFilePathReturnsNullIfFileDoesNotExist() {
+        final String wrongFileName = "wrong-configuration.yaml";
+
+        Configuration result = yamlToObjectConverter.convert(wrongFileName, Configuration.class);
+
+        assertNull(result);
+    }
+
+    @Test
+    public void validateConvertWithFullFilePathReturnsNullIfClassDoesNotMathYaml() {
+
+        HttpClient result = yamlToObjectConverter.convert(filePath+fileName, HttpClient.class);
+
+        assertNull(result);
+    }
+
+    @Test
+    public void validateConvertWithFilePathAndFileNameReturnsValidObjectCreatedFromYaml() {
+
+        Configuration result = yamlToObjectConverter.convert(filePath, Configuration.class, fileName);
+
+        assertThatCreatedObjectIsValid(result);
+    }
+
+    @Test
+    public void validateConvertWithFilePathAndFileNameReturnsNullIfClassIsNull() {
+
+        HttpClient result = yamlToObjectConverter.convert(filePath, null, fileName);
+
+        assertNull(result);
+    }
+
+    @Test
+    public void validateConvertFromByteArrayReturnsValidObjectCreatedFromYaml() throws IOException {
+
+        final byte[] yamlAsByteArray = getYamlAsBytesFromFile();
+
+        Configuration result = yamlToObjectConverter.convert(yamlAsByteArray, Configuration.class);
+
+        assertThatCreatedObjectIsValid(result);
+    }
+
+    @Test
+    public void validateConvertFromByteArrayReturnsNullIfByteArrayIsInCorrect() {
+
+        final byte[] yamlAsByteArray = "notYaml".getBytes();
+
+        Configuration result = yamlToObjectConverter.convert(yamlAsByteArray, Configuration.class);
+
+        assertNull(result);
+    }
+
+    @Test
+    public void validateConvertFromByteArrayReturnsNullIfClassIsInCorrect() throws IOException {
+
+        final byte[] yamlAsByteArray = getYamlAsBytesFromFile();
+
+        HttpClient result = yamlToObjectConverter.convert(yamlAsByteArray, HttpClient.class);
+
+        assertNull(result);
+    }
+
+    @Test
+    public void validateConvertFromByteArrayReturnsNullIfArrayIsNull() {
+
+        Configuration result = yamlToObjectConverter.convert((byte[])null, Configuration.class);
+
+        assertNull(result);
+    }
+
+    private byte[] getYamlAsBytesFromFile() throws IOException {
+        final InputStream inputYamlFile = Files.newInputStream(Paths.get(filePath+fileName));
+        final byte[] yamlAsByteArray = new byte[inputYamlFile.available()];
+        inputYamlFile.read(yamlAsByteArray);
+
+        return yamlAsByteArray;
+    }
+
+    private void assertThatCreatedObjectIsValid(Configuration result) {
+        assertEquals(result.getBeHttpPort(),new Integer(8080));
+        assertEquals(result.getBeSslPort(),new Integer(8443));
+        assertEquals(result.getVersion(),"1.1.0");
+        assertEquals(result.getUsers().size(),1);
+        assertEquals(result.getUsers().get("tom"),"passwd");
+    }
+}