Improve Test coverage of DFC
[nonrtric/plt/ranpm.git] / datafilecollector / src / test / java / org / oran / datafile / datastore / FileStoreTest.java
1 /*-
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
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  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.oran.datafile.datastore;
22
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;
31
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;
53
54 @ContextConfiguration(classes = { FileStore.class })
55 @ExtendWith(MockitoExtension.class)
56 class FileStoreTest {
57
58     @Mock
59     private AppConfig appConfig;
60
61     private FileStore fileStore;
62
63     @Mock
64     private Path mockPath;
65
66     @BeforeEach
67     void setup() {
68         MockitoAnnotations.initMocks(this);
69         fileStore = new FileStore(appConfig);
70
71         when(appConfig.getCollectedFilesPath()).thenReturn("/path/to/collected/files");
72     }
73
74     @Test
75     void testListObjects() {
76         when(appConfig.getCollectedFilesPath()).thenReturn("Collected Files Path");
77         fileStore.listObjects(DataStore.Bucket.FILES, "Prefix");
78         verify(appConfig).getCollectedFilesPath();
79     }
80     @Test
81     void testListObjects3() {
82         when(appConfig.getCollectedFilesPath()).thenReturn("Collected Files Path");
83         fileStore.listObjects(DataStore.Bucket.LOCKS, "Prefix");
84         verify(appConfig).getCollectedFilesPath();
85     }
86
87     @Test
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"));
92
93         when(appConfig.getCollectedFilesPath()).thenReturn("/path/to/collected/files");
94
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());
99
100             StepVerifier.create(fileStore.listObjects(DataStore.Bucket.FILES, "")).
101                  expectNext("file1.txt").
102                  expectNext("file2.txt").
103                  expectComplete();
104         }
105     }
106     @Test
107     void testReadObject() {
108         when(appConfig.getCollectedFilesPath()).thenReturn("Collected Files Path");
109         fileStore.readObject(DataStore.Bucket.FILES, "foo.txt");
110         verify(appConfig).getCollectedFilesPath();
111     }
112     @Test
113     void testReadObject2() {
114         when(appConfig.getCollectedFilesPath()).thenReturn("Collected Files Path");
115         fileStore.readObject(DataStore.Bucket.LOCKS, "foo.txt");
116         verify(appConfig).getCollectedFilesPath();
117     }
118
119     @Test
120     void testReadObject_WithExistingFile() {
121         byte[] content = "Hello, world!".getBytes();
122         Path filePath = Path.of("/path/to/collected/files/test.txt");
123
124         try (MockedStatic<Files> filesMockedStatic = mockStatic(Files.class)) {
125             filesMockedStatic.when(() -> Files.readAllBytes(eq(filePath))).
126                  thenReturn(content);
127
128             StepVerifier.create(fileStore.readObject(DataStore.Bucket.FILES, "test.txt")).
129                  expectNext(content).
130                  verifyComplete();
131         }
132     }
133     @Test
134     void testCreateLock() {
135         when(appConfig.getCollectedFilesPath()).thenReturn("Collected Files Path");
136         fileStore.createLock("Name");
137         verify(appConfig, atLeast(1)).getCollectedFilesPath();
138     }
139     @Test
140     void testCreateLock3() {
141         when(appConfig.getCollectedFilesPath()).thenReturn("");
142         fileStore.createLock("/");
143         verify(appConfig, atLeast(1)).getCollectedFilesPath();
144     }
145     @Test
146     void testDeleteLock() {
147         when(appConfig.getCollectedFilesPath()).thenReturn("Collected Files Path");
148         fileStore.deleteLock("Name");
149         verify(appConfig).getCollectedFilesPath();
150     }
151     @Test
152     void testDeleteLock2() {
153         when(appConfig.getCollectedFilesPath()).thenReturn("");
154         fileStore.deleteLock("//");
155         verify(appConfig).getCollectedFilesPath();
156     }
157     @Test
158     void testDeleteObject() {
159         when(appConfig.getCollectedFilesPath()).thenReturn("Collected Files Path");
160         fileStore.deleteObject(DataStore.Bucket.FILES, "Name");
161         verify(appConfig).getCollectedFilesPath();
162     }
163     @Test
164     void testDeleteObject2() {
165         when(appConfig.getCollectedFilesPath()).thenReturn("Collected Files Path");
166         fileStore.deleteObject(DataStore.Bucket.LOCKS, "Name");
167         verify(appConfig).getCollectedFilesPath();
168     }
169
170     @Test
171     void testPath() {
172         when(appConfig.getCollectedFilesPath()).thenReturn("Collected Files Path");
173         fileStore.path("Name");
174         verify(appConfig).getCollectedFilesPath();
175     }
176     @Test
177     void testFileExists() {
178         when(appConfig.getCollectedFilesPath()).thenReturn("Collected Files Path");
179         fileStore.fileExists(DataStore.Bucket.FILES, "Key");
180         verify(appConfig).getCollectedFilesPath();
181     }
182     @Test
183     void testFileExists2() {
184         when(appConfig.getCollectedFilesPath()).thenReturn("Collected Files Path");
185         fileStore.fileExists(DataStore.Bucket.LOCKS, "Key");
186         verify(appConfig).getCollectedFilesPath();
187     }
188     @Test
189     void testDeleteBucket() {
190         when(appConfig.getCollectedFilesPath()).thenReturn("Collected Files Path");
191         fileStore.deleteBucket(DataStore.Bucket.FILES);
192         verify(appConfig).getCollectedFilesPath();
193     }
194     @Test
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();
205         }
206     }
207     @Test
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();
218         }
219     }
220
221     @Test
222     void testCreateLock_Success() throws IOException {
223         Path lockPath = Path.of("/path/to/collected/files/locks/lock.txt");
224
225         when(appConfig.getCollectedFilesPath()).thenReturn("/path/to/collected/files");
226
227         try (MockedStatic<Files> filesMockedStatic = mockStatic(Files.class)) {
228             filesMockedStatic.when(() -> Files.createDirectories(lockPath.getParent())).
229                  thenReturn(lockPath.getParent());
230
231             try (MockedStatic<Path> pathMockedStatic = mockStatic(Path.class)) {
232                 filesMockedStatic.when(() -> Files.createFile(any(Path.class))).thenReturn(lockPath);
233
234                 String name = "test.txt";
235                 String[] pathComponents = { "collectedFiles", name };
236
237                 when(fileStore.path(Arrays.toString(pathComponents))).thenReturn(mockPath);
238                 Path path = fileStore.path(Arrays.toString(pathComponents));
239                 assertEquals(mockPath, path);
240             }
241         }
242     }
243
244     @Test
245     void testCopyFileTo_Failure() {
246         // Define dummy values for testing
247         Path from = Paths.get("non-existent-file.txt");
248         String to = "destination-folder";
249
250         // Use StepVerifier to test the method
251         Mono<String> resultMono = fileStore.copyFileTo(from, to);
252
253         StepVerifier.create(resultMono).
254              expectError(IOException.class).
255              verify();
256     }
257 }