2  * ========================LICENSE_START=================================
 
   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
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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===================================
 
  21 package org.onap.ccsdk.oran.a1policymanagementservice.datastore;
 
  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;
 
  36 import java.nio.charset.StandardCharsets;
 
  37 import java.util.Arrays;
 
  38 import java.util.function.Predicate;
 
  40 import static org.junit.jupiter.api.Assertions.*;
 
  41 import static org.mockito.Mockito.mock;
 
  42 import static org.mockito.Mockito.when;
 
  45 @ExtendWith({MockitoExtension.class, OutputCaptureExtension.class})
 
  46 @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
 
  47 class S3ObjectStoreTest {
 
  49     static ApplicationConfig appConfig;
 
  50     private static S3ObjectStore s3ObjectStore;
 
  51     private static final String bucketName = "s3bucket";
 
  54     private static final LocalStackContainer localstack =
 
  55             new LocalStackContainer(DockerImageName.parse("localstack/localstack:0.11.3"))
 
  56                     .withServices(LocalStackContainer.Service.S3);
 
  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");
 
  70     void testGetS3AsynchClient() {
 
  71         assertNotNull(s3ObjectStore);
 
  76     void testCreateAndDeleteS3BucketSuccess(CapturedOutput capturedOutput) {
 
  78         testSuccess(s3ObjectStore.createDataStore(), actual -> actual.equals(bucketName));
 
  79         assertFalse(capturedOutput.getOut().contains("Could not create S3 bucket:"));
 
  81         testSuccess(s3ObjectStore.deleteBucket(), actual -> actual.equals(bucketName));
 
  86     void testWriteAndReadAndDeleteObjectSuccess(CapturedOutput capturedOutput) {
 
  88         testSuccess(s3ObjectStore.createDataStore(), actual -> actual.equals(bucketName));
 
  89         byte[] fileData = "testData".getBytes(StandardCharsets.UTF_8);
 
  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"));
 
 101     void testListObjectsSuccess() {
 
 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));
 
 113     void testCreateAndDeleteS3BucketError(CapturedOutput capturedOutput) {
 
 115         when(appConfig.getS3Bucket()).thenReturn("S3Bucket");
 
 117         testFailure(s3ObjectStore.createDataStore(), actual -> actual.equals("Not Created"));
 
 119         testFailure(s3ObjectStore.deleteBucket(), actual -> actual.equals("NOK"));
 
 120         assertTrue(capturedOutput.getOut().contains("Could not delete bucket:"));
 
 123     <T> void testSuccess(Publisher<T> publisher, Predicate<T> equalityCheck) {
 
 124         StepVerifier.create(publisher)
 
 125                 .expectNextMatches(equalityCheck)
 
 129     <T> void testFailure(Publisher<T> publisher, Predicate<T> equalityCheck) {
 
 130         StepVerifier.create(publisher)
 
 131                 .expectNextMatches(equalityCheck)