Config fetch for VESCollector through DCAE-SDK (CBS Client)
[dcaegen2/collectors/ves.git] / src / test / java / org / onap / dcae / configuration / ConfigFilesFacadeTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dcaegen2.collectors.ves
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2018,2020 Nokia. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.dcae.configuration;
22
23 import static io.vavr.API.Map;
24 import static io.vavr.API.Some;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.onap.dcae.TestingUtilities.assertFailureHasInfo;
27 import static org.onap.dcae.TestingUtilities.assertJSONObjectsEqual;
28 import static org.onap.dcae.TestingUtilities.createTemporaryFile;
29 import static org.onap.dcae.TestingUtilities.readFile;
30 import static org.onap.dcae.TestingUtilities.readJSONFromFile;
31
32 import io.vavr.collection.Map;
33 import io.vavr.control.Try;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
36 import org.json.JSONObject;
37 import org.junit.Test;
38
39 public class ConfigFilesFacadeTest {
40
41     private static final Path NON_EXISTENT = Paths.get("/non-existent");
42     private static final ConfigFilesFacade TO_NON_EXISTENT_POINTING_FACADE = new ConfigFilesFacade(NON_EXISTENT,
43         NON_EXISTENT);
44
45     @Test
46     public void shouldReadPropertyFile() {
47         // given
48         Path temporaryFile = createTemporaryFile("some.property=10");
49
50         // when
51         ConfigFilesFacade configFilesFacade = new ConfigFilesFacade(temporaryFile, temporaryFile);
52
53         Try<Map<String, String>> propertiesConfigurations = configFilesFacade.readCollectorProperties();
54
55         // then
56         assertThat(propertiesConfigurations.isSuccess()).isTrue();
57         assertThat(propertiesConfigurations.get().containsKey("some.property")).isTrue();
58         assertThat(propertiesConfigurations.get().get("some.property")).isEqualTo(Some("10"));
59     }
60
61
62     @Test
63     public void shouldReadDMaaPFile() {
64         // given
65         Path temporaryFile = createTemporaryFile("{}");
66
67         // when
68         ConfigFilesFacade configFilesFacade = new ConfigFilesFacade(temporaryFile, temporaryFile);
69
70         Try<JSONObject> dMaaPConfiguration = configFilesFacade.readDMaaPConfiguration();
71
72         // then
73         assertThat(dMaaPConfiguration.isSuccess()).isTrue();
74         assertThat(dMaaPConfiguration.get().toString()).isEqualTo("{}");
75     }
76
77     @Test
78     public void shouldWriteDMaaPConf() {
79         // given
80         Path temporaryFile = createTemporaryFile("{}");
81         JSONObject desiredConf = new JSONObject("{\"key\": 1}");
82
83         // when
84         ConfigFilesFacade configFilesFacade = new ConfigFilesFacade(temporaryFile, temporaryFile);
85
86         Try<Void> propertiesConfigurations = configFilesFacade.writeDMaaPConfiguration(desiredConf);
87
88         // then
89         assertThat(propertiesConfigurations.isSuccess()).isTrue();
90         assertJSONObjectsEqual(readJSONFromFile(temporaryFile), desiredConf);
91     }
92
93
94     @Test
95     public void shouldWriteProperties() {
96         // given
97         Path temporaryFile = createTemporaryFile("{}");
98
99         // when
100         ConfigFilesFacade configFilesFacade = new ConfigFilesFacade(temporaryFile, temporaryFile);
101         Try<Void> propertiesConfigurations = configFilesFacade.writeProperties(Map("prop1", "hi"));
102
103         // then
104         assertThat(propertiesConfigurations.isSuccess()).isTrue();
105         assertThat(readFile(temporaryFile).trim()).isEqualTo("prop1 = hi");
106     }
107
108     @Test
109     public void shouldContainPropertiesPathInCaseOfFailures() {
110         Try<Map<String, String>> result = TO_NON_EXISTENT_POINTING_FACADE.readCollectorProperties();
111         assertThat(result.isFailure()).isTrue();
112         assertFailureHasInfo(result, NON_EXISTENT.toString());
113     }
114
115     @Test
116     public void shouldContainDMaaPPathPathInCaseOfFailures() {
117         Try<JSONObject> result = TO_NON_EXISTENT_POINTING_FACADE.readDMaaPConfiguration();
118         assertThat(result.isFailure()).isTrue();
119         assertFailureHasInfo(result, NON_EXISTENT.toString());
120     }
121
122     @Test
123     public void shouldContainPropertiesPathPathInCaseOfFailuresOnWrite() {
124         // given
125         Try<Void> result = TO_NON_EXISTENT_POINTING_FACADE.writeProperties(Map("any", "any"));
126         assertThat(result.isFailure()).isTrue();
127         assertFailureHasInfo(result, NON_EXISTENT.toString());
128     }
129
130     @Test
131     public void shouldContainDMaaPPathPathInCaseOfFailuresOnWrite() {
132         // given
133         Try<Void> result = TO_NON_EXISTENT_POINTING_FACADE.writeDMaaPConfiguration(new JSONObject());
134         assertThat(result.isFailure()).isTrue();
135         assertFailureHasInfo(result, NON_EXISTENT.toString());
136     }
137 }