Skip to content

Commit

Permalink
refactor(test): introduce Azurite and Minio extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt committed Oct 21, 2024
1 parent 61e4b19 commit 05ab0f2
Show file tree
Hide file tree
Showing 11 changed files with 387 additions and 334 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2024 Cofinity-X
*
* 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.edc.tests.aws;

import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.testcontainers.containers.GenericContainer;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentials;

import java.util.UUID;

public class MinioExtension implements BeforeAllCallback, AfterAllCallback {

private final MinioContainer minioContainer = new MinioContainer();

@Override
public void beforeAll(ExtensionContext context) {
minioContainer.start();
}

@Override
public void afterAll(ExtensionContext context) {
minioContainer.stop();
}

public int getPort() {
return minioContainer.getFirstMappedPort();
}

public AwsCredentials getCredentials() {
return minioContainer.getCredentials();
}

private static class MinioContainer extends GenericContainer<MinioContainer> {

private final String accessKeyId = "test-access-key";
private final String secretAccessKey = UUID.randomUUID().toString();

public MinioContainer() {
super("bitnami/minio");
addEnv("MINIO_ROOT_USER", accessKeyId);
addEnv("MINIO_ROOT_PASSWORD", secretAccessKey);
addExposedPort(9000);
}

public AwsBasicCredentials getCredentials() {
return AwsBasicCredentials.create(accessKeyId, secretAccessKey);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
/**
* Helper class that internally uses Azure SDK classes to create containers, upload blobs, generate SAS tokens, etc.
*/
public class AzureBlobHelper {
public class AzureBlobClient {
private final String accountName;
private final String key;
private final String host;
private final int port;
private BlobServiceClient blobServiceClient;

AzureBlobHelper(String accountName, String key, String host, int port) {
AzureBlobClient(String accountName, String key, String host, int port) {
this.accountName = accountName;
this.key = key;
this.host = host;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2024 Cofinity-X
*
* 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.edc.tests.azure;

import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.testcontainers.containers.GenericContainer;

import java.util.List;

import static java.util.Arrays.stream;
import static java.util.stream.Collectors.joining;

public class AzuriteExtension implements BeforeAllCallback, AfterAllCallback {

private final AzuriteContainer azuriteContainer;

public AzuriteExtension(int azuriteHostPort, Account... accounts) {
azuriteContainer = new AzuriteContainer(azuriteHostPort, accounts);
}

@Override
public void beforeAll(ExtensionContext context) {
azuriteContainer.start();
}

@Override
public void afterAll(ExtensionContext context) {
azuriteContainer.stop();
}

public AzureBlobClient getClientFor(Account account) {
return azuriteContainer.getHelper(account);
}

public record Account(String name, String key) { }

private static class AzuriteContainer extends GenericContainer<AzuriteContainer> {

private static final String IMAGE_NAME = "mcr.microsoft.com/azure-storage/azurite";
private final int containerPort = 10_000;

public AzuriteContainer(int azuriteHostPort, Account... accounts) {
super(IMAGE_NAME);
addEnv("AZURITE_ACCOUNTS", stream(accounts).map(it -> "%s:%s".formatted(it.name(), it.key())).collect(joining(";")));
setPortBindings(List.of("%d:%d".formatted(azuriteHostPort, containerPort)));
}

public AzureBlobClient getHelper(Account account) {
return new AzureBlobClient(account.name(), account.key(), getHost(), getMappedPort(containerPort));
}

}
}
Loading

0 comments on commit 05ab0f2

Please sign in to comment.