### Question:
HttpByteArrayCacheEntrySerializer implements HttpCacheEntrySerializer<byte[]> { @Override public byte[] serialize(final HttpCacheStorageEntry httpCacheEntry) throws ResourceIOException { if (httpCacheEntry.getKey() == null) { throw new IllegalStateException("Cannot serialize cache object with null storage key"); } final HttpRequest httpRequest = new BasicHttpRequest(httpCacheEntry.getContent().getRequestMethod(), "/"); final CacheValidityPolicy cacheValidityPolicy = new NoAgeCacheValidityPolicy(); final CachedHttpResponseGenerator cachedHttpResponseGenerator = new CachedHttpResponseGenerator(cacheValidityPolicy); final SimpleHttpResponse httpResponse = cachedHttpResponseGenerator.generateResponse(httpRequest, httpCacheEntry.getContent()); try(final ByteArrayOutputStream out = new ByteArrayOutputStream()) { escapeHeaders(httpResponse); addMetadataPseudoHeaders(httpResponse, httpCacheEntry); final byte[] bodyBytes = httpResponse.getBodyBytes(); final int resourceLength; if (bodyBytes == null) { httpResponse.addHeader(SC_HEADER_NAME_NO_CONTENT, Boolean.TRUE.toString()); resourceLength = 0; } else { resourceLength = bodyBytes.length; } final SessionOutputBufferImpl outputBuffer = new SessionOutputBufferImpl(BUFFER_SIZE); final AbstractMessageWriter<SimpleHttpResponse> httpResponseWriter = makeHttpResponseWriter(outputBuffer); httpResponseWriter.write(httpResponse, outputBuffer, out); outputBuffer.flush(out); final byte[] headerBytes = out.toByteArray(); final byte[] bytes = new byte[headerBytes.length + resourceLength]; System.arraycopy(headerBytes, 0, bytes, 0, headerBytes.length); if (resourceLength > 0) { System.arraycopy(bodyBytes, 0, bytes, headerBytes.length, resourceLength); } return bytes; } catch(final IOException|HttpException e) { throw new ResourceIOException("Exception while serializing cache entry", e); } } HttpByteArrayCacheEntrySerializer(); @Override byte[] serialize(final HttpCacheStorageEntry httpCacheEntry); @Override HttpCacheStorageEntry deserialize(final byte[] serializedObject); static final HttpByteArrayCacheEntrySerializer INSTANCE; }### Answer:
@Test(expected = IllegalStateException.class) public void testNullStorageKey() throws Exception { final HttpCacheStorageEntryTestTemplate cacheObjectValues = HttpCacheStorageEntryTestTemplate.makeDefault(); cacheObjectValues.storageKey = null; final HttpCacheStorageEntry testEntry = cacheObjectValues.toEntry(); serializer.serialize(testEntry); }
@Test(expected = ResourceIOException.class) public void testSerializeWithHTTPException() throws Exception { final AbstractMessageWriter<SimpleHttpResponse> throwyHttpWriter = Mockito.mock(AbstractMessageWriter.class); Mockito. doThrow(new HttpException("Test Exception")). when(throwyHttpWriter). write(Mockito.any(SimpleHttpResponse.class), Mockito.any(SessionOutputBuffer.class), Mockito.any(OutputStream.class)); final HttpCacheStorageEntryTestTemplate cacheObjectValues = HttpCacheStorageEntryTestTemplate.makeDefault(); final HttpCacheStorageEntry testEntry = cacheObjectValues.toEntry(); final HttpByteArrayCacheEntrySerializer testSerializer = new HttpByteArrayCacheEntrySerializer() { protected AbstractMessageWriter<SimpleHttpResponse> makeHttpResponseWriter(final SessionOutputBuffer outputBuffer) { return throwyHttpWriter; } }; testSerializer.serialize(testEntry); }