Improve code quality in Appconfig
[dcaegen2/platform.git] / mod / bpgenerator / src / test / java / org / onap / blueprintgenerator / models / blueprint / AppconfigTest.java
1 /*============LICENSE_START=======================================================
2  org.onap.dcae
3  ================================================================================
4  Copyright (c) 2020 Nokia. All rights reserved.
5  ================================================================================
6  Licensed under the Apache License, Version 2.0 (the "License");
7  you may not use this file except in compliance with the License.
8  You may obtain a copy of the License at
9
10  http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  See the License for the specific language governing permissions and
16  limitations under the License.
17  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.blueprintgenerator.models.blueprint;
21
22 import static org.junit.Assert.assertEquals;
23
24 import java.io.BufferedReader;
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.InputStream;
29 import java.io.InputStreamReader;
30 import java.nio.charset.StandardCharsets;
31 import java.util.LinkedHashMap;
32 import java.util.TreeMap;
33 import java.util.stream.Collectors;
34 import org.junit.Test;
35 import org.onap.blueprintgenerator.models.componentspec.ComponentSpec;
36
37 public class AppconfigTest {
38
39     @Test
40     public void createAppconfigShouldReturnExpectedResult() throws FileNotFoundException {
41         TreeMap<String, LinkedHashMap<String, Object>> inputs = new TreeMap<String, LinkedHashMap<String, Object>>();
42         ComponentSpec cs = new ComponentSpec();
43         cs.createComponentSpecFromFile("TestCases/testComponentSpec.json");
44
45         TreeMap<String, LinkedHashMap<String, Object>> result = new Appconfig().createAppconfig(inputs, cs, "", false);
46
47         assertEquals(getExpectedStringFromFile(), result.toString());
48     }
49
50     private String getExpectedStringFromFile() throws FileNotFoundException {
51         File file = new File("TestCases/expects/createAppConfigResult.txt");
52         InputStream inputStream = new FileInputStream(file);
53         return readFromInputStream(inputStream);
54     }
55
56     private String readFromInputStream(InputStream inputStream) {
57         return new BufferedReader(
58             new InputStreamReader(inputStream, StandardCharsets.UTF_8))
59             .lines()
60             .collect(Collectors.joining(""));
61     }
62 }