49fc5533020be1b048d1d9f0bd72cad0e6348a1f
[sdc.git] / common-app-api / src / test / java / org / openecomp / sdc / common / util / YamlToObjectConverterTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.sdc.common.util;
21
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;
27
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.nio.file.Files;
31 import java.nio.file.Paths;
32
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;
37
38 public class YamlToObjectConverterTest {
39
40     private final String filePath = "./src/test/resources/config/common/";
41     private final String fileName = "configuration.yaml";
42
43     private final String testYaml = "--- \n" +
44             "object: \n" +
45             "  key: value";
46
47     private YamlToObjectConverter yamlToObjectConverter;
48
49     @Before
50     public void setUp() {
51         yamlToObjectConverter = new YamlToObjectConverter();
52     }
53
54     @Test
55     public void validateIsValidYamlReturnsTrueIfGivenYamlIsValid() {
56         boolean result = yamlToObjectConverter.isValidYaml(testYaml.getBytes());
57
58         assertTrue(result);
59     }
60
61     @Test
62     public void validateIsValidYamlReturnsFalseIfGivenYamlIsNotValid() {
63         final String testNotYaml = "testString;";
64         boolean result = yamlToObjectConverter.isValidYaml(testNotYaml.getBytes());
65
66         assertFalse(result);
67     }
68
69     @Test
70     public void validateIsValidYamlEncoded64ReturnsTrueIfGivenYamlIsEncoded64() {
71         boolean result = yamlToObjectConverter.isValidYamlEncoded64(Base64.encodeBase64(testYaml.getBytes()));
72
73         assertTrue(result);
74     }
75
76     @Test
77     public void validateIsValidYamlEncoded64ReturnsFalseIfGivenYamlIsNotEncoded64() {
78         boolean result = yamlToObjectConverter.isValidYamlEncoded64(testYaml.getBytes());
79
80         assertFalse(result);
81     }
82
83     @Test
84     public void validateConvertWithFullFilePathReturnsValidObjectCreatedFromYaml() {
85         Configuration result = yamlToObjectConverter.convert(filePath+fileName, Configuration.class);
86
87         assertThatCreatedObjectIsValid(result);
88     }
89
90     @Test
91     public void validateConvertWithFullFilePathReturnsNullIfFileDoesNotExist() {
92         final String wrongFileName = "wrong-configuration.yaml";
93
94         Configuration result = yamlToObjectConverter.convert(wrongFileName, Configuration.class);
95
96         assertNull(result);
97     }
98
99     @Test
100     public void validateConvertWithFullFilePathReturnsNullIfClassDoesNotMathYaml() {
101
102         HttpClient result = yamlToObjectConverter.convert(filePath+fileName, HttpClient.class);
103
104         assertNull(result);
105     }
106
107     @Test
108     public void validateConvertWithFilePathAndFileNameReturnsValidObjectCreatedFromYaml() {
109
110         Configuration result = yamlToObjectConverter.convert(filePath, Configuration.class, fileName);
111
112         assertThatCreatedObjectIsValid(result);
113     }
114
115     @Test
116     public void validateConvertWithFilePathAndFileNameReturnsNullIfClassIsNull() {
117
118         HttpClient result = yamlToObjectConverter.convert(filePath, null, fileName);
119
120         assertNull(result);
121     }
122
123     @Test
124     public void validateConvertFromByteArrayReturnsValidObjectCreatedFromYaml() throws IOException {
125
126         final byte[] yamlAsByteArray = getYamlAsBytesFromFile();
127
128         Configuration result = yamlToObjectConverter.convert(yamlAsByteArray, Configuration.class);
129
130         assertThatCreatedObjectIsValid(result);
131     }
132
133     @Test
134     public void validateConvertFromByteArrayReturnsNullIfByteArrayIsInCorrect() {
135
136         final byte[] yamlAsByteArray = "notYaml".getBytes();
137
138         Configuration result = yamlToObjectConverter.convert(yamlAsByteArray, Configuration.class);
139
140         assertNull(result);
141     }
142
143     @Test
144     public void validateConvertFromByteArrayReturnsNullIfClassIsInCorrect() throws IOException {
145
146         final byte[] yamlAsByteArray = getYamlAsBytesFromFile();
147
148         HttpClient result = yamlToObjectConverter.convert(yamlAsByteArray, HttpClient.class);
149
150         assertNull(result);
151     }
152
153     @Test
154     public void validateConvertFromByteArrayReturnsNullIfArrayIsNull() {
155
156         Configuration result = yamlToObjectConverter.convert((byte[])null, Configuration.class);
157
158         assertNull(result);
159     }
160
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);
165
166         return yamlAsByteArray;
167     }
168
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");
175     }
176 }