941b7770284452f20345c7e1893491ae85a6f413
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
6  * ======================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.onap.ccsdk.oran.a1policymanagementservice.datastore;
22
23 import org.junit.jupiter.api.*;
24 import org.junit.jupiter.api.extension.ExtendWith;
25 import org.mockito.junit.jupiter.MockitoExtension;
26 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
27 import org.reactivestreams.Publisher;
28 import org.springframework.boot.test.system.CapturedOutput;
29 import org.springframework.boot.test.system.OutputCaptureExtension;
30 import org.testcontainers.containers.localstack.LocalStackContainer;
31 import org.testcontainers.junit.jupiter.Container;
32 import org.testcontainers.junit.jupiter.Testcontainers;
33 import org.testcontainers.utility.DockerImageName;
34 import reactor.test.StepVerifier;
35
36 import java.nio.charset.StandardCharsets;
37 import java.util.Arrays;
38 import java.util.function.Predicate;
39
40 import static org.junit.jupiter.api.Assertions.*;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.when;
43
44 @Testcontainers
45 @ExtendWith({MockitoExtension.class, OutputCaptureExtension.class})
46 @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
47 class S3ObjectStoreTest {
48
49     static ApplicationConfig appConfig;
50     private static S3ObjectStore s3ObjectStore;
51     private static final String bucketName = "s3bucket";
52
53     @Container
54     private static final LocalStackContainer localstack =
55             new LocalStackContainer(DockerImageName.parse("localstack/localstack:0.11.3"))
56                     .withServices(LocalStackContainer.Service.S3);
57
58     @BeforeAll
59     static void init() {
60         appConfig = mock(ApplicationConfig.class);
61         when(appConfig.isS3Enabled()).thenReturn(Boolean.TRUE);
62         when(appConfig.getS3EndpointOverride()).thenReturn(localstack.getEndpoint().toString());
63         when(appConfig.getS3AccessKeyId()).thenReturn(localstack.getAccessKey());
64         when(appConfig.getS3SecretAccessKey()).thenReturn(localstack.getSecretKey());
65         when(appConfig.getS3Bucket()).thenReturn(bucketName);
66         s3ObjectStore = new S3ObjectStore(appConfig, "location");
67     }
68     @Test
69     @Order(1)
70     void testGetS3AsynchClient() {
71         assertNotNull(s3ObjectStore);
72     }
73
74     @Test
75     @Order(2)
76     void testCreateAndDeleteS3BucketSuccess(CapturedOutput capturedOutput) {
77
78         testSuccess(s3ObjectStore.createDataStore(), actual -> actual.equals(bucketName));
79         assertFalse(capturedOutput.getOut().contains("Could not create S3 bucket:"));
80
81         testSuccess(s3ObjectStore.deleteBucket(), actual -> actual.equals(bucketName));
82     }
83
84     @Test
85     @Order(3)
86     void testWriteAndReadAndDeleteObjectSuccess(CapturedOutput capturedOutput) {
87
88         testSuccess(s3ObjectStore.createDataStore(), actual -> actual.equals(bucketName));
89         byte[] fileData = "testData".getBytes(StandardCharsets.UTF_8);
90         new String(fileData);
91         testSuccess(s3ObjectStore.writeObject("test", fileData),
92                 actual -> Arrays.equals(actual, fileData));
93         assertFalse(capturedOutput.getOut().contains("Failed to store object"));
94         testSuccess(s3ObjectStore.readObject("test"),
95                 actual -> Arrays.equals(actual, fileData));
96         testSuccess(s3ObjectStore.deleteAllObjects(), actual -> actual.equals("OK"));
97     }
98
99     @Test
100     @Order(4)
101     void testListObjectsSuccess() {
102
103         s3ObjectStore.createDataStore().block();
104         String objectName = "test";
105         byte[] fileData = "testData".getBytes(StandardCharsets.UTF_8);
106         testSuccess(s3ObjectStore.writeObject(objectName, fileData),
107                 actual -> Arrays.equals(actual, fileData));
108         testSuccess(s3ObjectStore.listObjects(""), actual -> actual.equals(objectName));
109     }
110
111     @Test
112     @Order(5)
113     void testCreateAndDeleteS3BucketError(CapturedOutput capturedOutput) {
114
115         when(appConfig.getS3Bucket()).thenReturn("S3Bucket");
116
117         testFailure(s3ObjectStore.createDataStore(), actual -> actual.equals("Not Created"));
118
119         testFailure(s3ObjectStore.deleteBucket(), actual -> actual.equals("NOK"));
120         assertTrue(capturedOutput.getOut().contains("Could not delete bucket:"));
121     }
122
123     <T> void testSuccess(Publisher<T> publisher, Predicate<T> equalityCheck) {
124         StepVerifier.create(publisher)
125                 .expectNextMatches(equalityCheck)
126                 .verifyComplete();
127     }
128
129     <T> void testFailure(Publisher<T> publisher, Predicate<T> equalityCheck) {
130         StepVerifier.create(publisher)
131                 .expectNextMatches(equalityCheck)
132                 .verifyComplete();
133     }
134 }