27a7248959258e8fb403d1bec6b5bc9465626ef2
[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.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.Test;
41 import org.junit.jupiter.api.extension.ExtendWith;
42 import org.junit.jupiter.api.io.TempDir;
43 import org.mockito.Mock;
44 import org.mockito.junit.jupiter.MockitoExtension;
45 import org.onap.ccsdk.oran.a1policymanagementservice.utils.LoggingUtils;
46
47 @ExtendWith(MockitoExtension.class)
48 class ConfigurationFileTest {
49     @Mock
50     ApplicationConfig applicationConfigMock;
51
52     @TempDir
53     public File temporaryFolder;
54
55     @Test
56     void writeFileWithError_shouldThrowException() throws Exception {
57         File tempJsonFile = new File(temporaryFolder, "config.json");
58         String filePath = tempJsonFile.getAbsolutePath();
59
60         ConfigurationFile configFileUnderTestSpy = spy(new ConfigurationFile(applicationConfigMock));
61
62         when(applicationConfigMock.getLocalConfigurationFilePath()).thenReturn(filePath);
63         String errorMessage = "Error";
64         doThrow(new IOException(errorMessage)).when(configFileUnderTestSpy).getFileWriter(any(String.class));
65
66         Exception actualException =
67                 assertThrows(IOException.class, () -> configFileUnderTestSpy.writeFile(new JsonObject()));
68
69         assertThat(actualException.getMessage()).isEqualTo(errorMessage);
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 read: " + filePath + ", Not a JSON Object: null");
119     }
120 }