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
9 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
19 package org.onap.ccsdk.oran.a1policymanagementservice.configuration;
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;
28 import ch.qos.logback.classic.Level;
29 import ch.qos.logback.classic.spi.ILoggingEvent;
30 import ch.qos.logback.core.read.ListAppender;
32 import com.google.gson.JsonObject;
33 import com.google.gson.JsonParser;
36 import java.io.IOException;
37 import java.nio.file.Files;
38 import java.util.Optional;
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;
48 @ExtendWith(MockitoExtension.class)
49 class ConfigurationFileTest {
51 ApplicationConfig applicationConfigMock;
54 public File temporaryFolder;
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();
62 ConfigurationFile configFileUnderTestSpy = spy(new ConfigurationFile(applicationConfigMock));
64 when(applicationConfigMock.getLocalConfigurationFilePath()).thenReturn(filePath);
65 String errorMessage = "Error";
66 doThrow(new IOException(errorMessage)).when(configFileUnderTestSpy).getFileWriter(any(String.class));
68 Exception actualException =
69 assertThrows(IOException.class, () -> configFileUnderTestSpy.writeFile(new JsonObject()));
71 assertThat(actualException.getMessage()).isEqualTo(errorMessage);
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();
80 ConfigurationFile configFileUnderTest = new ConfigurationFile(applicationConfigMock);
82 JsonObject content = JsonParser.parseString("{\"test\":\"test\"}").getAsJsonObject();
84 when(applicationConfigMock.getLocalConfigurationFilePath()).thenReturn(filePath);
86 configFileUnderTest.writeFile(content);
88 Optional<JsonObject> readContent = configFileUnderTest.readFile();
90 assertThat(readContent).isNotEmpty().hasValue(content);
94 @DisplayName("test read When File Missing should Return Empty")
95 void readWhenFileMissing_shouldReturnEmpty() {
96 ConfigurationFile configFileUnderTest = new ConfigurationFile(applicationConfigMock);
98 String filePath = "configFile.json";
99 when(applicationConfigMock.getLocalConfigurationFilePath()).thenReturn(filePath);
101 Optional<JsonObject> readContent = configFileUnderTest.readFile();
103 assertThat(readContent).isEmpty();
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());
113 ConfigurationFile configFileUnderTest = new ConfigurationFile(applicationConfigMock);
115 when(applicationConfigMock.getLocalConfigurationFilePath()).thenReturn(filePath);
117 ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ConfigurationFile.class, Level.ERROR);
118 Optional<JsonObject> readContent = configFileUnderTest.readFile();
120 assertThat(readContent).isEmpty();
122 assertThat(logAppender.list.get(0).getFormattedMessage())
123 .isEqualTo("Local configuration file not read: " + filePath + ", Not a JSON Object: null");