2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.common.util;
22 import org.apache.commons.codec.binary.Base64;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.openecomp.sdc.be.config.Configuration;
26 import org.openecomp.sdc.common.http.client.api.HttpClient;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.nio.file.Files;
31 import java.nio.file.Paths;
32 import org.openecomp.sdc.exception.YamlConversionException;
34 import static junit.framework.TestCase.assertEquals;
35 import static junit.framework.TestCase.assertFalse;
36 import static junit.framework.TestCase.assertNull;
37 import static junit.framework.TestCase.assertTrue;
39 public class YamlToObjectConverterTest {
41 private final String filePath = "./src/test/resources/config/common/";
42 private final String fileName = "configuration.yaml";
44 private final String testYaml = "--- \n" +
48 private YamlToObjectConverter yamlToObjectConverter;
52 yamlToObjectConverter = new YamlToObjectConverter();
56 public void validateIsValidYamlReturnsTrueIfGivenYamlIsValid() {
57 boolean result = yamlToObjectConverter.isValidYaml(testYaml.getBytes());
63 public void validateIsValidYamlReturnsFalseIfGivenYamlIsNotValid() {
64 final String testNotYaml = "testString;";
65 boolean result = yamlToObjectConverter.isValidYaml(testNotYaml.getBytes());
71 public void validateIsValidYamlEncoded64ReturnsTrueIfGivenYamlIsEncoded64() {
72 boolean result = yamlToObjectConverter.isValidYamlEncoded64(Base64.encodeBase64(testYaml.getBytes()));
78 public void validateIsValidYamlEncoded64ReturnsFalseIfGivenYamlIsNotEncoded64() {
79 boolean result = yamlToObjectConverter.isValidYamlEncoded64(testYaml.getBytes());
85 public void validateConvertWithFullFilePathReturnsValidObjectCreatedFromYaml() throws YamlConversionException {
86 Configuration result = yamlToObjectConverter.convert(filePath+fileName, Configuration.class);
88 assertThatCreatedObjectIsValid(result);
92 public void validateConvertWithFullFilePathReturnsNullIfFileDoesNotExist() throws YamlConversionException {
93 final String wrongFileName = "wrong-configuration.yaml";
95 Configuration result = yamlToObjectConverter.convert(wrongFileName, Configuration.class);
100 @Test(expected = YamlConversionException.class)
101 public void validateConvertWithFullFilePathThrowsExceptionIfClassDoesNotMathYaml() throws YamlConversionException {
102 yamlToObjectConverter.convert(filePath + fileName, HttpClient.class);
106 public void validateConvertWithFilePathAndFileNameReturnsValidObjectCreatedFromYaml()
107 throws YamlConversionException {
109 Configuration result = yamlToObjectConverter.convert(filePath, Configuration.class, fileName);
111 assertThatCreatedObjectIsValid(result);
114 @Test(expected = IllegalArgumentException.class)
115 public void validateConvertWithFilePathAndFileNameThrowsExceptionIfClassIsNull() throws YamlConversionException {
116 yamlToObjectConverter.convert(filePath, null, fileName);
120 public void validateConvertFromByteArrayReturnsValidObjectCreatedFromYaml() throws IOException {
122 final byte[] yamlAsByteArray = getYamlAsBytesFromFile();
124 Configuration result = yamlToObjectConverter.convert(yamlAsByteArray, Configuration.class);
126 assertThatCreatedObjectIsValid(result);
130 public void validateConvertFromByteArrayReturnsNullIfByteArrayIsInCorrect() {
132 final byte[] yamlAsByteArray = "notYaml".getBytes();
134 Configuration result = yamlToObjectConverter.convert(yamlAsByteArray, Configuration.class);
140 public void validateConvertFromByteArrayReturnsNullIfClassIsInCorrect() throws IOException {
142 final byte[] yamlAsByteArray = getYamlAsBytesFromFile();
144 HttpClient result = yamlToObjectConverter.convert(yamlAsByteArray, HttpClient.class);
150 public void validateConvertFromByteArrayReturnsNullIfArrayIsNull() {
152 Configuration result = yamlToObjectConverter.convert((byte[])null, Configuration.class);
157 private byte[] getYamlAsBytesFromFile() throws IOException {
158 final InputStream inputYamlFile = Files.newInputStream(Paths.get(filePath+fileName));
159 final byte[] yamlAsByteArray = new byte[inputYamlFile.available()];
160 inputYamlFile.read(yamlAsByteArray);
162 return yamlAsByteArray;
165 private void assertThatCreatedObjectIsValid(Configuration result) {
166 assertEquals(result.getBeHttpPort(),new Integer(8080));
167 assertEquals(result.getBeSslPort(),new Integer(8443));
168 assertEquals(result.getVersion(),"1.1.0");
169 assertEquals(result.getUsers().size(),1);
170 assertEquals(result.getUsers().get("tom"),"passwd");