ad4cbb81310625a742afd3f825ab3e350d9b8a60
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * Copyright (C) 2020 Nordix Foundation. All rights reserved.
4  * ======================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ========================LICENSE_END===================================
17  */
18
19 package org.onap.ccsdk.oran.a1policymanagementservice.configuration;
20
21 import static org.assertj.core.api.Assertions.assertThat;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.junit.jupiter.api.Assertions.assertThrows;
24 import static org.mockito.Mockito.doThrow;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.when;
27
28 import ch.qos.logback.classic.Level;
29 import ch.qos.logback.classic.spi.ILoggingEvent;
30 import ch.qos.logback.core.read.ListAppender;
31 import com.google.gson.JsonObject;
32 import com.google.gson.JsonParser;
33 import java.io.File;
34 import java.io.IOException;
35 import java.nio.file.Files;
36 import java.util.Optional;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.api.extension.ExtendWith;
39 import org.junit.jupiter.api.io.TempDir;
40 import org.mockito.Mock;
41 import org.mockito.junit.jupiter.MockitoExtension;
42 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
43 import org.onap.ccsdk.oran.a1policymanagementservice.utils.LoggingUtils;
44
45 @ExtendWith(MockitoExtension.class)
46 class ConfigurationFileTest {
47     @Mock
48     ApplicationConfig applicationConfigMock;
49
50     @TempDir
51     public File temporaryFolder;
52
53     @Test
54     void writeFileWithError_shouldThrowExceptionAndLogError() throws Exception {
55         File tempJsonFile = new File(temporaryFolder, "config.json");
56         String filePath = tempJsonFile.getAbsolutePath();
57
58         ConfigurationFile configFileUnderTestSpy = spy(new ConfigurationFile(applicationConfigMock));
59
60         when(applicationConfigMock.getLocalConfigurationFilePath()).thenReturn(filePath);
61         doThrow(new IOException("Error")).when(configFileUnderTestSpy).getFileWriter(any(String.class));
62
63         ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ConfigurationFile.class, Level.ERROR);
64         Exception actualException =
65                 assertThrows(ServiceException.class, () -> configFileUnderTestSpy.writeFile(new JsonObject()));
66
67         assertThat(actualException.getMessage()).startsWith("Local configuration file not written");
68
69         assertThat(logAppender.list.get(0).getFormattedMessage()).startsWith("Local configuration file not written");
70     }
71
72     @Test
73     void writeAndReadFile_shouldBeOk() throws Exception {
74         File tempJsonFile = new File(temporaryFolder, "config.json");
75         String filePath = tempJsonFile.getAbsolutePath();
76
77         ConfigurationFile configFileUnderTest = new ConfigurationFile(applicationConfigMock);
78
79         JsonObject content = JsonParser.parseString("{\"test\":\"test\"}").getAsJsonObject();
80
81         when(applicationConfigMock.getLocalConfigurationFilePath()).thenReturn(filePath);
82
83         configFileUnderTest.writeFile(content);
84
85         Optional<JsonObject> readContent = configFileUnderTest.readFile();
86
87         assertThat(readContent).isNotEmpty().hasValue(content);
88     }
89
90     @Test
91     void readWhenFileMissing_shouldReturnEmpty() {
92         ConfigurationFile configFileUnderTest = new ConfigurationFile(applicationConfigMock);
93
94         String filePath = "configFile.json";
95         when(applicationConfigMock.getLocalConfigurationFilePath()).thenReturn(filePath);
96
97         Optional<JsonObject> readContent = configFileUnderTest.readFile();
98
99         assertThat(readContent).isEmpty();
100     }
101
102     @Test
103     void readWhenFileWithIoError_shouldReturnEmptyAndLogError() throws Exception {
104         File tempJsonFile = new File(temporaryFolder, "config.json");
105         String filePath = tempJsonFile.getAbsolutePath();
106         Files.write(tempJsonFile.toPath(), "".getBytes());
107
108         ConfigurationFile configFileUnderTest = new ConfigurationFile(applicationConfigMock);
109
110         when(applicationConfigMock.getLocalConfigurationFilePath()).thenReturn(filePath);
111
112         ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ConfigurationFile.class, Level.ERROR);
113         Optional<JsonObject> readContent = configFileUnderTest.readFile();
114
115         assertThat(readContent).isEmpty();
116
117         assertThat(logAppender.list.get(0).getFormattedMessage())
118                 .isEqualTo("Local configuration file not loaded: " + filePath + ", Not a JSON Object: null");
119     }
120 }