diff options
author | Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com> | 2019-08-30 15:50:41 +0200 |
---|---|---|
committer | Tomasz Golabek <tomasz.golabek@nokia.com> | 2019-09-02 07:29:13 +0000 |
commit | b5682213ccd48671a3c064688d759944421f6b34 (patch) | |
tree | 05e947cae043e6b79cfd2d5319646d40888dd2ca /common-app-api/src/test/java | |
parent | f533d987e5858274c13d35cf28b660a44dcd30c4 (diff) |
testing common-app-api utils YamlToObjectConverter
Issue-ID: SDC-2326
Signed-off-by: Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com>
Change-Id: I10fc2d7a35150e97b90a1c69c4faac949e439db3
Diffstat (limited to 'common-app-api/src/test/java')
-rw-r--r-- | common-app-api/src/test/java/org/openecomp/sdc/common/util/YamlToObjectConverterTest.java | 176 |
1 files changed, 176 insertions, 0 deletions
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 index 0000000000..49fc553302 --- /dev/null +++ b/common-app-api/src/test/java/org/openecomp/sdc/common/util/YamlToObjectConverterTest.java @@ -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"); + } +} |