From eba75409ee51fb78a239e02a047d103fbd99a93a Mon Sep 17 00:00:00 2001 From: Pawel Sosnowski Date: Wed, 13 Mar 2024 11:34:12 +0100 Subject: [PATCH] feat(irs-api):[#199] added tests --- .../BlobPersistenceRuntimeException.java | 30 ------------ .../persistence/MinioBlobPersistenceTest.java | 48 +++++++++++++++++++ .../persistence/PolicyPersistence.java | 3 +- .../services/PolicyStoreServiceTest.java | 5 ++ 4 files changed, 54 insertions(+), 32 deletions(-) delete mode 100644 irs-common/src/main/java/org/eclipse/tractusx/irs/common/persistence/BlobPersistenceRuntimeException.java diff --git a/irs-common/src/main/java/org/eclipse/tractusx/irs/common/persistence/BlobPersistenceRuntimeException.java b/irs-common/src/main/java/org/eclipse/tractusx/irs/common/persistence/BlobPersistenceRuntimeException.java deleted file mode 100644 index 9a30f25b51..0000000000 --- a/irs-common/src/main/java/org/eclipse/tractusx/irs/common/persistence/BlobPersistenceRuntimeException.java +++ /dev/null @@ -1,30 +0,0 @@ -/******************************************************************************** - * Copyright (c) 2022,2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - ********************************************************************************/ -package org.eclipse.tractusx.irs.common.persistence; - -/** - * Runtime Exception for everything related to BlobPersistence actions - */ -public class BlobPersistenceRuntimeException extends RuntimeException { - - public BlobPersistenceRuntimeException(final String message, final Throwable cause) { - super(message, cause); - } -} diff --git a/irs-common/src/test/java/org/eclipse/tractusx/irs/common/persistence/MinioBlobPersistenceTest.java b/irs-common/src/test/java/org/eclipse/tractusx/irs/common/persistence/MinioBlobPersistenceTest.java index e62a2ae9e6..39b9320d93 100644 --- a/irs-common/src/test/java/org/eclipse/tractusx/irs/common/persistence/MinioBlobPersistenceTest.java +++ b/irs-common/src/test/java/org/eclipse/tractusx/irs/common/persistence/MinioBlobPersistenceTest.java @@ -32,10 +32,18 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.nio.charset.StandardCharsets; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; import java.util.Collection; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.stream.Stream; @@ -43,8 +51,16 @@ import io.minio.MinioClient; import io.minio.Result; import io.minio.errors.ErrorResponseException; +import io.minio.errors.InsufficientDataException; +import io.minio.errors.InternalException; +import io.minio.errors.InvalidResponseException; +import io.minio.errors.ServerException; +import io.minio.errors.XmlParserException; +import io.minio.messages.Contents; import io.minio.messages.ErrorResponse; import io.minio.messages.Item; +import lombok.AllArgsConstructor; +import okhttp3.Headers; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -217,4 +233,36 @@ void shouldNotFindBlobByPrefix() throws Exception { assertThat(blobsByPrefix).isEmpty(); } + @Test + void test() throws BlobPersistenceException, ServerException, InsufficientDataException, ErrorResponseException, + IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, + InternalException { + final String name = "test-name"; + when(client.listObjects(any())).thenReturn(List.of(new Result<>(new TestItem(name)))); + + final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); + objectOutputStream.writeUTF(""); + InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); + ObjectInputStream outputStream = new ObjectInputStream(inputStream); + + when(client.getObject(any())).thenReturn(new GetObjectResponse(null, null, null, null, outputStream)); + + // act + final Map allBlobs = testee.getAllBlobs(); + + // assert + assertThat(allBlobs.get(name)).isNotNull(); + } + + @AllArgsConstructor + static class TestItem extends Item { + + private final String name; + + @Override + public String objectName() { + return name; + } + } } \ No newline at end of file diff --git a/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/persistence/PolicyPersistence.java b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/persistence/PolicyPersistence.java index db5744402b..9830f00825 100644 --- a/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/persistence/PolicyPersistence.java +++ b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/persistence/PolicyPersistence.java @@ -38,7 +38,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import org.eclipse.tractusx.irs.common.persistence.BlobPersistence; import org.eclipse.tractusx.irs.common.persistence.BlobPersistenceException; -import org.eclipse.tractusx.irs.common.persistence.BlobPersistenceRuntimeException; import org.eclipse.tractusx.irs.edc.client.policy.Policy; import org.eclipse.tractusx.irs.policystore.exceptions.PolicyStoreException; import org.springframework.beans.factory.annotation.Qualifier; @@ -139,7 +138,7 @@ public Map> readAll() { throw new PolicyStoreException("Could not read the policies from the store", e); } }).collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); - } catch (BlobPersistenceException | BlobPersistenceRuntimeException e) { + } catch (BlobPersistenceException e) { throw new PolicyStoreException("Could not read the policies from the store", e); } } diff --git a/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/services/PolicyStoreServiceTest.java b/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/services/PolicyStoreServiceTest.java index 4741fb3b4e..412b78d350 100644 --- a/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/services/PolicyStoreServiceTest.java +++ b/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/services/PolicyStoreServiceTest.java @@ -187,6 +187,11 @@ void deletePolicy() { verify(persistence).delete(BPN, "testId"); } + @Test + void shouldThrowException() { + + } + @Test void deletePolicyShouldThrowResponseStatusException() { // act