Configuration file runtime reload
[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 import org.openecomp.sdc.exception.YamlConversionException;
33
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;
38
39 public class YamlToObjectConverterTest {
40
41     private final String filePath = "./src/test/resources/config/common/";
42     private final String fileName = "configuration.yaml";
43
44     private final String testYaml = "--- \n" +
45             "object: \n" +
46             "  key: value";
47
48     private YamlToObjectConverter yamlToObjectConverter;
49
50     @Before
51     public void setUp() {
52         yamlToObjectConverter = new YamlToObjectConverter();
53     }
54
55     @Test
56     public void validateIsValidYamlReturnsTrueIfGivenYamlIsValid() {
57         boolean result = yamlToObjectConverter.isValidYaml(testYaml.getBytes());
58
59         assertTrue(result);
60     }
61
62     @Test
63     public void validateIsValidYamlReturnsFalseIfGivenYamlIsNotValid() {
64         final String testNotYaml = "testString;";
65         boolean result = yamlToObjectConverter.isValidYaml(testNotYaml.getBytes());
66
67         assertFalse(result);
68     }
69
70     @Test
71     public void validateIsValidYamlEncoded64ReturnsTrueIfGivenYamlIsEncoded64() {
72         boolean result = yamlToObjectConverter.isValidYamlEncoded64(Base64.encodeBase64(testYaml.getBytes()));
73
74         assertTrue(result);
75     }
76
77     @Test
78     public void validateIsValidYamlEncoded64ReturnsFalseIfGivenYamlIsNotEncoded64() {
79         boolean result = yamlToObjectConverter.isValidYamlEncoded64(testYaml.getBytes());
80
81         assertFalse(result);
82     }
83
84     @Test
85     public void validateConvertWithFullFilePathReturnsValidObjectCreatedFromYaml() throws YamlConversionException {
86         Configuration result = yamlToObjectConverter.convert(filePath+fileName, Configuration.class);
87
88         assertThatCreatedObjectIsValid(result);
89     }
90
91     @Test
92     public void validateConvertWithFullFilePathReturnsNullIfFileDoesNotExist() throws YamlConversionException {
93         final String wrongFileName = "wrong-configuration.yaml";
94
95         Configuration result = yamlToObjectConverter.convert(wrongFileName, Configuration.class);
96
97         assertNull(result);
98     }
99
100     @Test(expected = YamlConversionException.class)
101     public void validateConvertWithFullFilePathThrowsExceptionIfClassDoesNotMathYaml() throws YamlConversionException {
102         yamlToObjectConverter.convert(filePath + fileName, HttpClient.class);
103     }
104
105     @Test
106     public void validateConvertWithFilePathAndFileNameReturnsValidObjectCreatedFromYaml()
107         throws YamlConversionException {
108
109         Configuration result = yamlToObjectConverter.convert(filePath, Configuration.class, fileName);
110
111         assertThatCreatedObjectIsValid(result);
112     }
113
114     @Test(expected = IllegalArgumentException.class)
115     public void validateConvertWithFilePathAndFileNameThrowsExceptionIfClassIsNull() throws YamlConversionException {
116         yamlToObjectConverter.convert(filePath, null, fileName);
117     }
118
119     @Test
120     public void validateConvertFromByteArrayReturnsValidObjectCreatedFromYaml() throws IOException {
121
122         final byte[] yamlAsByteArray = getYamlAsBytesFromFile();
123
124         Configuration result = yamlToObjectConverter.convert(yamlAsByteArray, Configuration.class);
125
126         assertThatCreatedObjectIsValid(result);
127     }
128
129     @Test
130     public void validateConvertFromByteArrayReturnsNullIfByteArrayIsInCorrect() {
131
132         final byte[] yamlAsByteArray = "notYaml".getBytes();
133
134         Configuration result = yamlToObjectConverter.convert(yamlAsByteArray, Configuration.class);
135
136         assertNull(result);
137     }
138
139     @Test
140     public void validateConvertFromByteArrayReturnsNullIfClassIsInCorrect() throws IOException {
141
142         final byte[] yamlAsByteArray = getYamlAsBytesFromFile();
143
144         HttpClient result = yamlToObjectConverter.convert(yamlAsByteArray, HttpClient.class);
145
146         assertNull(result);
147     }
148
149     @Test
150     public void validateConvertFromByteArrayReturnsNullIfArrayIsNull() {
151
152         Configuration result = yamlToObjectConverter.convert((byte[])null, Configuration.class);
153
154         assertNull(result);
155     }
156
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);
161
162         return yamlAsByteArray;
163     }
164
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");
171     }
172 }