### Question:
ObjectGetDelegate implements Delegate { public Object handle(GetObjectByIdRequest request) throws AccessDeniedException, AuthenticationFailedException, InvalidArgumentException { securityContext.checkPermission(TiFunctionConstants.viewThreatIntelFact); ObjectRecord object = objectFactDao.getObject(request.getId()); securityContext.checkReadPermission(object); return createObjectConverter().apply(object); } @Inject  ObjectGetDelegate(TiSecurityContext securityContext,
                           ObjectFactDao objectFactDao,
                           FactTypeByIdResponseResolver factTypeConverter,
                           ObjectTypeByIdResponseResolver objectTypeConverter,
                           ObjectTypeHandler objectTypeHandler); Object handle(GetObjectByIdRequest request); Object handle(GetObjectByTypeValueRequest request);  }### Answer:
@Test(expected = AccessDeniedException.class) public void testFetchObjectByIdWithoutViewPermission() throws Exception { doThrow(AccessDeniedException.class).when(securityContext).checkPermission(TiFunctionConstants.viewThreatIntelFact); delegate.handle(new GetObjectByIdRequest()); }
@Test public void testFetchNonExistingObjectById() throws Exception { GetObjectByIdRequest request = new GetObjectByIdRequest().setId(UUID.randomUUID()); doThrow(AccessDeniedException.class).when(securityContext).checkReadPermission((ObjectRecord) isNull()); try { delegate.handle(request); fail(); } catch (AccessDeniedException ignored) { verify(objectFactDao).getObject(request.getId()); verifyNoMoreInteractions(objectFactDao); } }
@Test public void testFetchObjectById() throws Exception { ObjectRecord object = new ObjectRecord().setId(UUID.randomUUID()); when(objectFactDao.getObject(object.getId())).thenReturn(object); assertNotNull(delegate.handle(new GetObjectByIdRequest().setId(object.getId()))); verify(securityContext).checkReadPermission(object); verify(objectFactDao).calculateObjectStatistics(argThat(criteria -> { assertEquals(Collections.singleton(object.getId()), criteria.getObjectID()); assertNotNull(criteria.getCurrentUserID()); assertNotNull(criteria.getAvailableOrganizationID()); return true; })); }
@Test(expected = AccessDeniedException.class) public void testFetchObjectByTypeValueWithoutViewPermission() throws Exception { doThrow(AccessDeniedException.class).when(securityContext).checkPermission(TiFunctionConstants.viewThreatIntelFact); delegate.handle(new GetObjectByTypeValueRequest()); }
@Test public void testFetchObjectByTypeValueWithNonExistingObjectType() throws Exception { GetObjectByTypeValueRequest request = new GetObjectByTypeValueRequest().setType("type").setValue("value"); doThrow(InvalidArgumentException.class).when(objectTypeHandler).assertObjectTypeExists(request.getType(), "type"); try { delegate.handle(request); fail(); } catch (InvalidArgumentException ignored) { verifyNoInteractions(objectFactDao); } }
@Test public void testFetchNonExistingObjectByTypeValue() throws Exception { GetObjectByTypeValueRequest request = new GetObjectByTypeValueRequest().setType("type").setValue("value"); doThrow(AccessDeniedException.class).when(securityContext).checkReadPermission((ObjectRecord) isNull()); try { delegate.handle(request); fail(); } catch (AccessDeniedException ignored) { verify(objectFactDao).getObject(request.getType(), request.getValue()); verifyNoMoreInteractions(objectFactDao); } }
@Test public void testFetchObjectByTypeValue() throws Exception { GetObjectByTypeValueRequest request = new GetObjectByTypeValueRequest().setType("type").setValue("value"); ObjectRecord object = new ObjectRecord().setId(UUID.randomUUID()); when(objectFactDao.getObject(request.getType(), request.getValue())).thenReturn(object); assertNotNull(delegate.handle(request)); verify(securityContext).checkReadPermission(object); verify(objectFactDao).calculateObjectStatistics(argThat(criteria -> { assertEquals(Collections.singleton(object.getId()), criteria.getObjectID()); assertNotNull(criteria.getCurrentUserID()); assertNotNull(criteria.getAvailableOrganizationID()); return true; })); }