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
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.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;
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;
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;
45 @ExtendWith(MockitoExtension.class)
46 class ConfigurationFileTest {
48 ApplicationConfig applicationConfigMock;
51 public File temporaryFolder;
54 void writeFileWithError_shouldThrowExceptionAndLogError() throws Exception {
55 File tempJsonFile = new File(temporaryFolder, "config.json");
56 String filePath = tempJsonFile.getAbsolutePath();
58 ConfigurationFile configFileUnderTestSpy = spy(new ConfigurationFile(applicationConfigMock));
60 when(applicationConfigMock.getLocalConfigurationFilePath()).thenReturn(filePath);
61 doThrow(new IOException("Error")).when(configFileUnderTestSpy).getFileWriter(any(String.class));
63 ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ConfigurationFile.class, Level.ERROR);
64 Exception actualException =
65 assertThrows(ServiceException.class, () -> configFileUnderTestSpy.writeFile(new JsonObject()));
67 assertThat(actualException.getMessage()).startsWith("Local configuration file not written");
69 assertThat(logAppender.list.get(0).getFormattedMessage()).startsWith("Local configuration file not written");
73 void writeAndReadFile_shouldBeOk() throws Exception {
74 File tempJsonFile = new File(temporaryFolder, "config.json");
75 String filePath = tempJsonFile.getAbsolutePath();
77 ConfigurationFile configFileUnderTest = new ConfigurationFile(applicationConfigMock);
79 JsonObject content = JsonParser.parseString("{\"test\":\"test\"}").getAsJsonObject();
81 when(applicationConfigMock.getLocalConfigurationFilePath()).thenReturn(filePath);
83 configFileUnderTest.writeFile(content);
85 Optional<JsonObject> readContent = configFileUnderTest.readFile();
87 assertThat(readContent).isNotEmpty().hasValue(content);
91 void readWhenFileMissing_shouldReturnEmpty() {
92 ConfigurationFile configFileUnderTest = new ConfigurationFile(applicationConfigMock);
94 String filePath = "configFile.json";
95 when(applicationConfigMock.getLocalConfigurationFilePath()).thenReturn(filePath);
97 Optional<JsonObject> readContent = configFileUnderTest.readFile();
99 assertThat(readContent).isEmpty();
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());
108 ConfigurationFile configFileUnderTest = new ConfigurationFile(applicationConfigMock);
110 when(applicationConfigMock.getLocalConfigurationFilePath()).thenReturn(filePath);
112 ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ConfigurationFile.class, Level.ERROR);
113 Optional<JsonObject> readContent = configFileUnderTest.readFile();
115 assertThat(readContent).isEmpty();
117 assertThat(logAppender.list.get(0).getFormattedMessage())
118 .isEqualTo("Local configuration file not loaded: " + filePath + ", Not a JSON Object: null");