2 * ============LICENSE_START=======================================================
3 * Copyright (C) 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.datafile.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.datafile.configuration.AppConfig;
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 AppConfig appConfig;
61 private FileStore fileStore;
64 private Path mockPath;
68 MockitoAnnotations.initMocks(this);
69 fileStore = new FileStore(appConfig);
71 when(appConfig.getCollectedFilesPath()).thenReturn("/path/to/collected/files");
75 void testListObjects() {
76 when(appConfig.getCollectedFilesPath()).thenReturn("Collected Files Path");
77 fileStore.listObjects(DataStore.Bucket.FILES, "Prefix");
78 verify(appConfig).getCollectedFilesPath();
81 void testListObjects3() {
82 when(appConfig.getCollectedFilesPath()).thenReturn("Collected Files Path");
83 fileStore.listObjects(DataStore.Bucket.LOCKS, "Prefix");
84 verify(appConfig).getCollectedFilesPath();
88 void testListObjects_WithExistingFiles() {
89 List<Path> fileList = new ArrayList<>();
90 fileList.add(Path.of("/path/to/collected/files/file1.txt"));
91 fileList.add(Path.of("/path/to/collected/files/file2.txt"));
93 when(appConfig.getCollectedFilesPath()).thenReturn("/path/to/collected/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.getCollectedFilesPath()).thenReturn("Collected Files Path");
109 fileStore.readObject(DataStore.Bucket.FILES, "foo.txt");
110 verify(appConfig).getCollectedFilesPath();
113 void testReadObject2() {
114 when(appConfig.getCollectedFilesPath()).thenReturn("Collected Files Path");
115 fileStore.readObject(DataStore.Bucket.LOCKS, "foo.txt");
116 verify(appConfig).getCollectedFilesPath();
120 void testReadObject_WithExistingFile() {
121 byte[] content = "Hello, world!".getBytes();
122 Path filePath = Path.of("/path/to/collected/files/test.txt");
124 try (MockedStatic<Files> filesMockedStatic = mockStatic(Files.class)) {
125 filesMockedStatic.when(() -> Files.readAllBytes(eq(filePath))).
128 StepVerifier.create(fileStore.readObject(DataStore.Bucket.FILES, "test.txt")).
134 void testCreateLock() {
135 when(appConfig.getCollectedFilesPath()).thenReturn("Collected Files Path");
136 fileStore.createLock("Name");
137 verify(appConfig, atLeast(1)).getCollectedFilesPath();
140 void testCreateLock3() {
141 when(appConfig.getCollectedFilesPath()).thenReturn("");
142 fileStore.createLock("/");
143 verify(appConfig, atLeast(1)).getCollectedFilesPath();
146 void testDeleteLock() {
147 when(appConfig.getCollectedFilesPath()).thenReturn("Collected Files Path");
148 fileStore.deleteLock("Name");
149 verify(appConfig).getCollectedFilesPath();
152 void testDeleteLock2() {
153 when(appConfig.getCollectedFilesPath()).thenReturn("");
154 fileStore.deleteLock("//");
155 verify(appConfig).getCollectedFilesPath();
158 void testDeleteObject() {
159 when(appConfig.getCollectedFilesPath()).thenReturn("Collected Files Path");
160 fileStore.deleteObject(DataStore.Bucket.FILES, "Name");
161 verify(appConfig).getCollectedFilesPath();
164 void testDeleteObject2() {
165 when(appConfig.getCollectedFilesPath()).thenReturn("Collected Files Path");
166 fileStore.deleteObject(DataStore.Bucket.LOCKS, "Name");
167 verify(appConfig).getCollectedFilesPath();
172 when(appConfig.getCollectedFilesPath()).thenReturn("Collected Files Path");
173 fileStore.path("Name");
174 verify(appConfig).getCollectedFilesPath();
177 void testFileExists() {
178 when(appConfig.getCollectedFilesPath()).thenReturn("Collected Files Path");
179 fileStore.fileExists(DataStore.Bucket.FILES, "Key");
180 verify(appConfig).getCollectedFilesPath();
183 void testFileExists2() {
184 when(appConfig.getCollectedFilesPath()).thenReturn("Collected Files Path");
185 fileStore.fileExists(DataStore.Bucket.LOCKS, "Key");
186 verify(appConfig).getCollectedFilesPath();
189 void testDeleteBucket() {
190 when(appConfig.getCollectedFilesPath()).thenReturn("Collected Files Path");
191 fileStore.deleteBucket(DataStore.Bucket.FILES);
192 verify(appConfig).getCollectedFilesPath();
195 void testDeleteBucket2() throws IOException {
196 try (MockedStatic<Files> mockFiles = mockStatic(Files.class)) {
197 mockFiles.when(() -> Files.walkFileTree(Mockito.<Path>any(), Mockito.<FileVisitor<Path>>any())).
198 thenReturn(Paths.get(System.getProperty("java.io.tmpdir"), "test.txt"));
199 mockFiles.when(() -> Files.exists(Mockito.<Path>any(), (LinkOption[]) any())).thenReturn(true);
200 when(appConfig.getCollectedFilesPath()).thenReturn("");
201 fileStore.deleteBucket(DataStore.Bucket.LOCKS);
202 mockFiles.verify(() -> Files.exists(Mockito.<Path>any(), (LinkOption[]) any()));
203 mockFiles.verify(() -> Files.walkFileTree(Mockito.<Path>any(), Mockito.<FileVisitor<Path>>any()));
204 verify(appConfig).getCollectedFilesPath();
208 void testDeleteBucket3() throws IOException {
209 try (MockedStatic<Files> mockFiles = mockStatic(Files.class)) {
210 mockFiles.when(() -> Files.walkFileTree(Mockito.<Path>any(), Mockito.<FileVisitor<Path>>any())).
211 thenThrow(new IOException("OK"));
212 mockFiles.when(() -> Files.exists(Mockito.<Path>any(), (LinkOption[]) any())).thenReturn(true);
213 when(appConfig.getCollectedFilesPath()).thenReturn("");
214 fileStore.deleteBucket(DataStore.Bucket.LOCKS);
215 mockFiles.verify(() -> Files.exists(Mockito.<Path>any(), (LinkOption[]) any()));
216 mockFiles.verify(() -> Files.walkFileTree(Mockito.<Path>any(), Mockito.<FileVisitor<Path>>any()));
217 verify(appConfig, atLeast(1)).getCollectedFilesPath();
222 void testCreateLock_Success() throws IOException {
223 Path lockPath = Path.of("/path/to/collected/files/locks/lock.txt");
225 when(appConfig.getCollectedFilesPath()).thenReturn("/path/to/collected/files");
227 try (MockedStatic<Files> filesMockedStatic = mockStatic(Files.class)) {
228 filesMockedStatic.when(() -> Files.createDirectories(lockPath.getParent())).
229 thenReturn(lockPath.getParent());
231 try (MockedStatic<Path> pathMockedStatic = mockStatic(Path.class)) {
232 filesMockedStatic.when(() -> Files.createFile(any(Path.class))).thenReturn(lockPath);
234 String name = "test.txt";
235 String[] pathComponents = { "collectedFiles", name };
237 when(fileStore.path(Arrays.toString(pathComponents))).thenReturn(mockPath);
238 Path path = fileStore.path(Arrays.toString(pathComponents));
239 assertEquals(mockPath, path);
245 void testCopyFileTo_Failure() {
246 // Define dummy values for testing
247 Path from = Paths.get("non-existent-file.txt");
248 String to = "destination-folder";
250 // Use StepVerifier to test the method
251 Mono<String> resultMono = fileStore.copyFileTo(from, to);
253 StepVerifier.create(resultMono).
254 expectError(IOException.class).