Skip to content

Commit

Permalink
Test Azure extension with Azurite
Browse files Browse the repository at this point in the history
Fixes #1072
  • Loading branch information
jamesnetherton committed May 15, 2020
1 parent b2aa1e5 commit b91b101
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 52 deletions.
13 changes: 0 additions & 13 deletions integration-tests/azure/README.adoc

This file was deleted.

5 changes: 5 additions & 0 deletions integration-tests/azure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-integration-testcontainers-support</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
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;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
Expand All @@ -32,6 +34,10 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.microsoft.azure.storage.StorageCredentials;
import com.microsoft.azure.storage.blob.CloudBlob;
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 @@ -43,12 +49,30 @@ 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") + "camel-test/test");
CloudBlockBlob cloudBlockBlob = new CloudBlockBlob(uri, credentials);
return cloudBlockBlob;
}

@Path("/blob/create")
@POST
@Consumes(MediaType.TEXT_PLAIN)
public Response createBlob(String message) throws Exception {
BlobBlock blob = new BlobBlock(new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8)));
producerTemplate.sendBody("azure-blob://{{env:AZURE_STORAGE_ACCOUNT}}/camel-test/test?operation=uploadBlobBlocks",
producerTemplate.sendBody(
"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 @@ -58,23 +82,25 @@ public Response createBlob(String message) throws Exception {
@Produces(MediaType.TEXT_PLAIN)
public String readBlob() throws Exception {
return producerTemplate.requestBodyAndHeader(
"azure-blob://{{env:AZURE_STORAGE_ACCOUNT}}/camel-test/test?operation=getBlob",
"azure-blob://devstoreaccount1/camel-test/test?operation=getBlob&azureBlobClient=#azureBlobClient&validateClientURI=false",
null, Exchange.CHARSET_NAME, StandardCharsets.UTF_8.name(), String.class);
}

@Path("/blob/update")
@PATCH
@Consumes(MediaType.TEXT_PLAIN)
public Response updateBlob(String message) throws Exception {
producerTemplate.sendBody("azure-blob://{{env:AZURE_STORAGE_ACCOUNT}}/camel-test/test?operation=updateBlockBlob",
producerTemplate.sendBody(
"azure-blob://devstoreaccount1/camel-test/test?operation=updateBlockBlob&azureBlobClient=#azureBlobClient&validateClientURI=false",
message);
return Response.ok().build();
}

@Path("/blob/delete")
@DELETE
public Response deleteBlob() throws Exception {
producerTemplate.sendBody("azure-blob://{{env:AZURE_STORAGE_ACCOUNT}}/camel-test/test?operation=deleteBlob",
producerTemplate.sendBody(
"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 @@ -21,6 +21,7 @@

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
Expand All @@ -30,6 +31,8 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.microsoft.azure.storage.StorageCredentials;
import com.microsoft.azure.storage.queue.CloudQueue;
import com.microsoft.azure.storage.queue.CloudQueueMessage;
import org.apache.camel.ProducerTemplate;

Expand All @@ -42,10 +45,21 @@ public class AzureQueueResource {
@Inject
ProducerTemplate producerTemplate;

@javax.enterprise.inject.Produces
@Named("azureQueueClient")
public CloudQueue createQueueClient() throws Exception {
StorageCredentials credentials = StorageCredentials.tryParseCredentials(System.getProperty("azurite.credentials"));
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://{{env:AZURE_STORAGE_ACCOUNT}}/" + QUEUE_NAME + "?operation=createQueue", null);
producerTemplate.sendBody(
"azure-queue://devstoreaccount1/" + QUEUE_NAME
+ "?operation=createQueue&azureQueueClient=#azureQueueClient&validateClientURI=false",
null);
return Response.created(new URI("https://camel.apache.org/")).build();
}

Expand All @@ -54,7 +68,8 @@ public Response createQueue() throws Exception {
@Produces(MediaType.TEXT_PLAIN)
public String retrieveMessage() throws Exception {
CloudQueueMessage message = producerTemplate.requestBody(
"azure-queue://{{env:AZURE_STORAGE_ACCOUNT}}/" + QUEUE_NAME + "?operation=retrieveMessage",
"azure-queue://devstoreaccount1/" + QUEUE_NAME
+ "?operation=retrieveMessage&azureQueueClient=#azureQueueClient&validateClientURI=false",
null, CloudQueueMessage.class);
return message.getMessageContentAsString();
}
Expand All @@ -63,15 +78,19 @@ public String retrieveMessage() throws Exception {
@POST
@Consumes(MediaType.TEXT_PLAIN)
public Response addMessage(String message) throws Exception {
producerTemplate.sendBody("azure-queue://{{env:AZURE_STORAGE_ACCOUNT}}/" + QUEUE_NAME + "?operation=addMessage",
producerTemplate.sendBody(
"azure-queue://devstoreaccount1/" + QUEUE_NAME
+ "?operation=addMessage&azureQueueClient=#azureQueueClient&validateClientURI=false",
message);
return Response.created(new URI("https://camel.apache.org/")).build();
}

@Path("/queue/delete")
@DELETE
public Response deleteQueue() throws Exception {
producerTemplate.sendBody("azure-queue://{{env:AZURE_STORAGE_ACCOUNT}}/" + QUEUE_NAME + "?operation=deleteQueue",
producerTemplate.sendBody(
"azure-queue://devstoreaccount1/" + QUEUE_NAME
+ "?operation=deleteQueue&azureQueueClient=#azureQueueClient&validateClientURI=false",
null);
return Response.noContent().build();
}
Expand Down
25 changes: 0 additions & 25 deletions integration-tests/azure/src/main/resources/application.properties

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@
package org.apache.camel.quarkus.component.azure.it;

import io.quarkus.test.junit.NativeImageTest;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;

@NativeImageTest
@EnabledIfEnvironmentVariable(named = "AZURE_STORAGE_ACCOUNT", matches = ".+")
@EnabledIfEnvironmentVariable(named = "AZURE_STORAGE_KEY", matches = ".+")
class AzureIT extends AzureTest {

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@
*/
package org.apache.camel.quarkus.component.azure.it;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;

import static org.hamcrest.core.Is.is;

@QuarkusTest
@EnabledIfEnvironmentVariable(named = "AZURE_STORAGE_ACCOUNT", matches = ".*")
@EnabledIfEnvironmentVariable(named = "AZURE_STORAGE_KEY", matches = ".*")
@QuarkusTestResource(AzureTestResource.class)
class AzureTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://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.
*/

package org.apache.camel.quarkus.component.azure.it;

import java.util.HashMap;
import java.util.Map;

import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.containers.wait.strategy.Wait;

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:3.6.0";
private static final String AZURITE_CREDENTIALS = "DefaultEndpointsProtocol=http;AccountName="
+ "devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;"
+ "BlobEndpoint=%s;QueueEndpoint=%s;";
private static final int BLOB_SERVICE_PORT = 10000;
private static final int QUEUE_SERVICE_PORT = 10001;

private GenericContainer<?> container;

@Override
public Map<String, String> start() {
try {
container = new GenericContainer<>(AZURITE_IMAGE)
.withExposedPorts(BLOB_SERVICE_PORT, QUEUE_SERVICE_PORT)
.withLogConsumer(new Slf4jLogConsumer(LOGGER))
.waitingFor(Wait.forListeningPort());
container.start();

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", 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);
}
}

@Override
public void stop() {
try {
if (container != null) {
container.stop();
}
} catch (Exception e) {
// ignored
}
}
}

0 comments on commit b91b101

Please sign in to comment.