From defb05c3bc2690bc0de3d94d9023ef95267dba2c Mon Sep 17 00:00:00 2001 From: Varsha Chandrashekar Date: Fri, 29 Mar 2019 14:38:23 -0400 Subject: [PATCH] Delete Group - Delete the Group from DynamoDB (#548) --- ...namoDBGroupRepositoryIntegrationSpec.scala | 25 +++++++++++++++++++ .../repository/DynamoDBGroupRepository.scala | 8 +++++- .../DynamoDBGroupRepositorySpec.scala | 13 ++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/modules/dynamodb/src/it/scala/vinyldns/dynamodb/repository/DynamoDBGroupRepositoryIntegrationSpec.scala b/modules/dynamodb/src/it/scala/vinyldns/dynamodb/repository/DynamoDBGroupRepositoryIntegrationSpec.scala index 51c9c335e..463523c50 100644 --- a/modules/dynamodb/src/it/scala/vinyldns/dynamodb/repository/DynamoDBGroupRepositoryIntegrationSpec.scala +++ b/modules/dynamodb/src/it/scala/vinyldns/dynamodb/repository/DynamoDBGroupRepositoryIntegrationSpec.scala @@ -165,5 +165,30 @@ class DynamoDBGroupRepositoryIntegrationSpec extends DynamoDBIntegrationSpec { test.unsafeRunSync().get.description shouldBe None } + + "add and delete a group should return successfully" in { + val deleted = deletedGroup.copy( + id = "test-deleted-group-get-groups", + memberIds = Set("foo"), + adminUserIds = Set("foo")) + val f = + for { + _ <- repo.save(deleted) + retrieved <- repo.delete(deleted) + } yield retrieved + + f.unsafeRunSync().id shouldBe deleted.id + + val getAfterDeleted = + for { + get <- repo.getGroup("test-deleted-group-get-groups") + getAll <- repo.getAllGroups() + } yield (get, getAll) + + val (get, getAll) = getAfterDeleted.unsafeRunSync() + get shouldBe None + getAll.filter(_.id == "test-deleted-group-get-groups") shouldBe Set.empty + getAll.filter(_.id == activeGroups.head.id) shouldBe Set(activeGroups.head) + } } } diff --git a/modules/dynamodb/src/main/scala/vinyldns/dynamodb/repository/DynamoDBGroupRepository.scala b/modules/dynamodb/src/main/scala/vinyldns/dynamodb/repository/DynamoDBGroupRepository.scala index af0566181..82e5cf1b5 100644 --- a/modules/dynamodb/src/main/scala/vinyldns/dynamodb/repository/DynamoDBGroupRepository.scala +++ b/modules/dynamodb/src/main/scala/vinyldns/dynamodb/repository/DynamoDBGroupRepository.scala @@ -99,7 +99,13 @@ class DynamoDBGroupRepository private[repository] ( } def delete(group: Group): IO[Group] = - save(group.copy(status = GroupStatus.Deleted)) + monitor("repo.Group.delete") { + log.info(s"Deleting group ${group.id} ${group.name}.") + val key = new HashMap[String, AttributeValue]() + key.put(GROUP_ID, new AttributeValue(group.id)) + val request = new DeleteItemRequest().withTableName(groupTableName).withKey(key) + dynamoDBHelper.deleteItem(request).map(_ => group) + } /*Looks up a group. If the group is not found, or if the group's status is Deleted, will return None */ def getGroup(groupId: String): IO[Option[Group]] = diff --git a/modules/dynamodb/src/test/scala/vinyldns/dynamodb/repository/DynamoDBGroupRepositorySpec.scala b/modules/dynamodb/src/test/scala/vinyldns/dynamodb/repository/DynamoDBGroupRepositorySpec.scala index 1f547e8e5..3c1e296ff 100644 --- a/modules/dynamodb/src/test/scala/vinyldns/dynamodb/repository/DynamoDBGroupRepositorySpec.scala +++ b/modules/dynamodb/src/test/scala/vinyldns/dynamodb/repository/DynamoDBGroupRepositorySpec.scala @@ -268,4 +268,17 @@ class DynamoDBGroupRepositorySpec a[ResourceNotFoundException] shouldBe thrownBy(result.unsafeRunSync()) } } + + "DynamoDBGroupRepository.delete" should { + "return a deleted group on delete" in { + val mockDeleteItemRequest = mock[DeleteItemResult] + + doReturn(IO.pure(mockDeleteItemRequest)) + .when(dynamoDBHelper) + .deleteItem(any[DeleteItemRequest]) + + val response = underTest.delete(okGroup).unsafeRunSync() + response shouldBe okGroup + } + } }