Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for CosmosDB emulator #361

Draft
wants to merge 1 commit into
base: 2.5.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ managed-testcontainers-redis = "1.6.4"
boms-testcontainers = { module = "org.testcontainers:testcontainers-bom", version.ref = "managed-testcontainers" }

managed-testcontainers-core = { module = "org.testcontainers:testcontainers", version.ref = "managed-testcontainers" }
managed-testcontainers-azure = { module = "org.testcontainers:azure", version.ref = "managed-testcontainers" }
managed-testcontainers-elasticsearch = { module = "org.testcontainers:elasticsearch", version.ref = "managed-testcontainers" }
managed-testcontainers-jdbc = { module = "org.testcontainers:jdbc", version.ref = "managed-testcontainers" }
managed-testcontainers-hivemq = { module = "org.testcontainers:hivemq", version.ref = "managed-testcontainers" }
Expand Down
11 changes: 10 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def localstackModules = [
'sqs'
]

def azureModules = [
'cosmos'
]

include 'test-resources-bom'
include 'test-resources-build-tools'
include 'test-resources-core'
Expand Down Expand Up @@ -94,13 +98,18 @@ localstackModules.each {
project(":test-resources-localstack-$it").projectDir = file("test-resources-localstack/$projectName")
}


hibernateReactiveModules.each {
String projectName = "test-resources-hibernate-reactive-$it"
include projectName
project(":test-resources-hibernate-reactive-$it").projectDir = file("test-resources-hibernate-reactive/$projectName")
}

azureModules.each {
String projectName = "test-resources-azure-$it"
include projectName
project(":test-resources-azure-$it").projectDir = file("test-resources-azure/$projectName")
}

