b79a4ca88a1ff33d6dacb7a49bdbfa8a319c5c90
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * Copyright (C) 2020-2023 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.junit.jupiter.api.Assertions.assertThrows;
23 import static org.mockito.ArgumentMatchers.any;
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
32 import com.google.gson.JsonObject;
33 import com.google.gson.JsonParser;
34
35 import java.io.File;
36 import java.io.IOException;
37 import java.nio.file.Files;
38 import java.util.Optional;
39
40 import org.junit.jupiter.api.DisplayName;
41 import org.junit.jupiter.api.Test;
42 import org.junit.jupiter.api.extension.ExtendWith;
43 import org.junit.jupiter.api.io.TempDir;
44 import org.mockito.Mock;
45 import org.mockito.junit.jupiter.MockitoExtension;
46 import org.onap.ccsdk.oran.a1policymanagementservice.utils.LoggingUtils;
47
48 @ExtendWith(MockitoExtension.class)
49 class ConfigurationFileTest {
50     @Mock
51     ApplicationConfig applicationConfigMock;
52
53     @TempDir
54     public File temporaryFolder;
55
56     @Test
57     @DisplayName("test write File With Error should Throw Exception")
58     void writeFileWithError_shouldThrowException() throws Exception {
59         File tempJsonFile = new File(temporaryFolder, "config.json");
60         String filePath = tempJsonFile.getAbsolutePath();
61
62         ConfigurationFile configFileUnderTestSpy = spy(new ConfigurationFile(applicationConfigMock));
63
64         when(applicationConfigMock.getLocalConfigurationFilePath()).thenReturn(filePath);
65         String errorMessage = "Error";
66         doThrow(new IOException(errorMessage)).when(configFileUnderTestSpy).getFileWriter(any(String.class));
67
68         Exception actualException =
69                 assertThrows(IOException.class, () -> configFileUnderTestSpy.writeFile(new JsonObject()));
70
71         assertThat(actualException.getMessage()).isEqualTo(errorMessage);
72     }
73
74     @Test
75     @DisplayName("test write And Read File should Be Ok")
76     void writeAndReadFile_shouldBeOk() throws Exception {
77         File tempJsonFile = new File(temporaryFolder, "config.json");
78         String filePath = tempJsonFile.getAbsolutePath();
79
80         ConfigurationFile configFileUnderTest = new ConfigurationFile(applicationConfigMock);
81
82         JsonObject content = JsonParser.parseString("{\"test\":\"test\"}").getAsJsonObject();
83
84         when(applicationConfigMock.getLocalConfigurationFilePath()).thenReturn(filePath);
85
86         configFileUnderTest.writeFile(content);
87
88         Optional<JsonObject> readContent = configFileUnderTest.readFile();
89
90         assertThat(readContent).isNotEmpty().hasValue(content);
91     }
92
93     @Test
94     @DisplayName("test read When File Missing should Return Empty")
95     void readWhenFileMissing_shouldReturnEmpty() {
96         ConfigurationFile configFileUnderTest = new ConfigurationFile(applicationConfigMock);
97
98         String filePath = "configFile.json";
99         when(applicationConfigMock.getLocalConfigurationFilePath()).thenReturn(filePath);
100
101         Optional<JsonObject> readContent = configFileUnderTest.readFile();
102
103         assertThat(readContent).isEmpty();
104     }
105
106     @Test
107     @DisplayName("test read When File With Io Error should Return Empty And Log Error")
108     void readWhenFileWithIoError_shouldReturnEmptyAndLogError() throws Exception {
109         File tempJsonFile = new File(temporaryFolder, "config.json");
110         String filePath = tempJsonFile.getAbsolutePath();
111         Files.write(tempJsonFile.toPath(), "".getBytes());
112
113         ConfigurationFile configFileUnderTest = new ConfigurationFile(applicationConfigMock);
114
115         when(applicationConfigMock.getLocalConfigurationFilePath()).thenReturn(filePath);
116
117         ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ConfigurationFile.class, Level.ERROR);
118         Optional<JsonObject> readContent = configFileUnderTest.readFile();
119
120         assertThat(readContent).isEmpty();
121
122         assertThat(logAppender.list.get(0).getFormattedMessage())
123                 .isEqualTo("Local configuration file not read: " + filePath + ", Not a JSON Object: null");
124     }
125 }