### Question:
OriginCreateDelegate implements Delegate { public Origin handle(CreateOriginRequest request) throws AccessDeniedException, AuthenticationFailedException, InvalidArgumentException { if (request.getOrganization() != null) { assertOrganizationExists(request); securityContext.checkPermission(TiFunctionConstants.addThreatIntelOrigin, request.getOrganization()); } else { securityContext.checkPermission(TiFunctionConstants.addThreatIntelOrigin); } assertOriginNotExists(request); OriginEntity entity = new OriginEntity() .setId(UUID.randomUUID()) .setNamespaceID(GLOBAL_NAMESPACE) .setOrganizationID(request.getOrganization()) .setName(request.getName()) .setDescription(request.getDescription()) .setTrust(request.getTrust()) .setType(OriginEntity.Type.Group); entity = originManager.saveOrigin(entity); return originResponseConverter.apply(entity); } @Inject  OriginCreateDelegate(TiSecurityContext securityContext,
                              OriginManager originManager,
                              OriginResponseConverter originResponseConverter); Origin handle(CreateOriginRequest request);  }### Answer:
@Test(expected = AccessDeniedException.class) public void testCreateOriginWithoutGeneralAddPermission() throws Exception { doThrow(AccessDeniedException.class).when(securityContext).checkPermission(TiFunctionConstants.addThreatIntelOrigin); delegate.handle(new CreateOriginRequest()); }
@Test(expected = AccessDeniedException.class) public void testCreateOriginWithoutSpecificAddPermission() throws Exception { CreateOriginRequest request = new CreateOriginRequest().setOrganization(UUID.randomUUID()); when(securityContext.getAvailableOrganizationID()).thenReturn(Collections.singleton(request.getOrganization())); doThrow(AccessDeniedException.class).when(securityContext).checkPermission(TiFunctionConstants.addThreatIntelOrigin, request.getOrganization()); delegate.handle(request); }
@Test(expected = InvalidArgumentException.class) public void testCreateOriginWithNonExistingOrganization() throws Exception { CreateOriginRequest request = new CreateOriginRequest().setOrganization(UUID.randomUUID()); delegate.handle(request); }
@Test(expected = InvalidArgumentException.class) public void testCreateOriginWithSameNameExists() throws Exception { CreateOriginRequest request = new CreateOriginRequest().setName("name"); when(originManager.getOrigin(request.getName())).thenReturn(new OriginEntity()); delegate.handle(request); }
@Test public void testCreateOriginWithAllFields() throws Exception { CreateOriginRequest request = new CreateOriginRequest() .setOrganization(UUID.randomUUID()) .setName("name") .setDescription("description") .setTrust(0.1f); when(securityContext.getAvailableOrganizationID()).thenReturn(Collections.singleton(request.getOrganization())); when(originManager.saveOrigin(notNull())).thenReturn(new OriginEntity()); delegate.handle(request); verify(securityContext).checkPermission(TiFunctionConstants.addThreatIntelOrigin, request.getOrganization()); verify(originResponseConverter).apply(notNull()); verify(originManager).saveOrigin(argThat(entity -> { assertNotNull(entity.getId()); assertNotNull(entity.getNamespaceID()); assertEquals(request.getOrganization(), entity.getOrganizationID()); assertEquals(request.getName(), entity.getName()); assertEquals(request.getDescription(), entity.getDescription()); assertEquals(request.getTrust(), entity.getTrust(), 0.0); assertEquals(OriginEntity.Type.Group, entity.getType()); return true; })); }
@Test public void testCreateOriginWithDefaultFields() throws Exception { CreateOriginRequest request = new CreateOriginRequest() .setName("name"); when(originManager.saveOrigin(notNull())).thenReturn(new OriginEntity()); delegate.handle(request); verify(securityContext).checkPermission(TiFunctionConstants.addThreatIntelOrigin); verify(originResponseConverter).apply(notNull()); verify(originManager).saveOrigin(argThat(entity -> { assertNotNull(entity.getId()); assertNotNull(entity.getNamespaceID()); assertNull(entity.getOrganizationID()); assertNull(entity.getDescription()); assertEquals(request.getName(), entity.getName()); assertEquals(0.8f, entity.getTrust(), 0.0); assertEquals(OriginEntity.Type.Group, entity.getType()); return true; })); }