Search in sources :

Example 6 with DatabaseException

use of com.sanctionco.thunder.dao.DatabaseException in project thunder by RohanNagar.

the class MongoDbUsersDaoTest method testUpdatePutRequestRejected.

@Test
void testUpdatePutRequestRejected() {
    MongoCollection<Document> collection = mock(MongoCollection.class);
    FindIterable<Document> findIterable = mock(FindIterable.class);
    MongoCommandException exception = mock(MongoCommandException.class);
    when(exception.getErrorMessage()).thenReturn("Test error");
    when(findIterable.first()).thenReturn(DOCUMENT);
    doReturn(findIterable).when(collection).find(any(Bson.class));
    doThrow(exception).when(collection).updateOne(any(Bson.class), any(Bson.class));
    MongoDbUsersDao usersDao = new MongoDbUsersDao(collection, MAPPER);
    CompletionException e = assertThrows(CompletionException.class, () -> usersDao.update(null, USER).join());
    assertTrue(e.getCause() instanceof DatabaseException);
    var exp = (DatabaseException) e.getCause();
    assertEquals(DatabaseException.Error.REQUEST_REJECTED, exp.getError());
    verify(collection, times(1)).find(any(Bson.class));
    verify(collection, times(1)).updateOne(argThat((Bson bson) -> {
        BsonDocument doc = bson.toBsonDocument(BsonDocument.class, MongoClientSettings.getDefaultCodecRegistry());
        return doc.containsKey("version");
    }), any(Bson.class));
}
Also used : MongoCommandException(com.mongodb.MongoCommandException) BsonDocument(org.bson.BsonDocument) CompletionException(java.util.concurrent.CompletionException) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) DatabaseException(com.sanctionco.thunder.dao.DatabaseException) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.Test)

Example 7 with DatabaseException

use of com.sanctionco.thunder.dao.DatabaseException in project thunder by RohanNagar.

the class MongoDbUsersDaoTest method testUpdatePutTimeout.

@Test
void testUpdatePutTimeout() {
    MongoCollection<Document> collection = mock(MongoCollection.class);
    FindIterable<Document> findIterable = mock(FindIterable.class);
    var exception = mock(MongoWriteException.class);
    var error = mock(WriteError.class);
    when(findIterable.first()).thenReturn(DOCUMENT);
    when(exception.getError()).thenReturn(error);
    when(error.getCategory()).thenReturn(ErrorCategory.EXECUTION_TIMEOUT);
    doReturn(findIterable).when(collection).find(any(Bson.class));
    doThrow(exception).when(collection).updateOne(any(Bson.class), any(Bson.class));
    MongoDbUsersDao usersDao = new MongoDbUsersDao(collection, MAPPER);
    CompletionException e = assertThrows(CompletionException.class, () -> usersDao.update(null, USER).join());
    assertTrue(e.getCause() instanceof DatabaseException);
    var exp = (DatabaseException) e.getCause();
    assertEquals(DatabaseException.Error.DATABASE_DOWN, exp.getError());
    verify(collection, times(1)).find(any(Bson.class));
    verify(collection, times(1)).updateOne(argThat((Bson bson) -> {
        BsonDocument doc = bson.toBsonDocument(BsonDocument.class, MongoClientSettings.getDefaultCodecRegistry());
        return doc.containsKey("version");
    }), any(Bson.class));
}
Also used : BsonDocument(org.bson.BsonDocument) CompletionException(java.util.concurrent.CompletionException) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) DatabaseException(com.sanctionco.thunder.dao.DatabaseException) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.Test)

Example 8 with DatabaseException

use of com.sanctionco.thunder.dao.DatabaseException in project thunder by RohanNagar.

the class MongoDbUsersDaoTest method testFindByEmailUnknownException.

@Test
void testFindByEmailUnknownException() {
    MongoCollection<Document> collection = mock(MongoCollection.class);
    MongoDbUsersDao usersDao = new MongoDbUsersDao(collection, MAPPER);
    doThrow(new IllegalStateException()).when(collection).find(any(Bson.class));
    CompletionException e = assertThrows(CompletionException.class, () -> usersDao.findByEmail("test@test.com").join());
    assertTrue(e.getCause() instanceof DatabaseException);
    var exp = (DatabaseException) e.getCause();
    assertEquals(DatabaseException.Error.DATABASE_DOWN, exp.getError());
    verify(collection, times(1)).find(eq(Filters.eq("_id", "test@test.com")));
}
Also used : CompletionException(java.util.concurrent.CompletionException) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) DatabaseException(com.sanctionco.thunder.dao.DatabaseException) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.Test)

