### Question:
HCRequestFactory implements RequestFactory<HttpUriRequest> { public HttpUriRequest create(String url, Request request) throws IOException { HttpUriRequest httpUriRequest; if (request.getHttpMethodName().equalsIgnoreCase("POST")) { httpUriRequest = new HttpPost(url); } else if (request.getHttpMethodName().equalsIgnoreCase("GET")) { httpUriRequest = new HttpGet(url); } else if (request.getHttpMethodName().equalsIgnoreCase("PUT")) { httpUriRequest = new HttpPut(url); } else if (request.getHttpMethodName().equalsIgnoreCase("HEAD")) { httpUriRequest = new HttpHead(url); } else { throw new UnsupportedOperationException(request.getHttpMethodName()); } if (httpUriRequest instanceof HttpEntityEnclosingRequest) { ((HttpEntityEnclosingRequest)httpUriRequest).setEntity(createHttpEntity(request)); } return httpUriRequest; }  HttpUriRequest create(String url, Request request);  }### Answer:
@Test public void throwsOnUnknownHttpMethodName() throws IOException, URISyntaxException { HCRequestFactory factory = createDefaultTestObject(); String expectedUrl = UUID.randomUUID().toString(); String httpMethodName = UUID.randomUUID().toString(); Request request = createDefaultMockRequest(expectedUrl, httpMethodName); expectedException.expect(UnsupportedOperationException.class); expectedException.expectMessage(httpMethodName); HttpUriRequest result = factory.create(expectedUrl, request); assertTrue(result instanceof HttpPost); assertEquals(result.getURI(), new URI(expectedUrl)); }
@Test public void createsPostRequest() throws IOException, URISyntaxException { HCRequestFactory factory = createDefaultTestObject(); String expectedUrl = UUID.randomUUID().toString(); Request request = createDefaultMockRequest(expectedUrl, "POST"); HttpUriRequest result = factory.create(expectedUrl, request); assertTrue(result instanceof HttpPost); assertEquals(result.getURI(), new URI(expectedUrl)); }
@Test public void createsPutRequest() throws IOException, URISyntaxException { HCRequestFactory factory = createDefaultTestObject(); String expectedUrl = UUID.randomUUID().toString(); Request request = createDefaultMockRequest(expectedUrl, "PUT"); HttpUriRequest result = factory.create(expectedUrl, request); assertTrue(result instanceof HttpPut); assertEquals(result.getURI(), new URI(expectedUrl)); }
@Test public void createsHeadRequest() throws IOException, URISyntaxException { HCRequestFactory factory = createDefaultTestObject(); String expectedUrl = UUID.randomUUID().toString(); Request request = createDefaultMockRequest(expectedUrl, "HEAD"); HttpUriRequest result = factory.create(expectedUrl, request); assertTrue(result instanceof HttpHead); assertEquals(result.getURI(), new URI(expectedUrl)); }
@Test public void createsGetRequest() throws IOException, URISyntaxException { HCRequestFactory factory = createDefaultTestObject(); String expectedUrl = UUID.randomUUID().toString(); Request request = createDefaultMockRequest(expectedUrl, "GET"); HttpUriRequest result = factory.create(expectedUrl, request); assertTrue(result instanceof HttpGet); assertEquals(result.getURI(), new URI(expectedUrl)); }
@Test public void createsEntityUsingGivenSource() throws IOException { HCRequestFactory factory = createDefaultTestObject(); String expectedUrl = UUID.randomUUID().toString(); Request request = createDefaultMockRequest(expectedUrl, "POST"); ByteBuf byteBuf = new CompositeByteBuf(UnpooledByteBufAllocator.DEFAULT, false, 2); byte[] expectedBytes = UUID.randomUUID().toString().getBytes(); byteBuf.writeBytes(expectedBytes); ItemSource<ByteBuf> itemSource = mock(ItemSource.class); when(itemSource.getSource()).thenReturn(byteBuf); when(request.serialize()).thenReturn(itemSource); HttpEntityEnclosingRequest result = (HttpEntityEnclosingRequest) factory.create(expectedUrl, request); assertEquals(expectedBytes.length, result.getEntity().getContentLength()); ByteBuf source = (ByteBuf) request.serialize().getSource(); source.readBytes(new byte[source.writerIndex()]); InputStream inputStream = result.getEntity().getContent(); assertEquals(0, inputStream.available()); }