### Question:
CoreTokenResource implements CollectionResourceProvider { public Promise<ResourceResponse, ResourceException> readInstance(Context serverContext, String tokenId, ReadRequest readRequest) { String principal = PrincipalRestUtils.getPrincipalNameFromServerContext(serverContext); try { Token token = store.read(tokenId); if (token == null) { error("READ by {0}: No token resource to read with ID: {1}", principal, tokenId); return generateNotFoundException(tokenId).asPromise(); } String json = serialisation.serialise(token); ResourceResponse response = newResourceResponse( tokenId, String.valueOf(System.currentTimeMillis()), JsonValueBuilder.toJsonValue(json)); debug("READ by {0}: Read token resource with ID: {1}", principal, tokenId); return newResultPromise(response); } catch (CoreTokenException e) { error(e, "READ by {0}: Error reading token resource with ID: {1}", principal, tokenId); return generateException(e).asPromise(); } } CoreTokenResource(JSONSerialisation serialisation, CTSPersistentStore store, Debug debug); Promise<ResourceResponse, ResourceException> createInstance(Context serverContext,
            CreateRequest createRequest); Promise<ResourceResponse, ResourceException> deleteInstance(Context serverContext, String tokenId,
            DeleteRequest deleteRequest); Promise<ResourceResponse, ResourceException> readInstance(Context serverContext, String tokenId,
            ReadRequest readRequest); Promise<ResourceResponse, ResourceException> updateInstance(Context serverContext, String tokenId,
            UpdateRequest updateRequest); Promise<QueryResponse, ResourceException> queryCollection(Context serverContext,
            QueryRequest queryRequest, QueryResourceHandler queryResultHandler); Promise<ResourceResponse, ResourceException> patchInstance(Context serverContext, String s,
            PatchRequest patchRequest); Promise<ActionResponse, ResourceException> actionInstance(Context serverContext, String s,
            ActionRequest actionRequest); Promise<ActionResponse, ResourceException> actionCollection(Context serverContext,
            ActionRequest actionRequest); static final String DEBUG_HEADER; }### Answer:
@Test public void shouldReadTokenFromStore() throws CoreTokenException { String one = "badger"; given(mockStore.read(anyString())).willReturn(mockToken); given(mockSerialisation.serialise(any())).willReturn("{ \"value\": \"some JSON\" }"); resource.readInstance(null, one, mock(ReadRequest.class)); verify(mockStore).read(one); }
@Test public void shouldReadAndReturnTokenInSerialisedForm() throws CoreTokenException { String serialisedToken = "{ \"value\": \"some JSON\" }"; given(mockStore.read(anyString())).willReturn(mockToken); given(mockSerialisation.serialise(any(Token.class))).willReturn(serialisedToken); Promise<ResourceResponse, ResourceException> promise = resource.readInstance(null, "", mock(ReadRequest.class)); assertThat(promise).succeeded().withContent().stringAt("value").isEqualTo("some JSON"); }
@Test public void shouldIndicateWhenNoTokenCanBeRead() throws CoreTokenException { given(mockStore.read(anyString())).willReturn(null); Promise<ResourceResponse, ResourceException> promise = resource.readInstance(null, "badger", mock(ReadRequest.class)); assertThat(promise).failedWithException(); try { promise.getOrThrowUninterruptibly(); fail(); } catch (ResourceException e) { Assert.assertEquals(e.getCode(), ResourceException.NOT_FOUND); } }