Skip to content

Commit

Permalink
Test Azure extension with Azurite
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesnetherton committed Apr 17, 2020
1 parent 1014617 commit 2223bae
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.net.URI;
import java.nio.charset.StandardCharsets;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;
Expand All @@ -35,9 +36,8 @@

import com.microsoft.azure.storage.StorageCredentials;
import com.microsoft.azure.storage.blob.CloudBlob;
import com.microsoft.azure.storage.blob.CloudBlobClient;
import com.microsoft.azure.storage.blob.CloudBlobContainer;
import com.microsoft.azure.storage.blob.CloudBlockBlob;

import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.azure.blob.BlobBlock;
Expand All @@ -49,11 +49,19 @@ public class AzureBlobResource {
@Inject
ProducerTemplate producerTemplate;

@PostConstruct
public void init() throws Exception {
StorageCredentials credentials = StorageCredentials.tryParseCredentials(System.getProperty("azurite.credentials"));
URI uri = new URI(System.getProperty("azurite.blob.service.url") + "camel-test");
CloudBlobContainer container = new CloudBlobContainer(uri, credentials);
container.create();
}

@javax.enterprise.inject.Produces
@Named("azureBlobClient")
public CloudBlob createBlobClient() throws Exception {
StorageCredentials credentials = StorageCredentials.tryParseCredentials(System.getProperty("azurite.credentials"));
URI uri = new URI(System.getProperty("azurite.blob.service.url"));
URI uri = new URI(System.getProperty("azurite.blob.service.url") + "camel-test/test");
return new CloudBlockBlob(uri, credentials);
}

Expand All @@ -63,7 +71,7 @@ public CloudBlob createBlobClient() throws Exception {
public Response createBlob(String message) throws Exception {
BlobBlock blob = new BlobBlock(new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8)));
producerTemplate.sendBody(
"azure-blob://devstoreaccount1/camel-test/test?operation=uploadBlobBlocks&azureBlobClient=#azureBlobClient",
"azure-blob://devstoreaccount1/camel-test/test?operation=uploadBlobBlocks&azureBlobClient=#azureBlobClient&validateClientURI=false",
blob);
return Response.created(new URI("https://camel.apache.org/")).build();
}
Expand All @@ -73,7 +81,7 @@ public Response createBlob(String message) throws Exception {
@Produces(MediaType.TEXT_PLAIN)
public String readBlob() throws Exception {
return producerTemplate.requestBodyAndHeader(
"azure-blob://devstoreaccount1/camel-test/test?operation=getBlob&azureBlobClient=#azureBlobClient",
"azure-blob://devstoreaccount1/camel-test/test?operation=getBlob&azureBlobClient=#azureBlobClient&validateClientURI=false",
null, Exchange.CHARSET_NAME, StandardCharsets.UTF_8.name(), String.class);
}

Expand All @@ -82,7 +90,7 @@ public String readBlob() throws Exception {
@Consumes(MediaType.TEXT_PLAIN)
public Response updateBlob(String message) throws Exception {
producerTemplate.sendBody(
"azure-blob://devstoreaccount1/camel-test/test?operation=updateBlockBlob&azureBlobClient=#azureBlobClient",
"azure-blob://devstoreaccount1/camel-test/test?operation=updateBlockBlob&azureBlobClient=#azureBlobClient&validateClientURI=false",
message);
return Response.ok().build();
}
Expand All @@ -91,7 +99,7 @@ public Response updateBlob(String message) throws Exception {
@DELETE
public Response deleteBlob() throws Exception {
producerTemplate.sendBody(
"azure-blob://devstoreaccount1/camel-test/test?operation=deleteBlob&azureBlobClient=#azureBlobClient",
"azure-blob://devstoreaccount1/camel-test/test?operation=deleteBlob&azureBlobClient=#azureBlobClient&validateClientURI=false",
null);
return Response.noContent().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,16 @@ public class AzureQueueResource {
@Named("azureQueueClient")
public CloudQueue createQueueClient() throws Exception {
StorageCredentials credentials = StorageCredentials.tryParseCredentials(System.getProperty("azurite.credentials"));
URI uri = new URI(System.getProperty("azurite.blob.service.url"));
URI uri = new URI(System.getProperty("azurite.queue.service.url") + QUEUE_NAME);
return new CloudQueue(uri, credentials);
}

@Path("/queue/create")
@POST
public Response createQueue() throws Exception {
producerTemplate.sendBody(
"azure-queue://devstoreaccount1/" + QUEUE_NAME + "?operation=createQueue&azureQueueClient=#azureQueueClient",
"azure-queue://devstoreaccount1/" + QUEUE_NAME
+ "?operation=createQueue&azureQueueClient=#azureQueueClient&validateClientURI=false",
null);
return Response.created(new URI("https://camel.apache.org/")).build();
}
Expand All @@ -68,7 +69,7 @@ public Response createQueue() throws Exception {
public String retrieveMessage() throws Exception {
CloudQueueMessage message = producerTemplate.requestBody(
"azure-queue://devstoreaccount1/" + QUEUE_NAME
+ "?operation=retrieveMessage&azureQueueClient=#azureQueueClient",
+ "?operation=retrieveMessage&azureQueueClient=#azureQueueClient&validateClientURI=false",
null, CloudQueueMessage.class);
return message.getMessageContentAsString();
}
Expand All @@ -78,7 +79,8 @@ public String retrieveMessage() throws Exception {
@Consumes(MediaType.TEXT_PLAIN)
public Response addMessage(String message) throws Exception {
producerTemplate.sendBody(
"azure-queue://devstoreaccount1/" + QUEUE_NAME + "?operation=addMessage&azureQueueClient=#azureQueueClient",
"azure-queue://devstoreaccount1/" + QUEUE_NAME
+ "?operation=addMessage&azureQueueClient=#azureQueueClient&validateClientURI=false",
message);
return Response.created(new URI("https://camel.apache.org/")).build();
}
Expand All @@ -87,7 +89,8 @@ public Response addMessage(String message) throws Exception {
@DELETE
public Response deleteQueue() throws Exception {
producerTemplate.sendBody(
"azure-queue://devstoreaccount1/" + QUEUE_NAME + "?operation=deleteQueue&azureQueueClient=#azureQueueClient",
"azure-queue://devstoreaccount1/" + QUEUE_NAME
+ "?operation=deleteQueue&azureQueueClient=#azureQueueClient&validateClientURI=false",
null);
return Response.noContent().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@

public class AzureTestResource implements QuarkusTestResourceLifecycleManager {
private static final Logger LOGGER = LoggerFactory.getLogger(AzureTestResource.class);
private static final String AZURITE_IMAGE = "mcr.microsoft.com/azure-storage/azurite";
private static final String AZURITE_IMAGE = "mcr.microsoft.com/azure-storage/azurite:3.6.0";
private static final String AZURITE_CREDENTIALS = "DefaultEndpointsProtocol=http;AccountName="
+ "devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;"
+ "BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;";
+ "BlobEndpoint=%s;QueueEndpoint=%s;";
private static final int BLOB_SERVICE_PORT = 10000;
private static final int QUEUE_SERVICE_PORT = 10001;

Expand All @@ -43,18 +43,20 @@ public Map<String, String> start() {
try {
container = new GenericContainer<>(AZURITE_IMAGE)
.withExposedPorts(BLOB_SERVICE_PORT, QUEUE_SERVICE_PORT)
.withEnv("AZURITE_ACCOUNTS", "camel-quarkus:s3cr3t")
.withLogConsumer(new Slf4jLogConsumer(LOGGER))
.waitingFor(Wait.forListeningPort());

container.start();
//https://devstoreaccount1.blob.core.windows.net/camel-test/test

String baseServiceUrl = "http://%s:%d/devstoreaccount1/";
String blobServiceUrl = String.format(baseServiceUrl, container.getContainerIpAddress(),
container.getMappedPort(BLOB_SERVICE_PORT));
String queueServiceUrl = String.format(baseServiceUrl, container.getContainerIpAddress(),
container.getMappedPort(QUEUE_SERVICE_PORT));

Map<String, String> configuration = new HashMap<>();
configuration.put("azurite.blob.service.url", String.format("http://%s:%d/devstoreaccount1",
container.getContainerIpAddress(), container.getMappedPort(BLOB_SERVICE_PORT)));
configuration.put("azurite.queue.service.url", String.format("http://%s:%d/devstoreaccount1",
container.getContainerIpAddress(), container.getMappedPort(QUEUE_SERVICE_PORT)));
configuration.put("azurite.credentials", AZURITE_CREDENTIALS);
configuration.put("azurite.blob.service.url", blobServiceUrl);
configuration.put("azurite.queue.service.url", queueServiceUrl);
configuration.put("azurite.credentials", String.format(AZURITE_CREDENTIALS, blobServiceUrl, queueServiceUrl));
return configuration;
} catch (Exception e) {
throw new RuntimeException(e);
Expand Down

0 comments on commit 2223bae

Please sign in to comment.