### Question:
ApplicationTypesResource extends SubjectAwareResource { @Override public Promise<ResourceResponse, ResourceException> readInstance(Context context, String resourceId, ReadRequest request) { final Subject mySubject = getContextSubject(context); if (mySubject == null) { debug.error("ApplicationsTypesResource :: READ : Unknown Subject"); return new InternalServerErrorException().asPromise(); } final String principalName = PrincipalRestUtils.getPrincipalNameFromSubject(mySubject); final ApplicationType applType = typeManager.getApplicationType(mySubject, resourceId); final ApplicationTypeWrapper wrap = new ApplicationTypeWrapper(applType); if (applType == null) { if (debug.errorEnabled()) { debug.error("ApplicationTypesResource :: READ by " + principalName + ": Requested application type short name not found: " + resourceId); } return new NotFoundException().asPromise(); } try { final ResourceResponse resource = newResourceResponse(resourceId, String.valueOf(System.currentTimeMillis()), JsonValue.json(wrap.toJsonValue())); return newResultPromise(resource); } catch (IOException e) { if (debug.errorEnabled()) { debug.error("ApplicationTypesResource :: READ by " + principalName + ": Could not jsonify class associated with defined Type: " + resourceId, e); } return new InternalServerErrorException().asPromise(); } } @Inject  ApplicationTypesResource(final ApplicationTypeManagerWrapper typeManager, @Named("frRest") Debug debug); @Override Promise<ActionResponse, ResourceException> actionCollection(Context context, ActionRequest request); @Override Promise<ActionResponse, ResourceException> actionInstance(Context context, String resourceId,
            ActionRequest request); @Override Promise<ResourceResponse, ResourceException> createInstance(Context context, CreateRequest request); @Override Promise<ResourceResponse, ResourceException> deleteInstance(Context context, String resourceId,
            DeleteRequest request); @Override Promise<ResourceResponse, ResourceException> patchInstance(Context context, String resourceId,
            PatchRequest request); @Override Promise<ResourceResponse, ResourceException> updateInstance(Context context, String resourceId,
            UpdateRequest request); @Override Promise<QueryResponse, ResourceException> queryCollection(Context context, QueryRequest request,
            QueryResourceHandler handler); @Override Promise<ResourceResponse, ResourceException> readInstance(Context context, String resourceId,
            ReadRequest request);  }### Answer:
@Test (expectedExceptions = InternalServerErrorException.class) public void undefinedSubjectShouldFail() throws ResourceException { SSOTokenContext mockSubjectContext = mock(SSOTokenContext.class); RealmContext realmContext = new RealmContext(mockSubjectContext); Context mockServerContext = ClientContext.newInternalClientContext(realmContext); Subject subject = null; given(mockSubjectContext.getCallerSubject()).willReturn(subject); ReadRequest request = mock(ReadRequest.class); Promise<ResourceResponse, ResourceException> result = testResource.readInstance(mockServerContext, "test", request); result.getOrThrowUninterruptibly(); }
@Test (expectedExceptions = NotFoundException.class) public void readShouldFailOnInvalidApplicationType() throws ResourceException { SSOTokenContext mockSubjectContext = mock(SSOTokenContext.class); RealmContext realmContext = new RealmContext(mockSubjectContext); Context mockServerContext = ClientContext.newInternalClientContext(realmContext); Subject subject = new Subject(); given(mockSubjectContext.getCallerSubject()).willReturn(subject); ReadRequest request = mock(ReadRequest.class); Promise<ResourceResponse, ResourceException> result = testResource.readInstance(mockServerContext, "test", request); result.getOrThrowUninterruptibly(); }
@Test public void shouldReadInstanceCorrectly() throws IllegalAccessException, InstantiationException, ExecutionException, InterruptedException { SSOTokenContext mockSubjectContext = mock(SSOTokenContext.class); RealmContext realmContext = new RealmContext(mockSubjectContext); Context mockServerContext = ClientContext.newInternalClientContext(realmContext); Subject subject = new Subject(); given(mockSubjectContext.getCallerSubject()).willReturn(subject); ReadRequest request = mock(ReadRequest.class); ApplicationType mockApplicationType = new ApplicationType("test", null, null, null, null); given(mockApplicationTypeManager.getApplicationType(subject, "test")).willReturn(mockApplicationType); Promise<ResourceResponse, ResourceException> result = testResource.readInstance(mockServerContext, "test", request); assertTrue(result.get().getId().equals("test")); }