Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

refactor: rename cache test base #381

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class AuthClientCacheTests extends BaseTestClass {
public class AuthClientCacheTests extends BaseCacheTestClass {
private static AuthClient authClient;

String key = "test-key";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class AuthClientTopicTests extends BaseTestClass {
public class AuthClientTopicTests extends BaseCacheTestClass {
private static AuthClient authClient;
private static String topicName = "topic";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;

public class BaseTestClass {
public class BaseCacheTestClass {
protected static final Duration DEFAULT_TTL_SECONDS = Duration.ofSeconds(60);
protected static final Duration FIVE_SECONDS = Duration.ofSeconds(5);
protected static CredentialProvider credentialProvider;
Expand Down
55 changes: 55 additions & 0 deletions momento-sdk/src/intTest/java/momento/sdk/BaseStorageTestClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package momento.sdk;

import java.time.Duration;
import java.util.UUID;
import momento.sdk.auth.CredentialProvider;
import momento.sdk.config.StorageConfigurations;
import momento.sdk.responses.storage.CreateStoreResponse;
import momento.sdk.responses.storage.DeleteStoreResponse;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;

public class BaseStorageTestClass {
protected static final Duration TEN_SECONDS = Duration.ofSeconds(10);
protected static CredentialProvider credentialProvider;
protected static PreviewStorageClient storageClient;
protected static String storeName;

@BeforeAll
static void beforeAll() {
credentialProvider = CredentialProvider.fromEnvVar("TEST_AUTH_TOKEN");
storageClient =
new PreviewStorageClientBuilder()
.withCredentialProvider(credentialProvider)
.withConfiguration(StorageConfigurations.Laptop.latest())
.build();
storeName = testStoreName();
ensureTestStoreExists(storeName);
}

@AfterAll
static void afterAll() {
cleanupTestStore(storeName);
storageClient.close();
}

protected static void ensureTestStoreExists(String storeName) {
CreateStoreResponse response = storageClient.createStore(storeName).join();
if (response instanceof CreateStoreResponse.Error) {
throw new RuntimeException(
"Failed to test create store: " + ((CreateStoreResponse.Error) response).getMessage());
}
}

public static void cleanupTestStore(String storeName) {
DeleteStoreResponse response = storageClient.deleteStore(storeName).join();
if (response instanceof DeleteStoreResponse.Error) {
throw new RuntimeException(
"Failed to test delete store: " + ((DeleteStoreResponse.Error) response).getMessage());
}
}

public static String testStoreName() {
return "java-integration-test-default-" + UUID.randomUUID();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.junit.jupiter.api.Test;

/** Just includes a happy test path that interacts with both control and data plane clients. */
final class CacheClientTest extends BaseTestClass {
final class CacheClientTest extends BaseCacheTestClass {
private static final String JWT_HEADER_BASE64 = "eyJhbGciOiJIUzUxMiJ9";
private static final String JWT_INVALID_SIGNATURE_BASE64 =
"gdghdjjfjyehhdkkkskskmmls76573jnajhjjjhjdhnndy";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Test;

final class CacheControlPlaneTest extends BaseTestClass {
final class CacheControlPlaneTest extends BaseCacheTestClass {
@Test
public void createListRevokeSigningKeyWorks() {
final SigningKeyCreateResponse signingKeyCreateResponse =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.junit.jupiter.api.Test;

/** Tests client side exceptions */
final class CacheDataPlaneClientSideTest extends BaseTestClass {
final class CacheDataPlaneClientSideTest extends BaseCacheTestClass {
@Test
public void nullKeyGetReturnsError() {
final GetResponse stringResponse = cacheClient.get(cacheName, (String) null).join();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package momento.sdk;

import static momento.sdk.BaseTestClass.credentialProvider;
import static momento.sdk.TestUtils.randomString;
import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -10,7 +9,7 @@
import momento.sdk.responses.cache.SetResponse;
import org.junit.jupiter.api.Test;

public class CacheDataPlaneEagerConnectionTest extends BaseTestClass {
public class CacheDataPlaneEagerConnectionTest extends BaseCacheTestClass {
@Test
void getReturnsHitAfterSet() {
CacheClient client =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.junit.jupiter.api.Test;

/** Tests with Async APIs. */
final class CacheDataPlaneTest extends BaseTestClass {
final class CacheDataPlaneTest extends BaseCacheTestClass {
@Test
void getReturnsHitAfterSet() {
final String key = randomString("key");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Test;

public class DictionaryTest extends BaseTestClass {
public class DictionaryTest extends BaseCacheTestClass {
Map<String, String> stringStringMap = new HashMap<>();
Map<String, byte[]> stringBytesMap = new HashMap<>();
Map<byte[], String> bytesStringMap = new HashMap<>();
Expand Down
2 changes: 1 addition & 1 deletion momento-sdk/src/intTest/java/momento/sdk/ListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Test;

public class ListTest extends BaseTestClass {
public class ListTest extends BaseCacheTestClass {
private final List<String> values = Arrays.asList("val1", "val2", "val3", "val4");

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class MomentoBatchUtilsIntegrationTest extends BaseTestClass {
public class MomentoBatchUtilsIntegrationTest extends BaseCacheTestClass {
private static MomentoBatchUtils momentoBatchUtils;

@BeforeAll
Expand Down
2 changes: 1 addition & 1 deletion momento-sdk/src/intTest/java/momento/sdk/SetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Test;

public class SetTest extends BaseTestClass {
public class SetTest extends BaseCacheTestClass {
@Test
public void setAddElementStringHappyPath() {
final String setName = randomString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Test;

public class SortedSetTest extends BaseTestClass {
public class SortedSetTest extends BaseCacheTestClass {
// sortedSetPutElement

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TopicClientTest extends BaseTestClass {
public class TopicClientTest extends BaseCacheTestClass {
private static TopicClient topicClient;

private final String topicName = "test-topic";
Expand Down
69 changes: 22 additions & 47 deletions momento-sdk/src/intTest/java/momento/sdk/storage/ControlTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import static momento.sdk.TestUtils.randomString;
import static org.assertj.core.api.Assertions.assertThat;

import java.time.Duration;
import momento.sdk.BaseTestClass;
import momento.sdk.BaseStorageTestClass;
import momento.sdk.PreviewStorageClient;
import momento.sdk.auth.CredentialProvider;
import momento.sdk.config.StorageConfigurations;
Expand All @@ -16,45 +15,21 @@
import momento.sdk.responses.storage.DeleteStoreResponse;
import momento.sdk.responses.storage.ListStoresResponse;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class ControlTests extends BaseTestClass {
private static PreviewStorageClient client;

public static final Duration TEN_SECONDS = Duration.ofSeconds(10);

@BeforeAll
static void setup() {
client =
new PreviewStorageClient(
CredentialProvider.fromEnvVar("MOMENTO_API_KEY"),
StorageConfigurations.Laptop.latest());

// TODO re-using this name
client.createStore(System.getenv("TEST_CACHE_NAME")).join();
}

@AfterAll
static void tearDown() {
client.close();
}

public class ControlTests extends BaseStorageTestClass {
@Test
public void returnsAlreadyExistsWhenCreatingExistingStore() {
// TODO externalize this
// TODO rename env var to something broader like TEST_RESOURCE_NAME
final String existingStore = System.getenv("TEST_CACHE_NAME");
final String existingStore = storeName;

assertThat(client.createStore(existingStore))
assertThat(storageClient.createStore(existingStore))
.succeedsWithin(TEN_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(CreateStoreResponse.AlreadyExists.class));
}

@Test
public void returnsNotFoundWhenDeletingUnknownStore() {
assertThat(client.deleteStore(randomString("name")))
assertThat(storageClient.deleteStore(randomString("name")))
.succeedsWithin(TEN_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(DeleteStoreResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(StoreNotFoundException.class));
Expand All @@ -64,45 +39,45 @@ public void returnsNotFoundWhenDeletingUnknownStore() {
public void listsStoresHappyPath() {
final String storeName = randomString("name");

assertThat(client.createStore(storeName))
assertThat(storageClient.createStore(storeName))
.succeedsWithin(TEN_SECONDS)
.isInstanceOf(CreateStoreResponse.Success.class);

try {
assertThat(client.listStores())
assertThat(storageClient.listStores())
.succeedsWithin(TEN_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(ListStoresResponse.Success.class))
.satisfies(
success ->
assertThat(success.getStores())
.anyMatch(storeInfo -> storeInfo.getName().equals(storeName)));

final ListStoresResponse response = client.listStores().join();
final ListStoresResponse response = storageClient.listStores().join();
assertThat(response).isInstanceOf(ListStoresResponse.Success.class);
} finally {
// cleanup
assertThat(client.deleteStore(storeName))
assertThat(storageClient.deleteStore(storeName))
.succeedsWithin(TEN_SECONDS)
.isInstanceOf(DeleteStoreResponse.Success.class);
}
}

@Test
public void returnsBadRequestForEmptyStoreName() {
assertThat(client.createStore(" "))
assertThat(storageClient.createStore(" "))
.succeedsWithin(TEN_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(CreateStoreResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(BadRequestException.class));
}

@Test
public void throwsValidationExceptionForNullStoreName() {
assertThat(client.createStore(null))
assertThat(storageClient.createStore(null))
.succeedsWithin(TEN_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(CreateStoreResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(InvalidArgumentException.class));

assertThat(client.deleteStore(null))
assertThat(storageClient.deleteStore(null))
.succeedsWithin(TEN_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(DeleteStoreResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(InvalidArgumentException.class));
Expand All @@ -113,25 +88,25 @@ public void deleteSucceeds() {
final String storeName = randomString("name");

try {
assertThat(client.createStore(storeName))
assertThat(storageClient.createStore(storeName))
.succeedsWithin(TEN_SECONDS)
.isInstanceOf(CreateStoreResponse.Success.class);

assertThat(client.createStore(storeName))
assertThat(storageClient.createStore(storeName))
.succeedsWithin(TEN_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(CreateStoreResponse.AlreadyExists.class));

assertThat(client.deleteStore(storeName))
assertThat(storageClient.deleteStore(storeName))
.succeedsWithin(TEN_SECONDS)
.isInstanceOf(DeleteStoreResponse.Success.class);

assertThat(client.deleteStore(storeName))
assertThat(storageClient.deleteStore(storeName))
.succeedsWithin(TEN_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(DeleteStoreResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(StoreNotFoundException.class));
} finally {
// Just in case the second create or delete fails
client.deleteStore(storeName).join();
storageClient.deleteStore(storeName).join();
}
}

Expand All @@ -144,23 +119,23 @@ public void returnsErrorForBadToken() {
+ "s76573jnajhjjjhjdhnndy";
final CredentialProvider badTokenProvider = CredentialProvider.fromString(badToken);

try (final PreviewStorageClient client =
try (final PreviewStorageClient storageClient =
new PreviewStorageClient(
CredentialProvider.fromString(badToken),
StorageConfigurations.Laptop.latest()) /*CacheClient.builder(
StorageConfigurations.Laptop.latest()) /*CacheStorageClient.builder(
badTokenProvider, Configurations.Laptop.latest(), Duration.ofSeconds(10))
.build()*/) {
assertThat(client.createStore(storeName))
assertThat(storageClient.createStore(storeName))
.succeedsWithin(TEN_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(CreateStoreResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(AuthenticationException.class));

assertThat(client.deleteStore(storeName))
assertThat(storageClient.deleteStore(storeName))
.succeedsWithin(TEN_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(DeleteStoreResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(AuthenticationException.class));

assertThat(client.listStores())
assertThat(storageClient.listStores())
.succeedsWithin(TEN_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(ListStoresResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(AuthenticationException.class));
Expand Down
Loading
Loading