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;
33 import static junit.framework.TestCase.assertEquals;
34 import static junit.framework.TestCase.assertFalse;
35 import static junit.framework.TestCase.assertNull;
36 import static junit.framework.TestCase.assertTrue;
38 public class YamlToObjectConverterTest {
40 private final String filePath = "./src/test/resources/config/common/";
41 private final String fileName = "configuration.yaml";
43 private final String testYaml = "--- \n" +
47 private YamlToObjectConverter yamlToObjectConverter;
51 yamlToObjectConverter = new YamlToObjectConverter();
55 public void validateIsValidYamlReturnsTrueIfGivenYamlIsValid() {
56 boolean result = yamlToObjectConverter.isValidYaml(testYaml.getBytes());
62 public void validateIsValidYamlReturnsFalseIfGivenYamlIsNotValid() {
63 final String testNotYaml = "testString;";
64 boolean result = yamlToObjectConverter.isValidYaml(testNotYaml.getBytes());
70 public void validateIsValidYamlEncoded64ReturnsTrueIfGivenYamlIsEncoded64() {
71 boolean result = yamlToObjectConverter.isValidYamlEncoded64(Base64.encodeBase64(testYaml.getBytes()));
77 public void validateIsValidYamlEncoded64ReturnsFalseIfGivenYamlIsNotEncoded64() {
78 boolean result = yamlToObjectConverter.isValidYamlEncoded64(testYaml.getBytes());
84 public void validateConvertWithFullFilePathReturnsValidObjectCreatedFromYaml() {
85 Configuration result = yamlToObjectConverter.convert(filePath+fileName, Configuration.class);
87 assertThatCreatedObjectIsValid(result);
91 public void validateConvertWithFullFilePathReturnsNullIfFileDoesNotExist() {
92 final String wrongFileName = "wrong-configuration.yaml";
94 Configuration result = yamlToObjectConverter.convert(wrongFileName, Configuration.class);
100 public void validateConvertWithFullFilePathReturnsNullIfClassDoesNotMathYaml() {
102 HttpClient result = yamlToObjectConverter.convert(filePath+fileName, HttpClient.class);
108 public void validateConvertWithFilePathAndFileNameReturnsValidObjectCreatedFromYaml() {
110 Configuration result = yamlToObjectConverter.convert(filePath, Configuration.class, fileName);
112 assertThatCreatedObjectIsValid(result);
116 public void validateConvertWithFilePathAndFileNameReturnsNullIfClassIsNull() {
118 HttpClient result = yamlToObjectConverter.convert(filePath, null, fileName);
124 public void validateConvertFromByteArrayReturnsValidObjectCreatedFromYaml() throws IOException {
126 final byte[] yamlAsByteArray = getYamlAsBytesFromFile();
128 Configuration result = yamlToObjectConverter.convert(yamlAsByteArray, Configuration.class);
130 assertThatCreatedObjectIsValid(result);
134 public void validateConvertFromByteArrayReturnsNullIfByteArrayIsInCorrect() {
136 final byte[] yamlAsByteArray = "notYaml".getBytes();
138 Configuration result = yamlToObjectConverter.convert(yamlAsByteArray, Configuration.class);
144 public void validateConvertFromByteArrayReturnsNullIfClassIsInCorrect() throws IOException {
146 final byte[] yamlAsByteArray = getYamlAsBytesFromFile();
148 HttpClient result = yamlToObjectConverter.convert(yamlAsByteArray, HttpClient.class);
154 public void validateConvertFromByteArrayReturnsNullIfArrayIsNull() {
156 Configuration result = yamlToObjectConverter.convert((byte[])null, Configuration.class);
161 private byte[] getYamlAsBytesFromFile() throws IOException {
162 final InputStream inputYamlFile = Files.newInputStream(Paths.get(filePath+fileName));
163 final byte[] yamlAsByteArray = new byte[inputYamlFile.available()];
164 inputYamlFile.read(yamlAsByteArray);
166 return yamlAsByteArray;
169 private void assertThatCreatedObjectIsValid(Configuration result) {
170 assertEquals(result.getBeHttpPort(),new Integer(8080));
171 assertEquals(result.getBeSslPort(),new Integer(8443));
172 assertEquals(result.getVersion(),"1.1.0");
173 assertEquals(result.getUsers().size(),1);
174 assertEquals(result.getUsers().get("tom"),"passwd");