From 1d905cdac6be1a54486518d1bdb79eff13c77df9 Mon Sep 17 00:00:00 2001 From: Blackbaud-MikeLueders Date: Mon, 8 Mar 2021 14:03:38 -0600 Subject: [PATCH] Rework tests (#19614) * added class to centralize integration test collection management * converted a few tests touse IntegrationTestCollectionManager * added mechanism to lock a cosmos container against concurrent use * integrated cosmos lock into IntegrationTestCollectionManager * changed IntegrationTestCollectionManager package * actually acquire the lease on container creation * added headers * converted failing test to use IntegrationTestCollectionManager * removed use of Stopwatch * add support for reactive template to ContainerLock * added reactive support to IntegrationTestCollectionManager * moved inner classes to top level * converted remaining tests to use IntegrationTestCollectionManager * update locking mechanism to use separate container * wait until the test run completes to delete the created collections * use collection manager for cleanup --- ...tractIntegrationTestCollectionManager.java | 177 +++++++++++++++ .../spring/data/cosmos/ContainerLock.java | 212 ++++++++++++++++++ .../spring/data/cosmos/ContainerLockIT.java | 98 ++++++++ .../IntegrationTestCollectionManager.java | 33 +++ ...ctiveIntegrationTestCollectionManager.java | 33 +++ .../data/cosmos/core/CosmosTemplateIT.java | 26 +-- .../core/CosmosTemplatePartitionIT.java | 26 +-- .../cosmos/core/ReactiveCosmosTemplateIT.java | 26 +-- .../ReactiveCosmosTemplatePartitionIT.java | 25 +-- .../repository/MultiCosmosTemplateIT.java | 31 +-- .../integration/AddressRepositoryIT.java | 33 +-- .../integration/AnnotatedQueryIT.java | 29 +-- .../repository/integration/AuditableIT.java | 16 +- .../integration/CompositeIndexIT.java | 17 +- .../integration/ContactRepositoryIT.java | 28 +-- .../integration/CosmosAnnotationIT.java | 73 +----- .../integration/CustomerRepositoryIT.java | 28 +-- .../cosmos/repository/integration/EtagIT.java | 16 +- .../integration/IndexPolicyUpdateIT.java | 11 + .../IntegerIdDomainRepositoryIT.java | 27 +-- .../integration/LongIdDomainRepositoryIT.java | 27 +-- .../integration/MemoRepositoryIT.java | 28 +-- .../NestedPartitionKeyRepositoryIT.java | 28 +-- .../PageableAddressRepositoryIT.java | 30 +-- .../integration/PageableMemoRepositoryIT.java | 21 +- .../PageablePersonRepositoryIT.java | 19 +- .../repository/integration/PersistableIT.java | 34 +-- .../integration/ProjectRepositoryIT.java | 32 +-- .../integration/ProjectRepositorySortIT.java | 28 +-- .../integration/QuestionRepositoryIT.java | 28 +-- .../integration/ReactiveAuditableIT.java | 17 +- .../ReactiveCourseRepositoryIT.java | 32 +-- .../integration/ReactiveEtagIT.java | 16 +- .../ReactiveIndexPolicyUpdateIT.java | 11 + ...dDomainPartitionPartitionRepositoryIT.java | 35 +-- ...eactiveNestedPartitionKeyRepositoryIT.java | 28 +-- .../integration/ReactiveRoleRepositoryIT.java | 30 +-- .../ReactiveTeacherRepositoryIT.java | 29 +-- .../integration/SquareRepositoryIT.java | 28 +-- .../integration/StudentRepositoryIT.java | 28 +-- .../integration/TeacherRepositoryIT.java | 28 +-- 41 files changed, 824 insertions(+), 698 deletions(-) create mode 100644 sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/AbstractIntegrationTestCollectionManager.java create mode 100644 sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/ContainerLock.java create mode 100644 sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/ContainerLockIT.java create mode 100644 sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/IntegrationTestCollectionManager.java create mode 100644 sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/ReactiveIntegrationTestCollectionManager.java diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/AbstractIntegrationTestCollectionManager.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/AbstractIntegrationTestCollectionManager.java new file mode 100644 index 0000000000000..ca91a268026e4 --- /dev/null +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/AbstractIntegrationTestCollectionManager.java @@ -0,0 +1,177 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +package com.azure.spring.data.cosmos; + +import com.azure.cosmos.models.CosmosContainerProperties; +import com.azure.spring.data.cosmos.core.CosmosTemplate; +import com.azure.spring.data.cosmos.core.ReactiveCosmosTemplate; +import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.time.Duration; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +public abstract class AbstractIntegrationTestCollectionManager implements TestRule { + + private static final Logger LOGGER = LoggerFactory.getLogger(AbstractIntegrationTestCollectionManager.class); + private static final Duration LEASE_DURATION = Duration.ofMinutes(5); + private static final ConcurrentMap CONTAINER_CLEANUP_REGISTRY = new ConcurrentHashMap<>(); + + static { + // since collections are sometimes re-used between tests, wait until the end of the test run to delete them + Runtime.getRuntime().addShutdownHook(new Thread(AbstractIntegrationTestCollectionManager::deleteRegisteredCollections)); + } + + public static void registerContainerForCleanup(Object template, String containerName) { + DeleteContainerAction action; + if (template instanceof CosmosTemplate) { + action = new DeleteContainerAction((CosmosTemplate) template, containerName); + } else if (template instanceof ReactiveCosmosTemplate) { + action = new DeleteContainerAction((ReactiveCosmosTemplate) template, containerName); + } else { + throw new IllegalStateException("Template must be instance of CosmosTemplate or ReactiveCosmosTemplate, was " + template); + } + CONTAINER_CLEANUP_REGISTRY.putIfAbsent(containerName, action); + } + + private static void deleteRegisteredCollections() { + CONTAINER_CLEANUP_REGISTRY.values().forEach(DeleteContainerAction::deleteContainer); + } + + protected T template; + private Map containerRefs = new HashMap<>(); + private boolean isSetupDone; + + protected abstract ContainerLock createLock(CosmosEntityInformation entityInfo, Duration leaseDuration); + protected abstract CosmosContainerProperties createContainerIfNotExists(CosmosEntityInformation entityInfo); + protected abstract void deleteContainerData(CosmosEntityInformation entityInfo); + protected abstract void deleteContainer(CosmosEntityInformation entityInfo); + + public void ensureContainersCreated(T template, Class... entityTypes) { + if (!isSetupDone) { + this.template = template; + initContainerRefs(entityTypes); + isSetupDone = true; + } else { + refreshContainerLeases(); + } + } + + private void initContainerRefs(Class[] entityTypes) { + for (Class entityType : entityTypes) { + CosmosEntityInformation entityInfo = new CosmosEntityInformation(entityType); + CosmosContainerProperties properties = createContainerIfNotExists(entityInfo); + registerContainerForCleanup(template, entityInfo.getContainerName()); + ContainerLock lock = createLock(entityInfo, LEASE_DURATION); + lock.acquire(LEASE_DURATION.multipliedBy(2)); + containerRefs.put(entityType, new ContainerRefs(entityInfo, properties, lock)); + } + } + + public void ensureContainersCreatedAndEmpty(T template, Class... entityTypes) { + ensureContainersCreated(template, entityTypes); + deleteContainerData(); + } + + public CosmosEntityInformation getEntityInformation(Class entityType) { + return containerRefs.get(entityType).cosmosEntityInformation; + } + + public CosmosContainerProperties getContainerProperties(Class entityType) { + return containerRefs.get(entityType).cosmosContainerProperties; + } + + public String getContainerName(Class entityType) { + return containerRefs.get(entityType).getContainerName(); + } + + private void deleteContainerData() { + for (ContainerRefs containerRef : containerRefs.values()) { + deleteContainerData(containerRef.cosmosEntityInformation); + } + } + + private void refreshContainerLeases() { + for (ContainerRefs containerRef : containerRefs.values()) { + containerRef.lock.renew(); + } + } + + private void releaseLocks() { + for (ContainerRefs containerRef : containerRefs.values()) { + try { + containerRef.lock.release(); + } catch (Exception ex) { + LOGGER.info("Failed to delete lock for container=" + containerRef.getContainerName()); + } + } + } + + @Override + public Statement apply(Statement base, Description description) { + return new Statement() { + public void evaluate() throws Throwable { + try { + base.evaluate(); + } finally { + releaseLocks(); + } + } + }; + } + + private static class ContainerRefs { + + CosmosEntityInformation cosmosEntityInformation; + CosmosContainerProperties cosmosContainerProperties; + ContainerLock lock; + + public ContainerRefs(CosmosEntityInformation cosmosEntityInformation, CosmosContainerProperties cosmosContainerProperties, ContainerLock lock) { + this.cosmosEntityInformation = cosmosEntityInformation; + this.cosmosContainerProperties = cosmosContainerProperties; + this.lock = lock; + } + + public String getContainerName() { + return cosmosEntityInformation.getContainerName(); + } + + } + + private static class DeleteContainerAction { + + private CosmosTemplate template; + private ReactiveCosmosTemplate reactiveTemplate; + private String containerName; + + public DeleteContainerAction(CosmosTemplate template, String containerName) { + this.template = template; + this.containerName = containerName; + } + + public DeleteContainerAction(ReactiveCosmosTemplate reactiveTemplate, String containerName) { + this.reactiveTemplate = reactiveTemplate; + this.containerName = containerName; + } + + public void deleteContainer() { + try { + if (template != null) { + template.deleteContainer(containerName); + } else { + reactiveTemplate.deleteContainer(containerName); + } + } catch (Exception ex) { + LOGGER.info("Failed to delete container=" + containerName); + } + } + } + +} diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/ContainerLock.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/ContainerLock.java new file mode 100644 index 0000000000000..dc94aedeab2bd --- /dev/null +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/ContainerLock.java @@ -0,0 +1,212 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +package com.azure.spring.data.cosmos; + +import com.azure.spring.data.cosmos.core.CosmosTemplate; +import com.azure.spring.data.cosmos.core.ReactiveCosmosTemplate; +import com.azure.spring.data.cosmos.core.mapping.PartitionKey; +import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; +import com.fasterxml.jackson.annotation.JsonIgnore; +import org.springframework.data.annotation.Id; +import org.springframework.data.annotation.Version; + +import java.time.Duration; +import java.time.OffsetDateTime; + +public class ContainerLock { + + private static CosmosEntityInformation lockEntityInfo; + + private LockStore lockStore; + private Duration leaseDuration; + private String lockName; + private LockEntry acquiredLock; + + public ContainerLock(CosmosTemplate template, String lockName, Duration leaseDuration) { + this.lockStore = new NonReactiveLockStore(template); + this.lockName = lockName; + this.leaseDuration = leaseDuration; + initLockContainer(lockStore, template); + } + + public ContainerLock(ReactiveCosmosTemplate reactiveTemplate, String lockName, Duration leaseDuration) { + this.lockStore = new ReactiveLockStore(reactiveTemplate); + this.lockName = lockName; + this.leaseDuration = leaseDuration; + initLockContainer(lockStore, reactiveTemplate); + } + + private static synchronized void initLockContainer(LockStore lockStore, Object template) { + if (lockEntityInfo == null) { + CosmosEntityInformation info = new CosmosEntityInformation<>(LockEntry.class); + lockStore.createContainerIfNotExists(info); + AbstractIntegrationTestCollectionManager.registerContainerForCleanup(template, info.getContainerName()); + lockEntityInfo = info; + } + } + + public void acquire(Duration tryForDuration) { + long started = System.currentTimeMillis(); + LockEntry entry = new LockEntry(lockName, OffsetDateTime.now().plus(leaseDuration)); + while (acquiredLock == null) { + try { + acquiredLock = lockStore.insertLock(entry); + } catch (Exception ex) { + if (shouldKeepTryingToAcquire(started, tryForDuration)) { + sleep(500); + releaseIfLeaseExpired(); + } else { + throw new LockAcquisitionFailedException(tryForDuration); + } + } + } + } + + private boolean shouldKeepTryingToAcquire(long started, Duration tryForDuration) { + long elapsedDuration = System.currentTimeMillis() - started; + return elapsedDuration <= tryForDuration.toMillis(); + } + + private void sleep(long millis) { + try { + Thread.sleep(millis); + } catch (InterruptedException e) { + // ignored + } + } + + private void releaseIfLeaseExpired() { + LockEntry entry = lockStore.findActiveLock(lockName); + if (entry != null && entry.isLeaseExpired()) { + acquiredLock = entry; + release(); + } + } + + public void release() { + if (acquiredLock != null) { + lockStore.deleteLock(acquiredLock); + acquiredLock = null; + } + } + + public void renew() { + if (acquiredLock != null) { + acquiredLock.leaseExpiration = OffsetDateTime.now().plus(leaseDuration); + acquiredLock = lockStore.refreshLock(acquiredLock); + } + } + + public OffsetDateTime getLeaseExpiration() { + if (acquiredLock == null) { + return null; + } + return acquiredLock.leaseExpiration; + } + + private interface LockStore { + LockEntry insertLock(LockEntry entry); + LockEntry findActiveLock(String id); + LockEntry refreshLock(LockEntry entry); + void deleteLock(LockEntry entry); + void createContainerIfNotExists(CosmosEntityInformation entityInfo); + } + + private static class NonReactiveLockStore implements LockStore { + + private final CosmosTemplate template; + + public NonReactiveLockStore(CosmosTemplate template) { + this.template = template; + } + + @Override + public LockEntry insertLock(LockEntry entry) { + return template.insert(lockEntityInfo.getContainerName(), entry); + } + + @Override + public LockEntry findActiveLock(String id) { + return template.findById(lockEntityInfo.getContainerName(), id, LockEntry.class); + } + + @Override + public LockEntry refreshLock(LockEntry entry) { + return template.upsertAndReturnEntity(lockEntityInfo.getContainerName(), entry); + } + + @Override + public void deleteLock(LockEntry entry) { + template.deleteEntity(lockEntityInfo.getContainerName(), entry); + } + + @Override + public void createContainerIfNotExists(CosmosEntityInformation entityInfo) { + template.createContainerIfNotExists(entityInfo); + } + } + + private static class ReactiveLockStore implements LockStore { + + private final ReactiveCosmosTemplate template; + + public ReactiveLockStore(ReactiveCosmosTemplate template) { + this.template = template; + } + + @Override + public LockEntry insertLock(LockEntry entry) { + return template.insert(lockEntityInfo.getContainerName(), entry).block(); + } + + @Override + public LockEntry findActiveLock(String id) { + return template.findById(lockEntityInfo.getContainerName(), id, LockEntry.class).block(); + } + + @Override + public LockEntry refreshLock(LockEntry entry) { + return template.upsert(lockEntityInfo.getContainerName(), entry).block(); + } + + @Override + public void deleteLock(LockEntry entry) { + template.deleteEntity(lockEntityInfo.getContainerName(), entry).block(); + } + + @Override + public void createContainerIfNotExists(CosmosEntityInformation entityInfo) { + template.createContainerIfNotExists(entityInfo).block(); + } + } + + static class LockEntry { + @Id + @PartitionKey + public String id; + @Version + public String version; + public OffsetDateTime leaseExpiration; + + public LockEntry() { + } + + public LockEntry(String id, OffsetDateTime leaseExpiration) { + this.id = id; + this.leaseExpiration = leaseExpiration; + } + + @JsonIgnore + public boolean isLeaseExpired() { + return OffsetDateTime.now().isAfter(leaseExpiration); + } + + } + + static class LockAcquisitionFailedException extends RuntimeException { + public LockAcquisitionFailedException(Duration tryForDuration) { + super("Failed to acquire lock within " + tryForDuration); + } + } + +} diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/ContainerLockIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/ContainerLockIT.java new file mode 100644 index 0000000000000..f13d43f8e15db --- /dev/null +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/ContainerLockIT.java @@ -0,0 +1,98 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +package com.azure.spring.data.cosmos; + +import com.azure.spring.data.cosmos.core.CosmosTemplate; +import com.azure.spring.data.cosmos.domain.Address; +import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; +import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.time.Duration; +import java.time.OffsetDateTime; + +import static org.junit.Assert.assertTrue; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = TestRepositoryConfig.class) +public class ContainerLockIT { + + private static final Duration SHORT_LEASE_DURATION = Duration.ofSeconds(3); + + @Autowired + private CosmosTemplate template; + @Autowired + private CosmosTemplate reactiveTemplate; + private static CosmosTemplate staticTemplate; + + private ContainerLock lock; + private ContainerLock otherLock; + + @Before + public void setup() { + staticTemplate = template; + CosmosEntityInformation entityInfo = new CosmosEntityInformation(Address.class); + template.createContainerIfNotExists(entityInfo); + AbstractIntegrationTestCollectionManager.registerContainerForCleanup(template, entityInfo.getContainerName()); + lock = new ContainerLock(template, entityInfo.getContainerName(), SHORT_LEASE_DURATION); + otherLock = new ContainerLock(reactiveTemplate, entityInfo.getContainerName(), SHORT_LEASE_DURATION); + } + + @After + public void cleanup() { + releaseLockIgnoreException(lock); + releaseLockIgnoreException(otherLock); + } + + private void releaseLockIgnoreException(ContainerLock lock) { + try { + lock.release(); + } catch (Exception ex) { + // ignore + } + } + + @Test + public void shouldAcquireAndReleaseLock() { + lock.acquire(Duration.ofSeconds(1)); + lock.release(); + } + + @Test + public void acquireShouldAcquireLockIfLeaseExpires() { + OffsetDateTime start = OffsetDateTime.now(); + lock.acquire(Duration.ofSeconds(1)); + + otherLock.acquire(SHORT_LEASE_DURATION.plusSeconds(1)); + assertTrue(OffsetDateTime.now().isAfter(start.plus(SHORT_LEASE_DURATION))); + } + + @Test + public void acquireShouldThrowExceptionIfWaitForIsLessThanLeaseDuration() { + lock.acquire(Duration.ofSeconds(1)); + + try { + otherLock.acquire(SHORT_LEASE_DURATION.minusSeconds(1)); + Assert.fail(); + } catch (ContainerLock.LockAcquisitionFailedException ex) { + } + } + + @Test + public void renewShouldRenewTheLeaseDuration() { + lock.acquire(Duration.ofSeconds(1)); + OffsetDateTime originalExpiration = lock.getLeaseExpiration(); + + lock.renew(); + assertTrue(lock.getLeaseExpiration().isAfter(originalExpiration)); + } + +} diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/IntegrationTestCollectionManager.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/IntegrationTestCollectionManager.java new file mode 100644 index 0000000000000..33d6a5da89ae6 --- /dev/null +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/IntegrationTestCollectionManager.java @@ -0,0 +1,33 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +package com.azure.spring.data.cosmos; + +import com.azure.cosmos.models.CosmosContainerProperties; +import com.azure.spring.data.cosmos.core.CosmosTemplate; +import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; + +import java.time.Duration; + +public class IntegrationTestCollectionManager extends AbstractIntegrationTestCollectionManager { + + @Override + public ContainerLock createLock(CosmosEntityInformation entityInfo, Duration leaseDuration) { + return new ContainerLock(template, entityInfo.getContainerName(), leaseDuration); + } + + @Override + public CosmosContainerProperties createContainerIfNotExists(CosmosEntityInformation entityInfo) { + return template.createContainerIfNotExists(entityInfo); + } + + @Override + public void deleteContainerData(CosmosEntityInformation entityInfo) { + template.deleteAll(entityInfo.getContainerName(), entityInfo.getJavaType()); + } + + @Override + public void deleteContainer(CosmosEntityInformation entityInfo) { + template.deleteContainer(entityInfo.getContainerName()); + } + +} diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/ReactiveIntegrationTestCollectionManager.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/ReactiveIntegrationTestCollectionManager.java new file mode 100644 index 0000000000000..7c30c0fee44ee --- /dev/null +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/ReactiveIntegrationTestCollectionManager.java @@ -0,0 +1,33 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +package com.azure.spring.data.cosmos; + +import com.azure.cosmos.models.CosmosContainerProperties; +import com.azure.spring.data.cosmos.core.ReactiveCosmosTemplate; +import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; + +import java.time.Duration; + +public class ReactiveIntegrationTestCollectionManager extends AbstractIntegrationTestCollectionManager { + + @Override + public ContainerLock createLock(CosmosEntityInformation entityInfo, Duration leaseDuration) { + return new ContainerLock(template, entityInfo.getContainerName(), leaseDuration); + } + + @Override + public CosmosContainerProperties createContainerIfNotExists(CosmosEntityInformation entityInfo) { + return template.createContainerIfNotExists(entityInfo).block().getProperties(); + } + + @Override + public void deleteContainerData(CosmosEntityInformation entityInfo) { + template.deleteAll(entityInfo.getContainerName(), entityInfo.getJavaType()).block(); + } + + @Override + public void deleteContainer(CosmosEntityInformation entityInfo) { + template.deleteContainer(entityInfo.getContainerName()); + } + +} diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/core/CosmosTemplateIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/core/CosmosTemplateIT.java index 93b3d3ef3b512..3161d6614f609 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/core/CosmosTemplateIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/core/CosmosTemplateIT.java @@ -9,6 +9,7 @@ import com.azure.cosmos.models.PartitionKey; import com.azure.cosmos.models.SqlQuerySpec; import com.azure.spring.data.cosmos.CosmosFactory; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.common.PageTestUtils; import com.azure.spring.data.cosmos.common.ResponseDiagnosticsTestUtils; import com.azure.spring.data.cosmos.common.TestConstants; @@ -29,9 +30,8 @@ import com.azure.spring.data.cosmos.repository.repository.AuditableRepository; import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; import org.assertj.core.util.Lists; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -89,10 +89,12 @@ public class CosmosTemplateIT { private static final String WRONG_ETAG = "WRONG_ETAG"; + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); + private static CosmosTemplate cosmosTemplate; private static CosmosEntityInformation personInfo; private static String containerName; - private static boolean initialized; private Person insertedPerson; @@ -109,7 +111,7 @@ public class CosmosTemplateIT { @Before public void setUp() throws ClassNotFoundException { - if (!initialized) { + if (cosmosTemplate == null) { CosmosAsyncClient client = CosmosFactory.createCosmosAsyncClient(cosmosClientBuilder); final CosmosFactory cosmosFactory = new CosmosFactory(client, TestConstants.DB_NAME); @@ -123,25 +125,15 @@ public void setUp() throws ClassNotFoundException { null); cosmosTemplate = new CosmosTemplate(cosmosFactory, cosmosConfig, cosmosConverter); - cosmosTemplate.createContainerIfNotExists(personInfo); - cosmosTemplate.createContainerIfNotExists(CosmosEntityInformation.getInstance(GenIdEntity.class)); - initialized = true; } + collectionManager.ensureContainersCreatedAndEmpty(cosmosTemplate, Person.class, + GenIdEntity.class, AuditableEntity.class); + insertedPerson = cosmosTemplate.insert(Person.class.getSimpleName(), TEST_PERSON, new PartitionKey(TEST_PERSON.getLastName())); } - @After - public void cleanup() { - cosmosTemplate.deleteAll(Person.class.getSimpleName(), Person.class); - } - - @AfterClass - public static void afterClassCleanup() { - cosmosTemplate.deleteContainer(personInfo.getContainerName()); - } - private void insertPerson(Person person) { cosmosTemplate.insert(person, new PartitionKey(personInfo.getPartitionKeyFieldValue(person))); diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/core/CosmosTemplatePartitionIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/core/CosmosTemplatePartitionIT.java index c54b3d015d5bb..20828ad632f1a 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/core/CosmosTemplatePartitionIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/core/CosmosTemplatePartitionIT.java @@ -7,6 +7,7 @@ import com.azure.cosmos.CosmosClientBuilder; import com.azure.cosmos.models.PartitionKey; import com.azure.spring.data.cosmos.CosmosFactory; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.common.PageTestUtils; import com.azure.spring.data.cosmos.common.TestConstants; import com.azure.spring.data.cosmos.common.TestUtils; @@ -20,9 +21,8 @@ import com.azure.spring.data.cosmos.domain.PartitionPerson; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -65,10 +65,12 @@ public class CosmosTemplatePartitionIT { private static final PartitionPerson TEST_PERSON_2 = new PartitionPerson(ID_2, NEW_FIRST_NAME, TEST_PERSON.getZipCode(), HOBBIES, ADDRESSES); + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); + private static CosmosTemplate cosmosTemplate; private static String containerName; private static CosmosEntityInformation personInfo; - private static boolean initialized; @Autowired private ApplicationContext applicationContext; @@ -79,37 +81,25 @@ public class CosmosTemplatePartitionIT { @Before public void setUp() throws ClassNotFoundException { - if (!initialized) { + if (cosmosTemplate == null) { CosmosAsyncClient client = CosmosFactory.createCosmosAsyncClient(cosmosClientBuilder); final CosmosFactory cosmosFactory = new CosmosFactory(client, TestConstants.DB_NAME); final CosmosMappingContext mappingContext = new CosmosMappingContext(); personInfo = new CosmosEntityInformation<>(PartitionPerson.class); + containerName = personInfo.getContainerName(); mappingContext.setInitialEntitySet(new EntityScanner(this.applicationContext).scan(Persistent.class)); final MappingCosmosConverter dbConverter = new MappingCosmosConverter(mappingContext, null); cosmosTemplate = new CosmosTemplate(cosmosFactory, cosmosConfig, dbConverter); - containerName = personInfo.getContainerName(); - - cosmosTemplate.createContainerIfNotExists(personInfo); - initialized = true; } + collectionManager.ensureContainersCreatedAndEmpty(cosmosTemplate, PartitionPerson.class); cosmosTemplate.insert(PartitionPerson.class.getSimpleName(), TEST_PERSON, new PartitionKey(TEST_PERSON.getZipCode())); } - @After - public void cleanup() { - cosmosTemplate.deleteAll(personInfo.getContainerName(), PartitionPerson.class); - } - - @AfterClass - public static void afterClassCleanup() { - cosmosTemplate.deleteContainer(personInfo.getContainerName()); - } - @Test public void testFindWithPartition() { Criteria criteria = Criteria.getInstance(CriteriaType.IS_EQUAL, PROPERTY_ZIP_CODE, diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/core/ReactiveCosmosTemplateIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/core/ReactiveCosmosTemplateIT.java index f69d37f3e2f94..ff7687daf6436 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/core/ReactiveCosmosTemplateIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/core/ReactiveCosmosTemplateIT.java @@ -10,9 +10,9 @@ import com.azure.cosmos.models.PartitionKey; import com.azure.cosmos.models.SqlQuerySpec; import com.azure.spring.data.cosmos.CosmosFactory; +import com.azure.spring.data.cosmos.ReactiveIntegrationTestCollectionManager; import com.azure.spring.data.cosmos.common.ResponseDiagnosticsTestUtils; import com.azure.spring.data.cosmos.common.TestConstants; -import com.azure.spring.data.cosmos.common.TestUtils; import com.azure.spring.data.cosmos.config.CosmosConfig; import com.azure.spring.data.cosmos.core.convert.MappingCosmosConverter; import com.azure.spring.data.cosmos.core.generator.FindQuerySpecGenerator; @@ -29,9 +29,9 @@ import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; import org.assertj.core.api.Assertions; import org.junit.After; -import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -49,9 +49,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.List; import java.util.UUID; -import java.util.function.Predicate; import static com.azure.spring.data.cosmos.common.TestConstants.ADDRESSES; import static com.azure.spring.data.cosmos.common.TestConstants.AGE; @@ -61,8 +59,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; @RunWith(SpringJUnit4ClassRunner.class) @@ -87,6 +83,9 @@ public class ReactiveCosmosTemplateIT { private static final String PRECONDITION_IS_NOT_MET = "is not met"; private static final String WRONG_ETAG = "WRONG_ETAG"; + @ClassRule + public static final ReactiveIntegrationTestCollectionManager collectionManager = new ReactiveIntegrationTestCollectionManager(); + @Value("${cosmos.secondaryKey}") private String cosmosDbSecondaryKey; @@ -98,8 +97,6 @@ public class ReactiveCosmosTemplateIT { private static CosmosEntityInformation personInfo; private static AzureKeyCredential azureKeyCredential; - private static boolean initialized; - private Person insertedPerson; @Autowired @@ -115,7 +112,7 @@ public class ReactiveCosmosTemplateIT { @Before public void setUp() throws ClassNotFoundException { - if (!initialized) { + if (cosmosTemplate == null) { azureKeyCredential = new AzureKeyCredential(cosmosDbKey); cosmosClientBuilder.credential(azureKeyCredential); CosmosAsyncClient client = CosmosFactory.createCosmosAsyncClient(cosmosClientBuilder); @@ -130,11 +127,10 @@ public void setUp() throws ClassNotFoundException { final MappingCosmosConverter dbConverter = new MappingCosmosConverter(mappingContext, null); cosmosTemplate = new ReactiveCosmosTemplate(dbFactory, cosmosConfig, dbConverter); - cosmosTemplate.createContainerIfNotExists(personInfo).block(); - cosmosTemplate.createContainerIfNotExists(CosmosEntityInformation.getInstance(GenIdEntity.class)).block(); - initialized = true; } + collectionManager.ensureContainersCreatedAndEmpty(cosmosTemplate, Person.class, GenIdEntity.class, AuditableEntity.class); + insertedPerson = cosmosTemplate.insert(TEST_PERSON, new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON))).block(); } @@ -143,12 +139,6 @@ public void setUp() throws ClassNotFoundException { public void cleanup() { // Reset master key azureKeyCredential.update(cosmosDbKey); - cosmosTemplate.deleteAll(Person.class.getSimpleName(), Person.class).block(); - } - - @AfterClass - public static void afterClassCleanup() { - cosmosTemplate.deleteContainer(personInfo.getContainerName()); } @Test diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/core/ReactiveCosmosTemplatePartitionIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/core/ReactiveCosmosTemplatePartitionIT.java index 4399be7f49d43..6ea2f33d8cf6a 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/core/ReactiveCosmosTemplatePartitionIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/core/ReactiveCosmosTemplatePartitionIT.java @@ -6,6 +6,7 @@ import com.azure.cosmos.CosmosClientBuilder; import com.azure.cosmos.models.PartitionKey; import com.azure.spring.data.cosmos.CosmosFactory; +import com.azure.spring.data.cosmos.ReactiveIntegrationTestCollectionManager; import com.azure.spring.data.cosmos.common.TestConstants; import com.azure.spring.data.cosmos.config.CosmosConfig; import com.azure.spring.data.cosmos.core.convert.MappingCosmosConverter; @@ -16,10 +17,9 @@ import com.azure.spring.data.cosmos.domain.PartitionPerson; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -51,12 +51,13 @@ public class ReactiveCosmosTemplatePartitionIT { TestConstants.NEW_FIRST_NAME, TEST_PERSON.getZipCode(), TestConstants.HOBBIES, TestConstants.ADDRESSES); + @ClassRule + public static final ReactiveIntegrationTestCollectionManager collectionManager = new ReactiveIntegrationTestCollectionManager(); + private static ReactiveCosmosTemplate cosmosTemplate; private static String containerName; private static CosmosEntityInformation personInfo; - private static boolean initialized; - @Autowired private ApplicationContext applicationContext; @Autowired @@ -66,7 +67,7 @@ public class ReactiveCosmosTemplatePartitionIT { @Before public void setUp() throws ClassNotFoundException { - if (!initialized) { + if (cosmosTemplate == null) { CosmosAsyncClient client = CosmosFactory.createCosmosAsyncClient(cosmosClientBuilder); final CosmosFactory dbFactory = new CosmosFactory(client, TestConstants.DB_NAME); @@ -80,23 +81,11 @@ public void setUp() throws ClassNotFoundException { final MappingCosmosConverter dbConverter = new MappingCosmosConverter(mappingContext, null); cosmosTemplate = new ReactiveCosmosTemplate(dbFactory, cosmosConfig, dbConverter); - cosmosTemplate.createContainerIfNotExists(personInfo).block(); - - initialized = true; } + collectionManager.ensureContainersCreatedAndEmpty(cosmosTemplate, PartitionPerson.class); cosmosTemplate.insert(TEST_PERSON).block(); } - @After - public void cleanup() { - cosmosTemplate.deleteAll(PartitionPerson.class.getSimpleName(), PartitionPerson.class).block(); - } - - @AfterClass - public static void afterClassCleanup() { - cosmosTemplate.deleteContainer(personInfo.getContainerName()); - } - @Test public void testFindWithPartition() { final Criteria criteria = Criteria.getInstance(CriteriaType.IS_EQUAL, TestConstants.PROPERTY_ZIP_CODE, diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/MultiCosmosTemplateIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/MultiCosmosTemplateIT.java index ae237a75fe99b..c1cbae8bb6e0b 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/MultiCosmosTemplateIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/MultiCosmosTemplateIT.java @@ -4,15 +4,15 @@ import com.azure.cosmos.CosmosAsyncClient; import com.azure.cosmos.models.PartitionKey; +import com.azure.spring.data.cosmos.ReactiveIntegrationTestCollectionManager; import com.azure.spring.data.cosmos.common.TestConstants; import com.azure.spring.data.cosmos.core.ReactiveCosmosTemplate; import com.azure.spring.data.cosmos.domain.Person; import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; import org.apache.commons.lang3.reflect.FieldUtils; import org.assertj.core.api.Assertions; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -34,8 +34,10 @@ public class MultiCosmosTemplateIT { private static final Person SECONDARY_TEST_PERSON = new Person(TestConstants.ID_2, TestConstants.NEW_FIRST_NAME, TestConstants.NEW_LAST_NAME, TestConstants.HOBBIES, TestConstants.ADDRESSES, AGE); - private static CosmosEntityInformation personInfo; - private static boolean initialized; + + @ClassRule + public static final ReactiveIntegrationTestCollectionManager primaryCollectionManager = new ReactiveIntegrationTestCollectionManager(); + @Autowired @Qualifier("secondaryReactiveCosmosTemplate") private ReactiveCosmosTemplate secondaryReactiveCosmosTemplate; @@ -45,32 +47,15 @@ public class MultiCosmosTemplateIT { @Autowired @Qualifier("reactiveCosmosTemplate") private ReactiveCosmosTemplate primaryReactiveCosmosTemplate; - - @Before - public void setUp() throws ClassNotFoundException { - if (!initialized) { - personInfo = new CosmosEntityInformation<>(Person.class); - initialized = true; - } - } - - @After - public void cleanup() { - } - - @AfterClass - public static void afterClassCleanup() { - } + private CosmosEntityInformation personInfo = new CosmosEntityInformation<>(Person.class); @Test public void testPrimaryTemplate() { - primaryReactiveCosmosTemplate.createContainerIfNotExists(personInfo).block(); + primaryCollectionManager.ensureContainersCreatedAndEmpty(primaryReactiveCosmosTemplate, Person.class); primaryReactiveCosmosTemplate.insert(PRIMARY_TEST_PERSON, new PartitionKey(personInfo.getPartitionKeyFieldValue(PRIMARY_TEST_PERSON))).block(); final Mono findById = primaryReactiveCosmosTemplate.findById(PRIMARY_TEST_PERSON.getId(), Person.class); Assertions.assertThat(findById.block().getFirstName()).isEqualTo(TestConstants.FIRST_NAME); - primaryReactiveCosmosTemplate.deleteAll(Person.class.getSimpleName(), Person.class).block(); - primaryReactiveCosmosTemplate.deleteContainer(personInfo.getContainerName()); } @Test diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/AddressRepositoryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/AddressRepositoryIT.java index c63d54a44079c..4ce3345d79e0d 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/AddressRepositoryIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/AddressRepositoryIT.java @@ -3,18 +3,17 @@ package com.azure.spring.data.cosmos.repository.integration; import com.azure.cosmos.models.PartitionKey; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.common.TestConstants; import com.azure.spring.data.cosmos.common.TestUtils; import com.azure.spring.data.cosmos.core.CosmosTemplate; import com.azure.spring.data.cosmos.domain.Address; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.AddressRepository; -import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; import org.assertj.core.util.Lists; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -39,11 +38,8 @@ @ContextConfiguration(classes = TestRepositoryConfig.class) public class AddressRepositoryIT { - private static final CosmosEntityInformation entityInformation - = new CosmosEntityInformation<>(Address.class); - - private static CosmosTemplate staticTemplate; - private static boolean isSetupDone; + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); @Autowired AddressRepository repository; @@ -56,24 +52,9 @@ public class AddressRepositoryIT { @Before public void setUp() { - if (!isSetupDone) { - staticTemplate = template; - template.createContainerIfNotExists(entityInformation); - } - repository.save(TEST_ADDRESS1_PARTITION1); - repository.saveAll(Lists.newArrayList(TEST_ADDRESS1_PARTITION2, + collectionManager.ensureContainersCreatedAndEmpty(template, Address.class); + repository.saveAll(Lists.newArrayList(TEST_ADDRESS1_PARTITION1, TEST_ADDRESS1_PARTITION2, TEST_ADDRESS2_PARTITION1, TEST_ADDRESS4_PARTITION3)); - isSetupDone = true; - } - - @After - public void cleanup() { - repository.deleteAll(); - } - - @AfterClass - public static void afterClassCleanup() { - staticTemplate.deleteContainer(entityInformation.getContainerName()); } @Test @@ -87,7 +68,7 @@ public void testFindAll() { @Test public void testFindByIdWithPartitionKey() { final Optional
addressById = repository.findById(TEST_ADDRESS1_PARTITION1.getPostalCode(), - new PartitionKey(entityInformation.getPartitionKeyFieldValue(TEST_ADDRESS1_PARTITION1))); + new PartitionKey(collectionManager.getEntityInformation(Address.class).getPartitionKeyFieldValue(TEST_ADDRESS1_PARTITION1))); if (!addressById.isPresent()) { fail("address not found"); diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/AnnotatedQueryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/AnnotatedQueryIT.java index a9c832dc7d33c..a343c5e2f8eb2 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/AnnotatedQueryIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/AnnotatedQueryIT.java @@ -2,6 +2,7 @@ // Licensed under the MIT License. package com.azure.spring.data.cosmos.repository.integration; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.common.TestConstants; import com.azure.spring.data.cosmos.core.CosmosTemplate; import com.azure.spring.data.cosmos.core.query.CosmosPageRequest; @@ -10,10 +11,8 @@ import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.AddressRepository; import com.azure.spring.data.cosmos.repository.repository.AuditableRepository; -import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -37,11 +36,8 @@ @ContextConfiguration(classes = TestRepositoryConfig.class) public class AnnotatedQueryIT { - private static final CosmosEntityInformation entityInformation = - new CosmosEntityInformation<>(Address.class); - - private static CosmosTemplate staticTemplate; - private static boolean isSetupDone; + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); @Autowired private CosmosTemplate template; @@ -52,24 +48,9 @@ public class AnnotatedQueryIT { @Autowired private AddressRepository addressRepository; - @AfterClass - public static void afterClassCleanup() { - staticTemplate.deleteContainer(entityInformation.getContainerName()); - } - @Before public void setUp() { - if (!isSetupDone) { - staticTemplate = template; - template.createContainerIfNotExists(entityInformation); - } - isSetupDone = true; - } - - @After - public void cleanup() { - auditableRepository.deleteAll(); - addressRepository.deleteAll(); + collectionManager.ensureContainersCreatedAndEmpty(template, Address.class, AuditableEntity.class); } @Test diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/AuditableIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/AuditableIT.java index c56ad9954a519..d674e783f1ebd 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/AuditableIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/AuditableIT.java @@ -2,6 +2,8 @@ // Licensed under the MIT License. package com.azure.spring.data.cosmos.repository.integration; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; +import com.azure.spring.data.cosmos.core.CosmosTemplate; import com.azure.spring.data.cosmos.domain.AuditableEntity; import com.azure.spring.data.cosmos.domain.AuditableIdGeneratedEntity; import com.azure.spring.data.cosmos.repository.StubAuditorProvider; @@ -9,7 +11,8 @@ import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.AuditableIdGeneratedRepository; import com.azure.spring.data.cosmos.repository.repository.AuditableRepository; -import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -26,6 +29,11 @@ @ContextConfiguration(classes = TestRepositoryConfig.class) public class AuditableIT { + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); + + @Autowired + private CosmosTemplate cosmosTemplate; @Autowired private AuditableRepository auditableRepository; @Autowired @@ -35,9 +43,9 @@ public class AuditableIT { @Autowired private StubAuditorProvider stubAuditorProvider; - @After - public void cleanup() { - this.auditableRepository.deleteAll(); + @Before + public void setup() { + collectionManager.ensureContainersCreatedAndEmpty(cosmosTemplate, AuditableEntity.class, AuditableIdGeneratedEntity.class); } @Test diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/CompositeIndexIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/CompositeIndexIT.java index f333b290b81bb..9ce99c5625112 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/CompositeIndexIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/CompositeIndexIT.java @@ -6,18 +6,17 @@ import com.azure.cosmos.models.CompositePath; import com.azure.cosmos.models.CompositePathSortOrder; import com.azure.cosmos.models.CosmosContainerProperties; -import com.azure.cosmos.models.ExcludedPath; -import com.azure.cosmos.models.IncludedPath; import com.azure.cosmos.models.IndexingPolicy; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.core.CosmosTemplate; import com.azure.spring.data.cosmos.core.ReactiveCosmosTemplate; -import com.azure.spring.data.cosmos.domain.ComplexIndexPolicyEntity; import com.azure.spring.data.cosmos.domain.CompositeIndexEntity; -import com.azure.spring.data.cosmos.domain.IndexPolicyEntity; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; import com.azure.spring.data.cosmos.repository.support.SimpleCosmosRepository; import com.azure.spring.data.cosmos.repository.support.SimpleReactiveCosmosRepository; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; @@ -26,16 +25,17 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.in; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = TestRepositoryConfig.class) public class CompositeIndexIT { + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); + @Autowired CosmosTemplate template; @@ -44,6 +44,11 @@ public class CompositeIndexIT { CosmosEntityInformation information = new CosmosEntityInformation<>(CompositeIndexEntity.class); + @Before + public void setup() { + collectionManager.ensureContainersCreatedAndEmpty(template, CompositeIndexEntity.class); + } + @Test public void canSetCompositeIndex() { new SimpleCosmosRepository<>(information, template); diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ContactRepositoryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ContactRepositoryIT.java index 450010c2417d3..67fd40e5234b5 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ContactRepositoryIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ContactRepositoryIT.java @@ -2,19 +2,18 @@ // Licensed under the MIT License. package com.azure.spring.data.cosmos.repository.integration; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.common.TestUtils; import com.azure.spring.data.cosmos.core.CosmosTemplate; import com.azure.spring.data.cosmos.domain.Contact; import com.azure.spring.data.cosmos.exception.CosmosAccessException; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.ContactRepository; -import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; import com.fasterxml.jackson.databind.node.ObjectNode; import org.assertj.core.util.Lists; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; @@ -41,11 +40,8 @@ public class ContactRepositoryIT { private static final Contact TEST_CONTACT4 = new Contact("testId4", "faketitle4", 43, true); private static final Contact TEST_CONTACT5 = new Contact("testId5", "faketitle3", 43, true); - private static final CosmosEntityInformation entityInformation = - new CosmosEntityInformation<>(Contact.class); - - private static CosmosTemplate staticTemplate; - private static boolean isSetupDone; + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); @Autowired ContactRepository repository; @@ -53,28 +49,14 @@ public class ContactRepositoryIT { @Autowired private CosmosTemplate template; - @AfterClass - public static void afterClassCleanup() { - staticTemplate.deleteContainer(entityInformation.getContainerName()); - } - @Before public void setUp() { - if (!isSetupDone) { - staticTemplate = template; - template.createContainerIfNotExists(entityInformation); - } + collectionManager.ensureContainersCreatedAndEmpty(template, Contact.class); repository.save(TEST_CONTACT1); repository.save(TEST_CONTACT2); repository.save(TEST_CONTACT3); repository.save(TEST_CONTACT4); repository.save(TEST_CONTACT5); - isSetupDone = true; - } - - @After - public void cleanup() { - repository.deleteAll(); } @Test diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/CosmosAnnotationIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/CosmosAnnotationIT.java index 1bcd1d84a9ac1..a620efe1c5e14 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/CosmosAnnotationIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/CosmosAnnotationIT.java @@ -2,34 +2,23 @@ // Licensed under the MIT License. package com.azure.spring.data.cosmos.repository.integration; -import com.azure.cosmos.CosmosAsyncClient; -import com.azure.cosmos.CosmosClientBuilder; -import com.azure.cosmos.models.CosmosContainerProperties; import com.azure.cosmos.models.ExcludedPath; import com.azure.cosmos.models.IncludedPath; import com.azure.cosmos.models.IndexingPolicy; import com.azure.cosmos.models.PartitionKey; -import com.azure.spring.data.cosmos.CosmosFactory; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.common.TestConstants; import com.azure.spring.data.cosmos.common.TestUtils; -import com.azure.spring.data.cosmos.config.CosmosConfig; import com.azure.spring.data.cosmos.core.CosmosTemplate; -import com.azure.spring.data.cosmos.core.convert.MappingCosmosConverter; -import com.azure.spring.data.cosmos.core.mapping.CosmosMappingContext; import com.azure.spring.data.cosmos.domain.Role; import com.azure.spring.data.cosmos.domain.TimeToLiveSample; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.ReactiveRoleRepository; -import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.domain.EntityScanner; -import org.springframework.context.ApplicationContext; -import org.springframework.data.annotation.Persistent; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.util.Assert; @@ -53,58 +42,20 @@ public class CosmosAnnotationIT { private static final Role TEST_ROLE_3 = new Role(TestConstants.ID_3, true, TestConstants.LEVEL, TestConstants.ROLE_NAME); + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); + @Autowired - private ApplicationContext applicationContext; - @Autowired - private CosmosConfig cosmosConfig; - @Autowired - private CosmosClientBuilder cosmosClientBuilder; + private CosmosTemplate cosmosTemplate; @Autowired private ReactiveRoleRepository repository; - private static CosmosTemplate cosmosTemplate; - private static CosmosContainerProperties collectionRole; - private static CosmosContainerProperties collectionSample; - private static CosmosEntityInformation roleInfo; - private static CosmosEntityInformation sampleInfo; - - private static boolean initialized; - @Before public void setUp() throws ClassNotFoundException { - if (!initialized) { - CosmosAsyncClient client = CosmosFactory.createCosmosAsyncClient(cosmosClientBuilder); - final CosmosFactory cosmosFactory = new CosmosFactory(client, TestConstants.DB_NAME); - - roleInfo = new CosmosEntityInformation<>(Role.class); - sampleInfo = new CosmosEntityInformation<>(TimeToLiveSample.class); - final CosmosMappingContext mappingContext = new CosmosMappingContext(); - - mappingContext.setInitialEntitySet(new EntityScanner(this.applicationContext).scan(Persistent.class)); - - final MappingCosmosConverter mappingConverter = new MappingCosmosConverter(mappingContext, null); - - cosmosTemplate = new CosmosTemplate(cosmosFactory, cosmosConfig, mappingConverter); - collectionRole = cosmosTemplate.createContainerIfNotExists(roleInfo); - collectionSample = cosmosTemplate.createContainerIfNotExists(sampleInfo); - - initialized = true; - } - + collectionManager.ensureContainersCreatedAndEmpty(cosmosTemplate, Role.class, TimeToLiveSample.class); repository.saveAll(Arrays.asList(TEST_ROLE_1, TEST_ROLE_2, TEST_ROLE_3)).collectList().block(); } - @After - public void cleanUp() { - repository.deleteAll().block(); - } - - @AfterClass - public static void afterClassCleanup() { - cosmosTemplate.deleteContainer(roleInfo.getContainerName()); - cosmosTemplate.deleteContainer(sampleInfo.getContainerName()); - } - @Test public void testFindAll() { Flux findAll = repository.findAll(); @@ -157,16 +108,16 @@ public void testDeleteByIdAndPartitionKey() { @Test public void testTimeToLiveAnnotation() { - Integer timeToLive = sampleInfo.getTimeToLive(); - assertThat(timeToLive).isEqualTo(collectionSample.getDefaultTimeToLiveInSeconds()); + Integer timeToLive = collectionManager.getEntityInformation(Role.class).getTimeToLive(); + assertThat(timeToLive).isEqualTo(collectionManager.getContainerProperties(Role.class).getDefaultTimeToLiveInSeconds()); - timeToLive = roleInfo.getTimeToLive(); - assertThat(timeToLive).isEqualTo(collectionRole.getDefaultTimeToLiveInSeconds()); + timeToLive = collectionManager.getEntityInformation(TimeToLiveSample.class).getTimeToLive(); + assertThat(timeToLive).isEqualTo(collectionManager.getContainerProperties(TimeToLiveSample.class).getDefaultTimeToLiveInSeconds()); } @Test public void testIndexingPolicyAnnotation() { - final IndexingPolicy policy = collectionRole.getIndexingPolicy(); + final IndexingPolicy policy = collectionManager.getContainerProperties(Role.class).getIndexingPolicy(); Assert.isTrue(policy.getIndexingMode() == TestConstants.INDEXING_POLICY_MODE, "unmatched collection policy indexing mode of class Role"); diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/CustomerRepositoryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/CustomerRepositoryIT.java index 1b3608a7a0528..3128d79651255 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/CustomerRepositoryIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/CustomerRepositoryIT.java @@ -2,15 +2,14 @@ // Licensed under the MIT License. package com.azure.spring.data.cosmos.repository.integration; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.core.CosmosTemplate; import com.azure.spring.data.cosmos.domain.Customer; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.CustomerRepository; -import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -49,11 +48,8 @@ public class CustomerRepositoryIT { private static final Customer CUSTOMER_1 = new Customer(CUSTOMER_ID_1, CUSTOMER_LEVEL_1, USER_1); private static final Customer CUSTOMER_2 = new Customer(CUSTOMER_ID_2, CUSTOMER_LEVEL_1, USER_2); - private static final CosmosEntityInformation entityInformation = - new CosmosEntityInformation<>(Customer.class); - - private static CosmosTemplate staticTemplate; - private static boolean isSetupDone; + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); @Autowired private CustomerRepository repository; @@ -63,22 +59,8 @@ public class CustomerRepositoryIT { @Before public void setUp() { - if (!isSetupDone) { - staticTemplate = template; - template.createContainerIfNotExists(entityInformation); - } + collectionManager.ensureContainersCreatedAndEmpty(template, Customer.class); this.repository.saveAll(Arrays.asList(CUSTOMER_0, CUSTOMER_1, CUSTOMER_2)); - isSetupDone = true; - } - - @After - public void cleanup() { - this.repository.deleteAll(); - } - - @AfterClass - public static void afterClassCleanup() { - staticTemplate.deleteContainer(entityInformation.getContainerName()); } private void assertCustomerListEquals(@NonNull List customers, @NonNull List reference) { diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/EtagIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/EtagIT.java index 67db3832b7e30..2c218500ad37c 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/EtagIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/EtagIT.java @@ -3,12 +3,15 @@ package com.azure.spring.data.cosmos.repository.integration; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; +import com.azure.spring.data.cosmos.core.CosmosTemplate; import com.azure.spring.data.cosmos.domain.PersonWithEtag; import com.azure.spring.data.cosmos.exception.CosmosAccessException; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.PersonWithEtagRepository; -import org.junit.After; import org.junit.Assert; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -31,12 +34,17 @@ @ContextConfiguration(classes = TestRepositoryConfig.class) public class EtagIT { + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); + + @Autowired + CosmosTemplate template; @Autowired PersonWithEtagRepository personWithEtagRepository; - @After - public void cleanup() { - personWithEtagRepository.deleteAll(); + @Before + public void setup() { + collectionManager.ensureContainersCreatedAndEmpty(template, PersonWithEtag.class); } private static PersonWithEtag createPersonWithEtag() { diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/IndexPolicyUpdateIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/IndexPolicyUpdateIT.java index 0d963034e521d..6483ab90602d4 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/IndexPolicyUpdateIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/IndexPolicyUpdateIT.java @@ -7,12 +7,15 @@ import com.azure.cosmos.models.IncludedPath; import com.azure.cosmos.models.IndexingMode; import com.azure.cosmos.models.IndexingPolicy; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.core.CosmosTemplate; import com.azure.spring.data.cosmos.domain.ComplexIndexPolicyEntity; import com.azure.spring.data.cosmos.domain.IndexPolicyEntity; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; import com.azure.spring.data.cosmos.repository.support.SimpleCosmosRepository; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; @@ -29,6 +32,9 @@ @ContextConfiguration(classes = TestRepositoryConfig.class) public class IndexPolicyUpdateIT { + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); + @Autowired CosmosTemplate template; @@ -39,6 +45,11 @@ public class IndexPolicyUpdateIT { CosmosEntityInformation complexIndexPolicyEntityInformation = new CosmosEntityInformation<>(ComplexIndexPolicyEntity.class); + @Before + public void setup() { + collectionManager.ensureContainersCreatedAndEmpty(template, IndexPolicyEntity.class, ComplexIndexPolicyEntity.class); + } + @Test public void testIndexPolicyUpdatesOnRepoInitialization() { // set index policy from entity annotation diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/IntegerIdDomainRepositoryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/IntegerIdDomainRepositoryIT.java index 66c4f357f171e..b8d7eeee97127 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/IntegerIdDomainRepositoryIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/IntegerIdDomainRepositoryIT.java @@ -2,6 +2,7 @@ // Licensed under the MIT License. package com.azure.spring.data.cosmos.repository.integration; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.core.CosmosTemplate; import com.azure.spring.data.cosmos.core.query.CosmosPageRequest; import com.azure.spring.data.cosmos.domain.IntegerIdDomain; @@ -9,10 +10,9 @@ import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.IntegerIdDomainRepository; import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -38,11 +38,8 @@ public class IntegerIdDomainRepositoryIT { private static final String NAME = "panli"; private static final IntegerIdDomain DOMAIN = new IntegerIdDomain(ID, NAME); - private static final CosmosEntityInformation entityInformation = - new CosmosEntityInformation<>(IntegerIdDomain.class); - - private static CosmosTemplate staticTemplate; - private static boolean isSetupDone; + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); @Autowired private CosmosTemplate template; @@ -52,22 +49,8 @@ public class IntegerIdDomainRepositoryIT { @Before public void setUp() { - if (!isSetupDone) { - staticTemplate = template; - template.createContainerIfNotExists(entityInformation); - } + collectionManager.ensureContainersCreatedAndEmpty(template, IntegerIdDomain.class); this.repository.save(DOMAIN); - isSetupDone = true; - } - - @After - public void cleanup() { - this.repository.deleteAll(); - } - - @AfterClass - public static void afterClassCleanup() { - staticTemplate.deleteContainer(entityInformation.getContainerName()); } @Test diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/LongIdDomainRepositoryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/LongIdDomainRepositoryIT.java index dd28ed846305b..b6d48db3c0f83 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/LongIdDomainRepositoryIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/LongIdDomainRepositoryIT.java @@ -2,6 +2,7 @@ // Licensed under the MIT License. package com.azure.spring.data.cosmos.repository.integration; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.common.TestUtils; import com.azure.spring.data.cosmos.core.CosmosTemplate; import com.azure.spring.data.cosmos.core.query.CosmosPageRequest; @@ -10,10 +11,9 @@ import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.LongIdDomainRepository; import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -47,11 +47,8 @@ public class LongIdDomainRepositoryIT { private static final LongIdDomain DOMAIN_1 = new LongIdDomain(ID_1, NAME_1); private static final LongIdDomain DOMAIN_2 = new LongIdDomain(ID_2, NAME_2); - private static final CosmosEntityInformation entityInformation = - new CosmosEntityInformation<>(LongIdDomain.class); - - private static CosmosTemplate staticTemplate; - private static boolean isSetupDone; + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); @Autowired private CosmosTemplate template; @@ -61,23 +58,9 @@ public class LongIdDomainRepositoryIT { @Before public void setUp() { - if (!isSetupDone) { - staticTemplate = template; - template.createContainerIfNotExists(entityInformation); - } + collectionManager.ensureContainersCreatedAndEmpty(template, LongIdDomain.class); this.repository.save(DOMAIN_1); this.repository.save(DOMAIN_2); - isSetupDone = true; - } - - @After - public void cleanup() { - this.repository.deleteAll(); - } - - @AfterClass - public static void afterClassCleanup() { - staticTemplate.deleteContainer(entityInformation.getContainerName()); } @Test diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/MemoRepositoryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/MemoRepositoryIT.java index 6a4efe7ebede5..a032499c99795 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/MemoRepositoryIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/MemoRepositoryIT.java @@ -2,6 +2,7 @@ // Licensed under the MIT License. package com.azure.spring.data.cosmos.repository.integration; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.common.TestConstants; import com.azure.spring.data.cosmos.common.TestUtils; import com.azure.spring.data.cosmos.core.CosmosTemplate; @@ -10,12 +11,10 @@ import com.azure.spring.data.cosmos.exception.CosmosAccessException; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.MemoRepository; -import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; +import org.junit.ClassRule; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; @@ -48,11 +47,8 @@ public class MemoRepositoryIT { private static Memo testMemo2; private static Memo testMemo3; - private static final CosmosEntityInformation entityInformation = - new CosmosEntityInformation<>(Memo.class); - - private static CosmosTemplate staticTemplate; - private static boolean isSetupDone; + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); @Autowired private CosmosTemplate template; @@ -74,22 +70,8 @@ public static void init() throws ParseException { @Before public void setUp() { - if (!isSetupDone) { - staticTemplate = template; - template.createContainerIfNotExists(entityInformation); - } + collectionManager.ensureContainersCreatedAndEmpty(template, Memo.class); repository.saveAll(Arrays.asList(testMemo1, testMemo2, testMemo3)); - isSetupDone = true; - } - - @After - public void cleanup() { - repository.deleteAll(); - } - - @AfterClass - public static void afterClassCleanup() { - staticTemplate.deleteContainer(entityInformation.getContainerName()); } @Test diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/NestedPartitionKeyRepositoryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/NestedPartitionKeyRepositoryIT.java index 47fd13b4f021a..5e2515c27cef7 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/NestedPartitionKeyRepositoryIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/NestedPartitionKeyRepositoryIT.java @@ -3,17 +3,16 @@ package com.azure.spring.data.cosmos.repository.integration; import com.azure.cosmos.models.PartitionKey; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.common.TestUtils; import com.azure.spring.data.cosmos.core.CosmosTemplate; import com.azure.spring.data.cosmos.domain.NestedEntity; import com.azure.spring.data.cosmos.domain.NestedPartitionKeyEntityWithGeneratedValue; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.NestedPartitionKeyRepository; -import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -36,11 +35,8 @@ public class NestedPartitionKeyRepositoryIT { private static final NestedPartitionKeyEntityWithGeneratedValue NESTED_ENTITY_2 = new NestedPartitionKeyEntityWithGeneratedValue(null, new NestedEntity("partitionKey2")); - private static final CosmosEntityInformation entityInformation = - new CosmosEntityInformation<>(NestedPartitionKeyEntityWithGeneratedValue.class); - - private static CosmosTemplate staticTemplate; - private static boolean isSetupDone; + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); @Autowired private CosmosTemplate template; @@ -53,21 +49,7 @@ public static void init() { } @Before public void setUp() { - if (!isSetupDone) { - staticTemplate = template; - template.createContainerIfNotExists(entityInformation); - } - isSetupDone = true; - } - - @After - public void cleanup() { - repository.deleteAll(); - } - - @AfterClass - public static void afterClassCleanup() { - staticTemplate.deleteContainer(entityInformation.getContainerName()); + collectionManager.ensureContainersCreatedAndEmpty(template, NestedPartitionKeyEntityWithGeneratedValue.class); } @Test diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/PageableAddressRepositoryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/PageableAddressRepositoryIT.java index d637fad3c8f17..3abb880dc6134 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/PageableAddressRepositoryIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/PageableAddressRepositoryIT.java @@ -6,6 +6,7 @@ import com.azure.cosmos.models.CosmosQueryRequestOptions; import com.azure.cosmos.models.FeedResponse; import com.azure.spring.data.cosmos.CosmosFactory; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.common.TestConstants; import com.azure.spring.data.cosmos.common.TestUtils; import com.azure.spring.data.cosmos.core.CosmosTemplate; @@ -13,10 +14,8 @@ import com.azure.spring.data.cosmos.domain.Address; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.PageableAddressRepository; -import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -44,11 +43,8 @@ @ContextConfiguration(classes = TestRepositoryConfig.class) public class PageableAddressRepositoryIT { - private static final CosmosEntityInformation entityInformation = - new CosmosEntityInformation<>(Address.class); - - private static CosmosTemplate staticTemplate; - private static boolean isSetupDone; + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); @Autowired private CosmosTemplate template; @@ -64,26 +60,12 @@ public class PageableAddressRepositoryIT { @Before public void setUp() { - if (!isSetupDone) { - staticTemplate = template; - template.createContainerIfNotExists(entityInformation); - } + collectionManager.ensureContainersCreatedAndEmpty(template, Address.class); repository.save(TEST_ADDRESS1_PARTITION1); repository.save(TEST_ADDRESS1_PARTITION2); repository.save(TEST_ADDRESS2_PARTITION1); repository.save(TEST_ADDRESS4_PARTITION3); repository.save(new Address(TestConstants.POSTAL_CODE, TestConstants.STREET, TestConstants.CITY_0)); - isSetupDone = true; - } - - @After - public void cleanup() { - repository.deleteAll(); - } - - @AfterClass - public static void afterClassCleanup() { - staticTemplate.deleteContainer(entityInformation.getContainerName()); } @Test @@ -171,7 +153,7 @@ public void testOffsetAndLimit() { final CosmosAsyncClient cosmosAsyncClient = applicationContext.getBean(CosmosAsyncClient.class); final Flux> feedResponseFlux = cosmosAsyncClient.getDatabase(cosmosFactory.getDatabaseName()) - .getContainer(entityInformation.getContainerName()) + .getContainer(collectionManager.getContainerName(Address.class)) .queryItems(query, options, Address.class) .byPage(); diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/PageableMemoRepositoryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/PageableMemoRepositoryIT.java index b60b3775daec7..2dada716bd098 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/PageableMemoRepositoryIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/PageableMemoRepositoryIT.java @@ -6,15 +6,15 @@ import com.azure.cosmos.models.CosmosQueryRequestOptions; import com.azure.cosmos.models.FeedResponse; import com.azure.spring.data.cosmos.CosmosFactory; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.core.CosmosTemplate; import com.azure.spring.data.cosmos.core.query.CosmosPageRequest; import com.azure.spring.data.cosmos.domain.Importance; import com.azure.spring.data.cosmos.domain.PageableMemo; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.PageableMemoRepository; -import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; -import org.junit.AfterClass; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -42,10 +42,8 @@ public class PageableMemoRepositoryIT { private static final int TOTAL_CONTENT_SIZE = 500; - private static final CosmosEntityInformation entityInformation = - new CosmosEntityInformation<>(PageableMemo.class); - - private static CosmosTemplate staticTemplate; + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); @Autowired private CosmosTemplate template; @@ -65,11 +63,11 @@ public class PageableMemoRepositoryIT { @Before public void setUp() { + collectionManager.ensureContainersCreated(template, PageableMemo.class); + if (isSetupDone) { return; } - template.createContainerIfNotExists(entityInformation); - staticTemplate = template; memoSet = new HashSet<>(); final Random random = new Random(); final Importance[] importanceValues = Importance.values(); @@ -86,11 +84,6 @@ public void setUp() { isSetupDone = true; } - @AfterClass - public static void afterClassCleanup() { - staticTemplate.deleteContainer(entityInformation.getContainerName()); - } - @Test public void testFindAllWithPageSizeLessThanReturned() { final Set memos = findAllWithPageSize(20); @@ -142,7 +135,7 @@ private Flux> getItemsWithOffsetAndLimit(int skipCoun final CosmosAsyncClient cosmosAsyncClient = applicationContext.getBean(CosmosAsyncClient.class); return cosmosAsyncClient.getDatabase(cosmosFactory.getDatabaseName()) - .getContainer(entityInformation.getContainerName()) + .getContainer(collectionManager.getContainerName(PageableMemo.class)) .queryItems(query, options, PageableMemo.class) .byPage(); } diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/PageablePersonRepositoryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/PageablePersonRepositoryIT.java index 8af3be8775014..72bfce2ba51b1 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/PageablePersonRepositoryIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/PageablePersonRepositoryIT.java @@ -2,16 +2,16 @@ // Licensed under the MIT License. package com.azure.spring.data.cosmos.repository.integration; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.core.CosmosTemplate; import com.azure.spring.data.cosmos.core.query.CosmosPageRequest; import com.azure.spring.data.cosmos.domain.Address; import com.azure.spring.data.cosmos.domain.PageablePerson; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.PageablePersonRepository; -import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; import org.apache.commons.lang3.StringUtils; -import org.junit.AfterClass; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -35,10 +35,8 @@ public class PageablePersonRepositoryIT { private static final int TOTAL_CONTENT_SIZE = 25; public static final int ONE_KB = 1024; - private static final CosmosEntityInformation entityInformation = - new CosmosEntityInformation<>(PageablePerson.class); - - private static CosmosTemplate staticTemplate; + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); @Autowired private CosmosTemplate template; @@ -52,12 +50,12 @@ public class PageablePersonRepositoryIT { @Before public void setUp() { + collectionManager.ensureContainersCreated(template, PageablePerson.class); + if (isSetupDone) { return; } personSet = new HashSet<>(); - template.createContainerIfNotExists(entityInformation); - staticTemplate = template; // Create larger documents with size more than 10 kb for (int i = 0; i < TOTAL_CONTENT_SIZE; i++) { @@ -77,11 +75,6 @@ public void setUp() { isSetupDone = true; } - @AfterClass - public static void afterClassCleanup() { - staticTemplate.deleteContainer(entityInformation.getContainerName()); - } - // Cosmos DB can return any number of documents less than or equal to requested page size // Because of available RUs, the number of return documents vary. // With documents more than 10 KB, and collection RUs 400, diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/PersistableIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/PersistableIT.java index 71132e617d5a5..2bf11ff8779e7 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/PersistableIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/PersistableIT.java @@ -3,16 +3,15 @@ package com.azure.spring.data.cosmos.repository.integration; import com.azure.cosmos.implementation.ConflictException; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.core.CosmosTemplate; import com.azure.spring.data.cosmos.domain.PersistableEntity; import com.azure.spring.data.cosmos.exception.CosmosAccessException; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.PersistableEntityRepository; import com.azure.spring.data.cosmos.repository.repository.ReactivePersistableEntityRepository; -import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -23,17 +22,18 @@ import reactor.core.publisher.Mono; import reactor.test.StepVerifier; -import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = TestRepositoryConfig.class) public class PersistableIT { - private static final CosmosEntityInformation entityInformation - = new CosmosEntityInformation<>(PersistableEntity.class); - - private static CosmosTemplate staticTemplate; - private static boolean isSetupDone; + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); @Autowired private PersistableEntityRepository repository; @@ -49,21 +49,7 @@ public class PersistableIT { @Before public void setUp() { - if (!isSetupDone) { - staticTemplate = template; - template.createContainerIfNotExists(entityInformation); - } - isSetupDone = true; - } - - @After - public void cleanup() { - repository.deleteAll(); - } - - @AfterClass - public static void afterClassCleanup() { - staticTemplate.deleteContainer(entityInformation.getContainerName()); + collectionManager.ensureContainersCreatedAndEmpty(template, PersistableEntity.class); } @Test diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ProjectRepositoryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ProjectRepositoryIT.java index bf80a639d7f23..5394b175a6b32 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ProjectRepositoryIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ProjectRepositoryIT.java @@ -3,16 +3,15 @@ package com.azure.spring.data.cosmos.repository.integration; import com.azure.cosmos.models.PartitionKey; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.common.TestUtils; import com.azure.spring.data.cosmos.core.CosmosTemplate; import com.azure.spring.data.cosmos.domain.Project; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.ProjectRepository; -import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -74,11 +73,8 @@ public class ProjectRepositoryIT { private static final List PROJECTS = Arrays.asList(PROJECT_0, PROJECT_1, PROJECT_2, PROJECT_3, PROJECT_4); - private static final CosmosEntityInformation entityInformation = - new CosmosEntityInformation<>(Project.class); - - private static CosmosTemplate staticTemplate; - private static boolean isSetupDone; + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); @Autowired private CosmosTemplate template; @@ -88,22 +84,8 @@ public class ProjectRepositoryIT { @Before public void setUp() { - if (!isSetupDone) { - staticTemplate = template; - template.createContainerIfNotExists(entityInformation); - } + collectionManager.ensureContainersCreatedAndEmpty(template, Project.class); this.repository.saveAll(PROJECTS); - isSetupDone = true; - } - - @After - public void cleanup() { - this.repository.deleteAll(); - } - - @AfterClass - public static void afterClassCleanup() { - staticTemplate.deleteContainer(entityInformation.getContainerName()); } private void assertProjectListEquals(@NonNull List projects, @NonNull List reference) { @@ -159,7 +141,7 @@ public void testFindByWithAndPartition() { assertProjectListEquals(projects, Arrays.asList(PROJECT_0, PROJECT_4)); } - + @Test public void testFindByWithRepeatedParameters() { List projects = TestUtils.toList(this.repository.findByNameAndCreatorOrNameAndCreator(NAME_1, CREATOR_1, NAME_2, CREATOR_2)); @@ -378,7 +360,7 @@ public void testFindByTrueFalseWithOr() { @Test public void findByIdWithPartitionKey() { final Optional project = repository.findById(PROJECT_0.getId(), - new PartitionKey(entityInformation.getPartitionKeyFieldValue(PROJECT_0))); + new PartitionKey(collectionManager.getEntityInformation(Project.class).getPartitionKeyFieldValue(PROJECT_0))); Assert.assertTrue(project.isPresent()); diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ProjectRepositorySortIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ProjectRepositorySortIT.java index c5cd28ec1ccf1..9934344e2c7ce 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ProjectRepositorySortIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ProjectRepositorySortIT.java @@ -2,18 +2,17 @@ // Licensed under the MIT License. package com.azure.spring.data.cosmos.repository.integration; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.core.CosmosTemplate; import com.azure.spring.data.cosmos.core.query.CosmosPageRequest; import com.azure.spring.data.cosmos.domain.SortedProject; import com.azure.spring.data.cosmos.exception.CosmosAccessException; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.SortedProjectRepository; -import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; import org.assertj.core.util.Lists; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -79,11 +78,8 @@ public class ProjectRepositorySortIT { private static final List PROJECTS = Arrays.asList(PROJECT_4, PROJECT_3, PROJECT_2, PROJECT_1, PROJECT_0); - private static final CosmosEntityInformation entityInformation = - new CosmosEntityInformation<>(SortedProject.class); - - private static CosmosTemplate staticTemplate; - private static boolean isSetupDone; + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); @Autowired private CosmosTemplate template; @@ -93,22 +89,8 @@ public class ProjectRepositorySortIT { @Before public void setUp() { - if (!isSetupDone) { - staticTemplate = template; - template.createContainerIfNotExists(entityInformation); - } + collectionManager.ensureContainersCreatedAndEmpty(template, SortedProject.class); this.repository.saveAll(PROJECTS); - isSetupDone = true; - } - - @After - public void cleanup() { - this.repository.deleteAll(); - } - - @AfterClass - public static void afterClassCleanup() { - staticTemplate.deleteContainer(entityInformation.getContainerName()); } @Test diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/QuestionRepositoryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/QuestionRepositoryIT.java index 5cd17d70a034c..bbd2ae67dc24b 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/QuestionRepositoryIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/QuestionRepositoryIT.java @@ -2,16 +2,15 @@ // Licensed under the MIT License. package com.azure.spring.data.cosmos.repository.integration; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.core.CosmosTemplate; import com.azure.spring.data.cosmos.domain.Question; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.QuestionRepository; -import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; import org.assertj.core.util.Lists; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -32,11 +31,8 @@ public class QuestionRepositoryIT { private static final Question QUESTION = new Question(QUESTION_ID, QUESTION_URL); - private static final CosmosEntityInformation entityInformation = - new CosmosEntityInformation<>(Question.class); - - private static CosmosTemplate staticTemplate; - private static boolean isSetupDone; + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); @Autowired private CosmosTemplate template; @@ -46,22 +42,8 @@ public class QuestionRepositoryIT { @Before public void setUp() { - if (!isSetupDone) { - staticTemplate = template; - template.createContainerIfNotExists(entityInformation); - } + collectionManager.ensureContainersCreatedAndEmpty(template, Question.class); this.repository.save(QUESTION); - isSetupDone = true; - } - - @After - public void cleanup() { - this.repository.deleteAll(); - } - - @AfterClass - public static void afterClassCleanup() { - staticTemplate.deleteContainer(entityInformation.getContainerName()); } @Test diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveAuditableIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveAuditableIT.java index f17368184faee..af357da4932b6 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveAuditableIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveAuditableIT.java @@ -2,6 +2,8 @@ // Licensed under the MIT License. package com.azure.spring.data.cosmos.repository.integration; +import com.azure.spring.data.cosmos.ReactiveIntegrationTestCollectionManager; +import com.azure.spring.data.cosmos.core.ReactiveCosmosTemplate; import com.azure.spring.data.cosmos.domain.AuditableEntity; import com.azure.spring.data.cosmos.domain.AuditableIdGeneratedEntity; import com.azure.spring.data.cosmos.repository.StubAuditorProvider; @@ -9,7 +11,8 @@ import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.ReactiveAuditableIdGeneratedRepository; import com.azure.spring.data.cosmos.repository.repository.ReactiveAuditableRepository; -import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -28,6 +31,11 @@ @ContextConfiguration(classes = TestRepositoryConfig.class) public class ReactiveAuditableIT { + @ClassRule + public static final ReactiveIntegrationTestCollectionManager collectionManager = new ReactiveIntegrationTestCollectionManager(); + + @Autowired + private ReactiveCosmosTemplate template; @Autowired private ReactiveAuditableRepository auditableRepository; @Autowired @@ -37,9 +45,9 @@ public class ReactiveAuditableIT { @Autowired private StubAuditorProvider stubAuditorProvider; - @After - public void cleanup() { - this.auditableRepository.deleteAll().block(); + @Before + public void setup() { + collectionManager.ensureContainersCreatedAndEmpty(template, AuditableEntity.class, AuditableIdGeneratedEntity.class); } @Test @@ -122,4 +130,5 @@ private boolean validateAuditableFields(AuditableIdGeneratedEntity entity, assertThat(entity.getLastModifiedByDate()).isEqualTo(expectedModifiedTime); return true; } + } diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveCourseRepositoryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveCourseRepositoryIT.java index fa2fb294fbbae..72ebf0bc81081 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveCourseRepositoryIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveCourseRepositoryIT.java @@ -3,6 +3,7 @@ package com.azure.spring.data.cosmos.repository.integration; import com.azure.cosmos.models.PartitionKey; +import com.azure.spring.data.cosmos.ReactiveIntegrationTestCollectionManager; import com.azure.spring.data.cosmos.core.ReactiveCosmosTemplate; import com.azure.spring.data.cosmos.domain.Course; import com.azure.spring.data.cosmos.exception.CosmosAccessException; @@ -11,9 +12,8 @@ import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; import com.fasterxml.jackson.databind.node.ObjectNode; import org.assertj.core.api.Assertions; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -56,39 +56,23 @@ public class ReactiveCourseRepositoryIT { private static final Course COURSE_4 = new Course(COURSE_ID_4, COURSE_NAME_4, DEPARTMENT_NAME_1); private static final Course COURSE_5 = new Course(COURSE_ID_5, COURSE_NAME_5, DEPARTMENT_NAME_1); - private static final CosmosEntityInformation entityInformation = - new CosmosEntityInformation<>(Course.class); - - private static ReactiveCosmosTemplate staticTemplate; - private static boolean isSetupDone; + @ClassRule + public static final ReactiveIntegrationTestCollectionManager collectionManager = new ReactiveIntegrationTestCollectionManager(); @Autowired private ReactiveCosmosTemplate template; @Autowired private ReactiveCourseRepository repository; + private CosmosEntityInformation entityInformation; @Before public void setUp() { - if (!isSetupDone) { - staticTemplate = template; - template.createContainerIfNotExists(entityInformation); - } + collectionManager.ensureContainersCreatedAndEmpty(template, Course.class); + entityInformation = collectionManager.getEntityInformation(Course.class); final Flux savedFlux = repository.saveAll(Arrays.asList(COURSE_1, COURSE_2, COURSE_3, COURSE_4)); StepVerifier.create(savedFlux).thenConsumeWhile(course -> true).expectComplete().verify(); - isSetupDone = true; - } - - @After - public void cleanup() { - final Mono deletedMono = repository.deleteAll(); - StepVerifier.create(deletedMono).thenAwait().verifyComplete(); - } - - @AfterClass - public static void afterClassCleanup() { - staticTemplate.deleteContainer(entityInformation.getContainerName()); } @Test @@ -296,7 +280,7 @@ public void testFindByNameAndDepartmentAllIgnoreCase() { COURSE_NAME_1.toLowerCase(), DEPARTMENT_NAME_3.toLowerCase()); StepVerifier.create(findResult).expectNext(COURSE_1).verifyComplete(); } - + @Test public void testFindByNameAndDepartmentOrNameAndDepartment() { final Flux findResult = repository.findByNameAndDepartmentOrNameAndDepartment( diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveEtagIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveEtagIT.java index a39e65e16278b..d6c3b2dc3f328 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveEtagIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveEtagIT.java @@ -2,12 +2,15 @@ // Licensed under the MIT License. package com.azure.spring.data.cosmos.repository.integration; +import com.azure.spring.data.cosmos.ReactiveIntegrationTestCollectionManager; +import com.azure.spring.data.cosmos.core.ReactiveCosmosTemplate; import com.azure.spring.data.cosmos.domain.CourseWithEtag; import com.azure.spring.data.cosmos.exception.CosmosAccessException; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.ReactiveCourseWithEtagRepository; -import org.junit.After; import org.junit.Assert; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -29,12 +32,17 @@ @ContextConfiguration(classes = TestRepositoryConfig.class) public class ReactiveEtagIT { + @ClassRule + public static final ReactiveIntegrationTestCollectionManager collectionManager = new ReactiveIntegrationTestCollectionManager(); + + @Autowired + ReactiveCosmosTemplate template; @Autowired ReactiveCourseWithEtagRepository reactiveCourseWithEtagRepository; - @After - public void cleanup() { - reactiveCourseWithEtagRepository.deleteAll().block(); + @Before + public void setup() { + collectionManager.ensureContainersCreatedAndEmpty(template, CourseWithEtag.class); } private static CourseWithEtag createCourseWithEtag() { diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveIndexPolicyUpdateIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveIndexPolicyUpdateIT.java index 258c50e9ca963..fcfe90bdce734 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveIndexPolicyUpdateIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveIndexPolicyUpdateIT.java @@ -7,12 +7,15 @@ import com.azure.cosmos.models.IncludedPath; import com.azure.cosmos.models.IndexingMode; import com.azure.cosmos.models.IndexingPolicy; +import com.azure.spring.data.cosmos.ReactiveIntegrationTestCollectionManager; import com.azure.spring.data.cosmos.core.ReactiveCosmosTemplate; import com.azure.spring.data.cosmos.domain.ComplexIndexPolicyEntity; import com.azure.spring.data.cosmos.domain.IndexPolicyEntity; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; import com.azure.spring.data.cosmos.repository.support.SimpleReactiveCosmosRepository; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; @@ -29,6 +32,9 @@ @ContextConfiguration(classes = TestRepositoryConfig.class) public class ReactiveIndexPolicyUpdateIT { + @ClassRule + public static final ReactiveIntegrationTestCollectionManager collectionManager = new ReactiveIntegrationTestCollectionManager(); + @Autowired ReactiveCosmosTemplate template; @@ -39,6 +45,11 @@ public class ReactiveIndexPolicyUpdateIT { CosmosEntityInformation complexIndexPolicyEntityInformation = new CosmosEntityInformation<>(ComplexIndexPolicyEntity.class); + @Before + public void setup() { + collectionManager.ensureContainersCreatedAndEmpty(template, IndexPolicyEntity.class, ComplexIndexPolicyEntity.class); + } + @Test public void testIndexPolicyUpdatesOnRepoInitialization() { // set index policy based on entity annotation diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveLongIdDomainPartitionPartitionRepositoryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveLongIdDomainPartitionPartitionRepositoryIT.java index c06d9ec1cb89b..823b7897bc8fa 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveLongIdDomainPartitionPartitionRepositoryIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveLongIdDomainPartitionPartitionRepositoryIT.java @@ -3,15 +3,15 @@ package com.azure.spring.data.cosmos.repository.integration; import com.azure.cosmos.models.PartitionKey; -import com.azure.spring.data.cosmos.core.CosmosTemplate; +import com.azure.spring.data.cosmos.ReactiveIntegrationTestCollectionManager; +import com.azure.spring.data.cosmos.core.ReactiveCosmosTemplate; import com.azure.spring.data.cosmos.domain.LongIdDomainPartition; import com.azure.spring.data.cosmos.exception.CosmosAccessException; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.ReactiveLongIdDomainPartitionRepository; import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -38,38 +38,23 @@ public class ReactiveLongIdDomainPartitionPartitionRepositoryIT { private static final LongIdDomainPartition DOMAIN_1 = new LongIdDomainPartition(ID_1, NAME_1); private static final LongIdDomainPartition DOMAIN_2 = new LongIdDomainPartition(ID_2, NAME_2); - private static final CosmosEntityInformation entityInformation = - new CosmosEntityInformation<>(LongIdDomainPartition.class); - - private static CosmosTemplate staticTemplate; - private static boolean isSetupDone; + @ClassRule + public static final ReactiveIntegrationTestCollectionManager collectionManager = new ReactiveIntegrationTestCollectionManager(); @Autowired - private CosmosTemplate template; + private ReactiveCosmosTemplate template; @Autowired private ReactiveLongIdDomainPartitionRepository repository; + private CosmosEntityInformation entityInformation; + @Before public void setUp() { - if (!isSetupDone) { - staticTemplate = template; - template.createContainerIfNotExists(entityInformation); - } + collectionManager.ensureContainersCreatedAndEmpty(template, LongIdDomainPartition.class); + entityInformation = collectionManager.getEntityInformation(LongIdDomainPartition.class); Flux savedAllFlux = this.repository.saveAll(Arrays.asList(DOMAIN_1, DOMAIN_2)); StepVerifier.create(savedAllFlux).thenConsumeWhile(domain -> true).expectComplete().verify(); - isSetupDone = true; - } - - @After - public void cleanup() { - final Mono deletedMono = repository.deleteAll(); - StepVerifier.create(deletedMono).thenAwait().verifyComplete(); - } - - @AfterClass - public static void afterClassCleanup() { - staticTemplate.deleteContainer(entityInformation.getContainerName()); } @Test diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveNestedPartitionKeyRepositoryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveNestedPartitionKeyRepositoryIT.java index 12867029fc930..fb583422fe3a7 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveNestedPartitionKeyRepositoryIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveNestedPartitionKeyRepositoryIT.java @@ -3,16 +3,15 @@ package com.azure.spring.data.cosmos.repository.integration; import com.azure.cosmos.models.PartitionKey; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.core.CosmosTemplate; import com.azure.spring.data.cosmos.domain.NestedEntity; import com.azure.spring.data.cosmos.domain.NestedPartitionKeyEntity; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.ReactiveNestedPartitionKeyRepository; -import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -34,11 +33,8 @@ public class ReactiveNestedPartitionKeyRepositoryIT { private static final NestedPartitionKeyEntity NESTED_ENTITY_2 = new NestedPartitionKeyEntity(null, new NestedEntity("partitionKey2")); - private static final CosmosEntityInformation entityInformation = - new CosmosEntityInformation<>(NestedPartitionKeyEntity.class); - - private static CosmosTemplate staticTemplate; - private static boolean isSetupDone; + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); @Autowired private CosmosTemplate template; @@ -51,21 +47,7 @@ public static void init() { } @Before public void setUp() { - if (!isSetupDone) { - staticTemplate = template; - template.createContainerIfNotExists(entityInformation); - } - isSetupDone = true; - } - - @After - public void cleanup() { - repository.deleteAll(); - } - - @AfterClass - public static void afterClassCleanup() { - staticTemplate.deleteContainer(entityInformation.getContainerName()); + collectionManager.ensureContainersCreatedAndEmpty(template, NestedPartitionKeyEntity.class); } @Test diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveRoleRepositoryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveRoleRepositoryIT.java index d79b375925da1..1c51dcef164e4 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveRoleRepositoryIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveRoleRepositoryIT.java @@ -2,15 +2,14 @@ // Licensed under the MIT License. package com.azure.spring.data.cosmos.repository.integration; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.common.TestConstants; import com.azure.spring.data.cosmos.core.CosmosTemplate; import com.azure.spring.data.cosmos.domain.Role; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.ReactiveRoleRepository; -import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -18,7 +17,6 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; import reactor.test.StepVerifier; import java.util.Arrays; @@ -34,35 +32,19 @@ public class ReactiveRoleRepositoryIT { private static final Role TEST_ROLE_3 = new Role(TestConstants.ID_3, true, TestConstants.ROLE_NAME, TestConstants.LEVEL); - private static final CosmosEntityInformation entityInformation = - new CosmosEntityInformation<>(Role.class); - private static CosmosTemplate staticTemplate; - private static boolean isSetupDone; + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); + @Autowired private CosmosTemplate template; @Autowired private ReactiveRoleRepository repository; - @AfterClass - public static void afterClassCleanup() { - staticTemplate.deleteContainer(entityInformation.getContainerName()); - } - @Before public void setUp() { - if (!isSetupDone) { - staticTemplate = template; - template.createContainerIfNotExists(entityInformation); - } + collectionManager.ensureContainersCreatedAndEmpty(template, Role.class); final Flux savedFlux = repository.saveAll(Arrays.asList(TEST_ROLE_1, TEST_ROLE_2, TEST_ROLE_3)); StepVerifier.create(savedFlux).thenConsumeWhile(role -> true).expectComplete().verify(); - isSetupDone = true; - } - - @After - public void cleanup() { - final Mono deletedMono = repository.deleteAll(); - StepVerifier.create(deletedMono).thenAwait().verifyComplete(); } @Test diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveTeacherRepositoryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveTeacherRepositoryIT.java index 7ae7cc39faaf3..8a707c7e51eb8 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveTeacherRepositoryIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveTeacherRepositoryIT.java @@ -3,14 +3,13 @@ package com.azure.spring.data.cosmos.repository.integration; +import com.azure.spring.data.cosmos.ReactiveIntegrationTestCollectionManager; import com.azure.spring.data.cosmos.core.ReactiveCosmosTemplate; import com.azure.spring.data.cosmos.domain.ReactiveTeacher; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.ReactiveTeacherRepository; -import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -34,11 +33,8 @@ public class ReactiveTeacherRepositoryIT { private static final ReactiveTeacher TEACHER_1 = new ReactiveTeacher(TEACHER_ID_1, TEACHER_FIRST_NAME_1, DEPARTMENT_LAST_NAME_1); - private static final CosmosEntityInformation entityInformation = - new CosmosEntityInformation<>(ReactiveTeacher.class); - - private static ReactiveCosmosTemplate staticTemplate; - private static boolean isSetupDone; + @ClassRule + public static final ReactiveIntegrationTestCollectionManager collectionManager = new ReactiveIntegrationTestCollectionManager(); @Autowired private ReactiveCosmosTemplate template; @@ -48,24 +44,9 @@ public class ReactiveTeacherRepositoryIT { @Before public void setUp() { - if (!isSetupDone) { - staticTemplate = template; - template.createContainerIfNotExists(entityInformation); - } + collectionManager.ensureContainersCreatedAndEmpty(template, ReactiveTeacher.class); final Flux savedFlux = repository.saveAll(Arrays.asList(TEACHER_1)); StepVerifier.create(savedFlux).thenConsumeWhile(ReactiveTeacher -> true).expectComplete().verify(); - isSetupDone = true; - } - - @After - public void cleanup() { - final Mono deletedMono = repository.deleteAll(); - StepVerifier.create(deletedMono).thenAwait().verifyComplete(); - } - - @AfterClass - public static void afterClassCleanup() { - staticTemplate.deleteContainer(entityInformation.getContainerName()); } @Test diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/SquareRepositoryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/SquareRepositoryIT.java index 131fe0c756268..f4cf427b14b95 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/SquareRepositoryIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/SquareRepositoryIT.java @@ -2,15 +2,14 @@ // Licensed under the MIT License. package com.azure.spring.data.cosmos.repository.integration; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.common.TestUtils; import com.azure.spring.data.cosmos.core.CosmosTemplate; import com.azure.spring.data.cosmos.domain.inheritance.Square; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.SquareRepository; -import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -28,11 +27,8 @@ public class SquareRepositoryIT { private Square square1 = new Square("id_1", 1, 1); private Square square2 = new Square("id_2", 2, 4); - private static final CosmosEntityInformation entityInformation = - new CosmosEntityInformation<>(Square.class); - - private static CosmosTemplate staticTemplate; - private static boolean isSetupDone; + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); @Autowired private CosmosTemplate template; @@ -42,23 +38,9 @@ public class SquareRepositoryIT { @Before public void setUp() { - if (!isSetupDone) { - staticTemplate = template; - template.createContainerIfNotExists(entityInformation); - } + collectionManager.ensureContainersCreatedAndEmpty(template, Square.class); repository.save(square1); repository.save(square2); - isSetupDone = true; - } - - @After - public void cleanup() { - repository.deleteAll(); - } - - @AfterClass - public static void afterClassCleanup() { - staticTemplate.deleteContainer(entityInformation.getContainerName()); } @Test diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/StudentRepositoryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/StudentRepositoryIT.java index 7cf65109d7f67..410f7ba9ec5e8 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/StudentRepositoryIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/StudentRepositoryIT.java @@ -2,16 +2,15 @@ // Licensed under the MIT License. package com.azure.spring.data.cosmos.repository.integration; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.common.TestUtils; import com.azure.spring.data.cosmos.core.CosmosTemplate; import com.azure.spring.data.cosmos.domain.Student; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.StudentRepository; -import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -58,11 +57,8 @@ public class StudentRepositoryIT { private static final List PEOPLE = Arrays.asList(STUDENT_0, STUDENT_1, STUDENT_2, STUDENT_3, STUDENT_4, STUDENT_5); - private static final CosmosEntityInformation entityInformation = - new CosmosEntityInformation<>(Student.class); - - private static CosmosTemplate staticTemplate; - private static boolean isSetupDone; + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); @Autowired private CosmosTemplate template; @@ -72,22 +68,8 @@ public class StudentRepositoryIT { @Before public void setUp() { - if (!isSetupDone) { - staticTemplate = template; - template.createContainerIfNotExists(entityInformation); - } + collectionManager.ensureContainersCreatedAndEmpty(template, Student.class); this.repository.saveAll(PEOPLE); - isSetupDone = true; - } - - @After - public void cleanup() { - this.repository.deleteAll(); - } - - @AfterClass - public static void afterClassCleanup() { - staticTemplate.deleteContainer(entityInformation.getContainerName()); } @Test diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/TeacherRepositoryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/TeacherRepositoryIT.java index b9a0bf032ec6b..c8be63dec53f3 100644 --- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/TeacherRepositoryIT.java +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/TeacherRepositoryIT.java @@ -3,14 +3,13 @@ package com.azure.spring.data.cosmos.repository.integration; +import com.azure.spring.data.cosmos.IntegrationTestCollectionManager; import com.azure.spring.data.cosmos.core.CosmosTemplate; import com.azure.spring.data.cosmos.domain.Teacher; import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; import com.azure.spring.data.cosmos.repository.repository.TeacherRepository; -import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -36,11 +35,8 @@ public class TeacherRepositoryIT { private static final Teacher TEACHER_0 = new Teacher(ID_0, FIRST_NAME_0, LAST_NAME_0); - private static final CosmosEntityInformation entityInformation = - new CosmosEntityInformation<>(Teacher.class); - - private static CosmosTemplate staticTemplate; - private static boolean isSetupDone; + @ClassRule + public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager(); @Autowired private CosmosTemplate template; @@ -50,22 +46,8 @@ public class TeacherRepositoryIT { @Before public void setUp() { - if (!isSetupDone) { - staticTemplate = template; - template.createContainerIfNotExists(entityInformation); - } + collectionManager.ensureContainersCreatedAndEmpty(template, Teacher.class); this.repository.save(TEACHER_0); - isSetupDone = true; - } - - @After - public void cleanup() { - this.repository.deleteAll(); - } - - @AfterClass - public static void afterClassCleanup() { - staticTemplate.deleteContainer(entityInformation.getContainerName()); } @Test