micronautBuild {
useStandardizedProjectNames = true
importMicronautCatalog()
Expand Down
19 changes: 19 additions & 0 deletions test-resources-azure/test-resources-azure-cosmos/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
plugins {
id 'io.micronaut.build.internal.test-resources-module'
}

description = """
Provides support for launching an Azure Cosmos test container.
"""

dependencies {
api(project(':micronaut-test-resources-core'))
api(project(':micronaut-test-resources-testcontainers'))
api(libs.managed.testcontainers.azure)

testAnnotationProcessor(mn.micronaut.inject.java)
testAnnotationProcessor(mnData.micronaut.data.document.processor)
testImplementation(project(":micronaut-test-resources-embedded"))
testImplementation(testFixtures(project(":micronaut-test-resources-testcontainers")))
testImplementation(mnData.micronaut.data.azure.cosmos)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright 2003-2021 the original author or authors.
*
* Licensed 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
*
* 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.
*/
package io.micronaut.testresources.azure.cosmos;

import io.micronaut.testresources.testcontainers.AbstractTestContainersProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.CosmosDBEmulatorContainer;
import org.testcontainers.utility.DockerImageName;

import java.io.IOException;
import java.nio.file.Files;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.time.Duration;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

public class CosmosTestResourcesProvider extends AbstractTestContainersProvider<CosmosDBEmulatorContainer> {
private static final Logger LOGGER = LoggerFactory.getLogger(CosmosTestResourcesProvider.class);
private static final Duration STARTUP_TIMEOUT = Duration.ofMinutes(5);

public static final String SIMPLE_NAME = "cosmosdb";
public static final String DEFAULT_IMAGE = "mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:latest";

public static final String ENDPOINT = "azure.cosmos.endpoint";
public static final String KEY = "azure.cosmos.key";
public static final List<String> RESOLVABLE_PROPERTIES_LIST = List.of(ENDPOINT, KEY);
public static final Set<String> RESOLVABLE_PROPERTIES_SET = Set.of(ENDPOINT, KEY);

@Override
protected String getSimpleName() {
return SIMPLE_NAME;
}

@Override
protected String getDefaultImageName() {
return DEFAULT_IMAGE;
}

@Override
public List<String> getResolvableProperties(Map<String, Collection<String>> propertyEntries, Map<String, Object> testResourcesConfig) {
return RESOLVABLE_PROPERTIES_LIST;
}

@Override
protected boolean shouldAnswer(String propertyName, Map<String, Object> requestedProperties, Map<String, Object> testResourcesConfig) {
return RESOLVABLE_PROPERTIES_SET.contains(propertyName);
}

@Override
protected CosmosDBEmulatorContainer createContainer(DockerImageName imageName, Map<String, Object> requestedProperties, Map<String, Object> testResourcesConfig) {
return new CosmosDBEmulatorWithKeystoreContainer(imageName).withStartupTimeout(STARTUP_TIMEOUT);
}

@Override
protected Optional<String> resolveProperty(String propertyName, CosmosDBEmulatorContainer container) {
if (RESOLVABLE_PROPERTIES_SET.contains(propertyName)) {
return Optional.ofNullable(switch (propertyName) {
case ENDPOINT -> container.getEmulatorEndpoint();
case KEY -> container.getEmulatorKey();
default -> null;
});
}
return Optional.empty();
}

private static class CosmosDBEmulatorWithKeystoreContainer extends CosmosDBEmulatorContainer {
public CosmosDBEmulatorWithKeystoreContainer(DockerImageName imageName) {
super(imageName);
}

@Override
public void start() {
super.start();
configureKeyStore(this);
}

private void configureKeyStore(CosmosDBEmulatorContainer emulator) {
try {
var keyStoreFile = Files.createTempFile("azure-cosmos-emulator", ".keystore");
var keyStore = emulator.buildNewKeyStore();
try (var outputStream = Files.newOutputStream(keyStoreFile)) {
keyStore.store(outputStream, emulator.getEmulatorKey().toCharArray());
}
} catch (IOException | KeyStoreException | NoSuchAlgorithmException |
CertificateException ex) {
LOGGER.error("Cannot create keystore for CosmosDB", ex);
}
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.micronaut.testresources.azure.cosmos.CosmosTestResourcesProvider
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.micronaut.testresources.azure.cosmos

import io.micronaut.test.extensions.spock.annotation.MicronautTest
import io.micronaut.testresources.testcontainers.AbstractTestContainersSpec
import jakarta.inject.Inject

@MicronautTest
class CosmosStartedTest extends AbstractTestContainersSpec {
@Inject
CosmosBookRepository repository

def "starts a CosmosDB container"() {
def book = new CosmosBook(null, "Micronaut for Spring developers", 50, "1")
repository.save(book)

when:
def books = repository.findAll()

then:
books.size() == 1
}

@Override
String getImageName() {
"cosmosdb"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2003-2021 the original author or authors.
*
* Licensed 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
*
* 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.
*/
package io.micronaut.testresources.azure.cosmos;

import io.micronaut.data.annotation.GeneratedValue;
import io.micronaut.data.annotation.Id;
import io.micronaut.data.annotation.MappedEntity;
import io.micronaut.data.cosmos.annotation.ETag;
import io.micronaut.data.cosmos.annotation.PartitionKey;

@MappedEntity("cosmosbook")
public record CosmosBook(
@Id
@GeneratedValue
@PartitionKey
String id,
String title,
int totalPages,
@ETag
String version) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2003-2021 the original author or authors.
*
* Licensed 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
*
* 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.
*/
package io.micronaut.testresources.azure.cosmos;

import io.micronaut.data.cosmos.annotation.CosmosRepository;
import io.micronaut.data.repository.CrudRepository;

@CosmosRepository
public abstract class CosmosBookRepository implements CrudRepository<CosmosBook, String> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
azure:
cosmos:
database:
packages: io.micronaut.testresources.azure.cosmos
default-gateway-mode: true
endpoint-discovery-enabled: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="info">
<appender-ref ref="STDOUT" />
</root>

</configuration>