### Question:
OriginDeleteDelegate implements Delegate { public Origin handle(DeleteOriginRequest request) throws AccessDeniedException, AuthenticationFailedException, InvalidArgumentException, ObjectNotFoundException { OriginEntity entity = fetchExistingOrigin(request); securityContext.checkReadPermission(entity); checkDeletePermission(entity); assertNotDeleted(entity); entity.addFlag(OriginEntity.Flag.Deleted); entity = originManager.saveOrigin(entity); return originResponseConverter.apply(entity); } @Inject  OriginDeleteDelegate(TiSecurityContext securityContext,
                              OriginManager originManager,
                              OriginResolver originResolver,
                              OriginResponseConverter originResponseConverter); Origin handle(DeleteOriginRequest request);  }### Answer:
@Test(expected = ObjectNotFoundException.class) public void testDeleteOriginNotExistingOrigin() throws Exception { delegate.handle(new DeleteOriginRequest().setId(UUID.randomUUID())); }
@Test(expected = AccessDeniedException.class) public void testDeleteOriginNoReadPermission() throws Exception { OriginEntity entity = new OriginEntity().setId(UUID.randomUUID()); when(originResolver.apply(entity.getId())).thenReturn(entity); doThrow(AccessDeniedException.class).when(securityContext).checkReadPermission(entity); delegate.handle(new DeleteOriginRequest().setId(entity.getId())); }
@Test(expected = AccessDeniedException.class) public void testDeleteOriginNoDeletePermissionWithoutOrganization() throws Exception { OriginEntity entity = new OriginEntity().setId(UUID.randomUUID()); when(originResolver.apply(entity.getId())).thenReturn(entity); doThrow(AccessDeniedException.class).when(securityContext).checkPermission(TiFunctionConstants.deleteThreatIntelOrigin); delegate.handle(new DeleteOriginRequest().setId(entity.getId())); }
@Test(expected = AccessDeniedException.class) public void testDeleteOriginNoDeletePermissionWithOrganization() throws Exception { OriginEntity entity = new OriginEntity() .setId(UUID.randomUUID()) .setOrganizationID(UUID.randomUUID()); when(originResolver.apply(entity.getId())).thenReturn(entity); doThrow(AccessDeniedException.class).when(securityContext).checkPermission(TiFunctionConstants.deleteThreatIntelOrigin, entity.getOrganizationID()); delegate.handle(new DeleteOriginRequest().setId(entity.getId())); }
@Test(expected = InvalidArgumentException.class) public void testDeleteOriginAlreadyDeleted() throws Exception { OriginEntity entity = new OriginEntity() .setId(UUID.randomUUID()) .addFlag(OriginEntity.Flag.Deleted); when(originResolver.apply(entity.getId())).thenReturn(entity); delegate.handle(new DeleteOriginRequest().setId(entity.getId())); }
@Test public void testDeleteOriginMarksAsDeleted() throws Exception { OriginEntity entity = new OriginEntity() .setId(UUID.randomUUID()); when(originResolver.apply(entity.getId())).thenReturn(entity); when(originManager.saveOrigin(entity)).thenReturn(entity); delegate.handle(new DeleteOriginRequest().setId(entity.getId())); verify(originManager).saveOrigin(argThat(origin -> origin.getFlags().contains(OriginEntity.Flag.Deleted))); verify(originResponseConverter).apply(entity); }