Example 9 with DatabaseException

use of com.sanctionco.thunder.dao.DatabaseException in project thunder by RohanNagar.

the class MongoDbUsersDaoTest method testUnsuccessfulDeleteDatabaseDown.

@Test
void testUnsuccessfulDeleteDatabaseDown() {
    MongoCollection<Document> collection = mock(MongoCollection.class);
    FindIterable<Document> findIterable = mock(FindIterable.class);
    when(findIterable.first()).thenReturn(DOCUMENT);
    doReturn(findIterable).when(collection).find(any(Bson.class));
    doThrow(MongoTimeoutException.class).when(collection).deleteOne(any(Bson.class));
    MongoDbUsersDao usersDao = new MongoDbUsersDao(collection, MAPPER);
    CompletionException e = assertThrows(CompletionException.class, () -> usersDao.delete("test@test.com").join());
    assertTrue(e.getCause() instanceof DatabaseException);
    var exp = (DatabaseException) e.getCause();
    assertEquals(DatabaseException.Error.DATABASE_DOWN, exp.getError());
    verify(collection, times(1)).find(eq(Filters.eq("_id", "test@test.com")));
    verify(collection, times(1)).deleteOne(eq(Filters.eq("_id", "test@test.com")));
}
Also used : CompletionException(java.util.concurrent.CompletionException) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) DatabaseException(com.sanctionco.thunder.dao.DatabaseException) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.Test)

Example 10 with DatabaseException

use of com.sanctionco.thunder.dao.DatabaseException in project thunder by RohanNagar.

the class DynamoDbUsersDao method findByEmail.

@Override
public CompletableFuture<User> findByEmail(String email) {
    Objects.requireNonNull(email);
    GetItemRequest request = GetItemRequest.builder().tableName(tableName).key(Collections.singletonMap("email", AttributeValue.builder().s(email).build())).build();
    return dynamoDbClient.getItem(request).thenApply(response -> {
        if (response.item().size() <= 0) {
            LOG.warn("The email {} was not found in the database.", email);
            throw new DatabaseException("User not found in the database.", DatabaseException.Error.USER_NOT_FOUND);
        }
        return UsersDao.fromJson(mapper, response.item().get("document").s()).withTime(Long.parseLong(response.item().get("creation_time").n()), Long.parseLong(response.item().get("update_time").n()));
    }).exceptionally(throwable -> {
        throw convertToDatabaseException(throwable.getCause(), email);
    });
}
Also used : GetItemRequest(software.amazon.awssdk.services.dynamodb.model.GetItemRequest) LoggerFactory(org.slf4j.LoggerFactory) DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) SdkException(software.amazon.awssdk.core.exception.SdkException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ComparisonOperator(software.amazon.awssdk.services.dynamodb.model.ComparisonOperator) Map(java.util.Map) ExpectedAttributeValue(software.amazon.awssdk.services.dynamodb.model.ExpectedAttributeValue) ConditionalCheckFailedException(software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException) Nullable(javax.annotation.Nullable) AwsServiceException(software.amazon.awssdk.awscore.exception.AwsServiceException) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) UUID(java.util.UUID) Instant(java.time.Instant) DatabaseException(com.sanctionco.thunder.dao.DatabaseException) Objects(java.util.Objects) PutItemRequest(software.amazon.awssdk.services.dynamodb.model.PutItemRequest) User(com.sanctionco.thunder.models.User) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) DeleteItemRequest(software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest) Collections(java.util.Collections) UsersDao(com.sanctionco.thunder.dao.UsersDao) ReturnValue(software.amazon.awssdk.services.dynamodb.model.ReturnValue) DatabaseException(com.sanctionco.thunder.dao.DatabaseException) GetItemRequest(software.amazon.awssdk.services.dynamodb.model.GetItemRequest)

Aggregations

DatabaseException (com.sanctionco.thunder.dao.DatabaseException)29 Test (org.junit.jupiter.api.Test)25 CompletionException (java.util.concurrent.CompletionException)24 BsonDocument (org.bson.BsonDocument)24 Document (org.bson.Document)24 Bson (org.bson.conversions.Bson)18 MongoCommandException (com.mongodb.MongoCommandException)5 User (com.sanctionco.thunder.models.User)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 UsersDao (com.sanctionco.thunder.dao.UsersDao)3 Instant (java.time.Instant)3 Collections (java.util.Collections)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Objects (java.util.Objects)3 UUID (java.util.UUID)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Nullable (javax.annotation.Nullable)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3