2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2023 Nordix Foundation.
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.oran.pmproducer.datastore;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.mockito.Mockito.any;
25 import static org.mockito.Mockito.anyInt;
26 import static org.mockito.Mockito.atLeast;
27 import static org.mockito.Mockito.eq;
28 import static org.mockito.Mockito.mockStatic;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
32 import java.io.IOException;
33 import java.nio.file.FileVisitor;
34 import java.nio.file.Files;
35 import java.nio.file.LinkOption;
36 import java.nio.file.Path;
37 import java.nio.file.Paths;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
41 import org.junit.jupiter.api.BeforeEach;
42 import org.junit.jupiter.api.Test;
43 import org.junit.jupiter.api.extension.ExtendWith;
44 import org.mockito.Mock;
45 import org.mockito.MockedStatic;
46 import org.mockito.Mockito;
47 import org.mockito.MockitoAnnotations;
48 import org.mockito.junit.jupiter.MockitoExtension;
49 import org.oran.pmproducer.configuration.ApplicationConfig;
50 import org.springframework.test.context.ContextConfiguration;
51 import reactor.core.publisher.Mono;
52 import reactor.test.StepVerifier;
54 @ContextConfiguration(classes = {FileStore.class})
55 @ExtendWith(MockitoExtension.class)
59 private ApplicationConfig appConfig;
61 private FileStore fileStore;
64 private Path mockPath;
68 MockitoAnnotations.initMocks(this);
69 fileStore = new FileStore(appConfig);
71 when(appConfig.getPmFilesPath()).thenReturn("/path/to/pm/files");
75 void testListObjects() {
76 when(appConfig.getPmFilesPath()).thenReturn("PM Files Path");
77 fileStore.listObjects(DataStore.Bucket.FILES, "Prefix");
78 verify(appConfig).getPmFilesPath();
81 void testListObjects3() {
82 when(appConfig.getPmFilesPath()).thenReturn("PM Files Path");
83 fileStore.listObjects(DataStore.Bucket.LOCKS, "Prefix");
84 verify(appConfig).getPmFilesPath();
88 void testListObjects_WithExistingFiles() {
89 List<Path> fileList = new ArrayList<>();
90 fileList.add(Path.of("/path/to/pm/files/file1.txt"));
91 fileList.add(Path.of("/path/to/pm/files/file2.txt"));
93 when(appConfig.getPmFilesPath()).thenReturn("/path/to/pm/files");
95 // Mock Files.walk() to return the prepared stream
96 try (MockedStatic<Files> filesMockedStatic = mockStatic(Files.class)) {
97 filesMockedStatic.when(() -> Files.walk(any(), anyInt()))
98 .thenReturn(fileList.stream());
100 StepVerifier.create(fileStore.listObjects(DataStore.Bucket.FILES, ""))
101 .expectNext("file1.txt")
102 .expectNext("file2.txt")
107 void testReadObject() {
108 when(appConfig.getPmFilesPath()).thenReturn("PM Files Path");
109 fileStore.readObject(DataStore.Bucket.FILES, "foo.txt");
110 verify(appConfig).getPmFilesPath();
113 void testReadObject2() {
114 when(appConfig.getPmFilesPath()).thenReturn("PM Files Path");
115 fileStore.readObject(DataStore.Bucket.LOCKS, "foo.txt");
116 verify(appConfig).getPmFilesPath();
120 void testReadObject_WithExistingFile() {
121 byte[] content = "Hello, world!".getBytes();
122 Path filePath = Path.of("/path/to/pm/files/test.txt");
124 try (MockedStatic<Files> filesMockedStatic = mockStatic(Files.class)) {
125 filesMockedStatic.when(() -> Files.readAllBytes(eq(filePath)))
126 .thenReturn(content);
128 StepVerifier.create(fileStore.readObject(DataStore.Bucket.FILES, "test.txt"))
134 void testCreateLock() {
135 when(appConfig.getPmFilesPath()).thenReturn("PM Files Path");
136 fileStore.createLock("Name");
137 verify(appConfig, atLeast(1)).getPmFilesPath();
140 void testCreateLock3() {
141 when(appConfig.getPmFilesPath()).thenReturn("");
142 fileStore.createLock("/");
143 verify(appConfig, atLeast(1)).getPmFilesPath();
146 void testDeleteLock() {
147 when(appConfig.getPmFilesPath()).thenReturn("PM Files Path");
148 fileStore.deleteLock("Name");
149 verify(appConfig).getPmFilesPath();
152 void testDeleteLock2() {
153 when(appConfig.getPmFilesPath()).thenReturn("");
154 fileStore.deleteLock("//");
155 verify(appConfig).getPmFilesPath();
158 void testDeleteObject() {
159 when(appConfig.getPmFilesPath()).thenReturn("PM Files Path");
160 fileStore.deleteObject(DataStore.Bucket.FILES, "Name");
161 verify(appConfig).getPmFilesPath();
164 void testDeleteObject2() {
165 when(appConfig.getPmFilesPath()).thenReturn("PM Files Path");
166 fileStore.deleteObject(DataStore.Bucket.LOCKS, "Name");
167 verify(appConfig).getPmFilesPath();
172 when(appConfig.getPmFilesPath()).thenReturn("PM Files Path");
173 fileStore.path("Name");
174 verify(appConfig).getPmFilesPath();
178 void testDeleteBucket() {
179 when(appConfig.getPmFilesPath()).thenReturn("PM Files Path");
180 fileStore.deleteBucket(DataStore.Bucket.FILES);
181 verify(appConfig).getPmFilesPath();
184 void testDeleteBucket2() throws IOException {
185 try (MockedStatic<Files> mockFiles = mockStatic(Files.class)) {
186 mockFiles.when(() -> Files.walkFileTree(Mockito.<Path>any(), Mockito.<FileVisitor<Path>>any()))
187 .thenReturn(Paths.get(System.getProperty("java.io.tmpdir"), "test.txt"));
188 mockFiles.when(() -> Files.exists(Mockito.<Path>any(), (LinkOption[]) any())).thenReturn(true);
189 when(appConfig.getPmFilesPath()).thenReturn("");
190 fileStore.deleteBucket(DataStore.Bucket.LOCKS);
191 mockFiles.verify(() -> Files.exists(Mockito.<Path>any(), (LinkOption[]) any()));
192 mockFiles.verify(() -> Files.walkFileTree(Mockito.<Path>any(), Mockito.<FileVisitor<Path>>any()));
193 verify(appConfig).getPmFilesPath();
197 void testDeleteBucket3() throws IOException {
198 try (MockedStatic<Files> mockFiles = mockStatic(Files.class)) {
199 mockFiles.when(() -> Files.walkFileTree(Mockito.<Path>any(), Mockito.<FileVisitor<Path>>any()))
200 .thenThrow(new IOException("OK"));
201 mockFiles.when(() -> Files.exists(Mockito.<Path>any(), (LinkOption[]) any())).thenReturn(true);
202 when(appConfig.getPmFilesPath()).thenReturn("");
203 fileStore.deleteBucket(DataStore.Bucket.LOCKS);
204 mockFiles.verify(() -> Files.exists(Mockito.<Path>any(), (LinkOption[]) any()));
205 mockFiles.verify(() -> Files.walkFileTree(Mockito.<Path>any(), Mockito.<FileVisitor<Path>>any()));
206 verify(appConfig, atLeast(1)).getPmFilesPath();
211 void testCreateLock_Success() throws IOException {
212 Path lockPath = Path.of("/path/to/pm/files/locks/lock.txt");
214 when(appConfig.getPmFilesPath()).thenReturn("/path/to/pm/files");
216 try (MockedStatic<Files> filesMockedStatic = mockStatic(Files.class)) {
217 filesMockedStatic.when(() -> Files.createDirectories(lockPath.getParent()))
218 .thenReturn(lockPath.getParent());
220 try (MockedStatic<Path> pathMockedStatic = mockStatic(Path.class)) {
221 filesMockedStatic.when(() -> Files.createFile(any(Path.class))).thenReturn(lockPath);
223 String name = "test.txt";
224 String[] pathComponents = {"pmFiles", name};
226 when(fileStore.path(Arrays.toString(pathComponents))).thenReturn(mockPath);
227 Path path = fileStore.path(Arrays.toString(pathComponents));
228 assertEquals(mockPath, path);
234 void testCopyFileTo_Failure() {
235 // Define dummy values for testing
236 Path from = Paths.get("non-existent-file.txt");
237 String to = "destination-folder";
239 // Use StepVerifier to test the method
240 Mono<String> resultMono = fileStore.copyFileTo(from, to);
242 StepVerifier.create(resultMono)
243 .expectError(IOException.class)