diff options
Diffstat (limited to 'cloudify-client/src/test/java/org/onap/so/cloudify/connector/http/HttpClientConnectorTest.java')
-rw-r--r-- | cloudify-client/src/test/java/org/onap/so/cloudify/connector/http/HttpClientConnectorTest.java | 399 |
1 files changed, 200 insertions, 199 deletions
diff --git a/cloudify-client/src/test/java/org/onap/so/cloudify/connector/http/HttpClientConnectorTest.java b/cloudify-client/src/test/java/org/onap/so/cloudify/connector/http/HttpClientConnectorTest.java index 4475fff093..c85b88f7f3 100644 --- a/cloudify-client/src/test/java/org/onap/so/cloudify/connector/http/HttpClientConnectorTest.java +++ b/cloudify-client/src/test/java/org/onap/so/cloudify/connector/http/HttpClientConnectorTest.java @@ -24,7 +24,6 @@ import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; - import com.github.tomakehurst.wiremock.http.Fault; import com.github.tomakehurst.wiremock.junit.WireMockRule; import static com.github.tomakehurst.wiremock.client.WireMock.get; @@ -43,11 +42,9 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.assertEquals; - import java.io.ByteArrayInputStream; import java.io.InputStream; import java.nio.charset.StandardCharsets; - import org.onap.so.cloudify.base.client.CloudifyConnectException; import org.onap.so.cloudify.base.client.CloudifyRequest; import org.onap.so.cloudify.base.client.CloudifyResponseException; @@ -56,199 +53,203 @@ import org.onap.so.cloudify.base.client.HttpMethod; import org.onap.so.cloudify.v3.model.Deployment; public class HttpClientConnectorTest { - - @Rule - public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort()); - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void sunnyDay_POST(){ - wireMockRule.stubFor(post(urlPathEqualTo("/testUrl")).willReturn(aResponse() - .withHeader("Content-Type", "application/json").withBody("TEST").withStatus(HttpStatus.SC_OK))); - int port = wireMockRule.port(); - HttpClientConnector conector = new HttpClientConnector(); - CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>(); - Deployment deployment = new Deployment(); - deployment.setId("id"); - request.entity(deployment, "application/json"); - request.endpoint("http://localhost:"+port+"/testUrl"); - request.setBasicAuthentication("USER","PASSWORD"); - request.header("Content-Type","application/json"); - request.method(HttpMethod.POST); - conector.request(request); - verify(postRequestedFor(urlEqualTo("/testUrl"))); - } - - - @Test - public void sunnyDay_GET(){ - wireMockRule.stubFor(get(urlPathEqualTo("/testUrl")).willReturn(aResponse() - .withHeader("Content-Type", "application/json").withBody("TEST").withStatus(HttpStatus.SC_OK))); - int port = wireMockRule.port(); - HttpClientConnector conector = new HttpClientConnector(); - CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>(); - request.endpoint("http://localhost:"+port+"/testUrl"); - request.setBasicAuthentication("USER","PASSWORD"); - request.method(HttpMethod.GET); - conector.request(request); - verify(getRequestedFor(urlEqualTo("/testUrl"))); - } - - @Test - public void sunnyDay_PUT(){ - wireMockRule.stubFor(put(urlPathEqualTo("/testUrl")).willReturn(aResponse() - .withHeader("Content-Type", "application/json").withBody("TEST").withStatus(HttpStatus.SC_OK))); - int port = wireMockRule.port(); - HttpClientConnector conector = new HttpClientConnector(); - CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>(); - request.endpoint("http://localhost:"+port+"/testUrl"); - request.setBasicAuthentication("USER","PASSWORD"); - request.method(HttpMethod.PUT); - conector.request(request); - verify(putRequestedFor(urlEqualTo("/testUrl"))); - } - - - @Test - public void sunnyDay_DELETE(){ - wireMockRule.stubFor(delete(urlPathEqualTo("/testUrl")).willReturn(aResponse() - .withHeader("Content-Type", "application/json").withBody("TEST").withStatus(HttpStatus.SC_OK))); - int port = wireMockRule.port(); - HttpClientConnector conector = new HttpClientConnector(); - CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>(); - request.endpoint("http://localhost:"+port+"/testUrl"); - request.setBasicAuthentication("USER","PASSWORD"); - request.method(HttpMethod.DELETE); - conector.request(request); - verify(deleteRequestedFor(urlEqualTo("/testUrl"))); - } - - - @Test - public void rainyDay_PATCH(){ - thrown.expect(HttpClientException.class); - thrown.expectMessage("Unrecognized HTTP Method: PATCH"); - HttpClientConnector conector = new HttpClientConnector(); - CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>(); - request.endpoint("http://localhost:123123/testUrl"); - request.setBasicAuthentication("USER","PASSWORD"); - request.method(HttpMethod.PATCH); - conector.request(request); - - } - - @Test - public void rainyDayRunTimeException(){ - wireMockRule.stubFor(post(urlEqualTo("/503")).willReturn( - aResponse().withStatus(503).withHeader("Content-Type", "text/plain").withBody("failure"))); - thrown.expect(RuntimeException.class); - thrown.expectMessage("Unexpected client exception"); - HttpClientConnector conector = new HttpClientConnector(); - CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>(); - request.endpoint("http://localhost:123123/503"); - request.setBasicAuthentication("USER","PASSWORD"); - request.method(HttpMethod.POST); - conector.request(request); - - } - - @Test - public void rainyDayBadUri() { - wireMockRule.stubFor(post(urlPathEqualTo("/testUrl")).willReturn(aResponse() - .withHeader("Content-Type", "application/json").withBody("TEST").withStatus(HttpStatus.SC_OK))); - thrown.expect(HttpClientException.class); - int port = wireMockRule.port(); - HttpClientConnector conector = new HttpClientConnector(); - CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>(); - Deployment deployment = new Deployment(); - deployment.setId("id"); - request.entity(deployment, "application/json"); - request.endpoint("(@#$@(#*$&asfasdf"); - request.setBasicAuthentication("USER","PASSWORD"); - request.header("Content-Type","application/json"); - request.method(HttpMethod.POST); - conector.request(request); - } - - @Test - public void sunnyDayWithJsonEntity_POST(){ - wireMockRule.stubFor(post(urlPathEqualTo("/testUrl")).willReturn(aResponse() - .withHeader("Content-Type", "application/json").withBody("TEST").withStatus(HttpStatus.SC_OK))); - int port = wireMockRule.port(); - HttpClientConnector conector = new HttpClientConnector(); - - Deployment deployment = new Deployment(); - deployment.setId("id"); - - CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>(null, HttpMethod.POST, "/", Entity.json(deployment), null); - - request.endpoint("http://localhost:"+port); - request.path("testUrl"); - request.header("Content-Type","application/json"); - request.header("Content-Type", null); - - request.returnType(Deployment.class); - assertEquals(Deployment.class, request.returnType()); - - Entity<Deployment> t = request.json(deployment); - assertEquals(t.getEntity().getId(), "id"); - - request.queryParam("test", "one").queryParam("test", "two"); - - conector.request(request); - - verify(postRequestedFor(urlEqualTo("/testUrl?test=two"))); - } - - @Test - public void sunnyDayWithStreamEntity_POST() { - wireMockRule.stubFor(post(urlPathEqualTo("/testUrl")).willReturn(aResponse() - .withHeader("Content-Type", "application/json").withBody("TEST").withStatus(HttpStatus.SC_OK))); - int port = wireMockRule.port(); - HttpClientConnector conector = new HttpClientConnector(); - - InputStream is = new ByteArrayInputStream("{}".getBytes(StandardCharsets.UTF_8)); - - CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>(null, HttpMethod.POST, "/testUrl", Entity.stream(is), null); - - request.endpoint("http://localhost:"+port); - request.setBasicAuthentication("USER","PASSWORD"); - request.header("Content-Type","application/json"); - - conector.request(request); - verify(postRequestedFor(urlEqualTo("/testUrl"))); - } - - @Test - public void rainyDayGarbageData(){ - wireMockRule.stubFor(get(urlPathEqualTo("/testUrl")).willReturn( - aResponse().withFault(Fault.RANDOM_DATA_THEN_CLOSE))); - thrown.expect(CloudifyConnectException.class); - int port = wireMockRule.port(); - HttpClientConnector conector = new HttpClientConnector(); - CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>(); - request.endpoint("http://localhost:"+port+"/testUrl"); - request.setBasicAuthentication("USER","PASSWORD"); - request.method(HttpMethod.GET); - conector.request(request); - } - - @Test - public void rainyDayEmptyResponse(){ - wireMockRule.stubFor(get(urlPathEqualTo("/testUrl")).willReturn(aResponse() - .withHeader("Content-Type", "application/json").withBody("TEST").withStatus(HttpStatus.SC_NOT_FOUND))); - - thrown.expect(HttpClientException.class); - int port = wireMockRule.port(); - HttpClientConnector conector = new HttpClientConnector(); - CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>(); - request.endpoint("http://localhost:"+port+"/testUrl"); - request.setBasicAuthentication("USER","PASSWORD"); - request.method(HttpMethod.GET); - conector.request(request); // gets down to "Get here on an error response (4XX-5XX)", then tries to throw a CloudifyResponseException, which calls getEntity, which tries to parse an HTML error page as a JSON, which causes the HttpClientException. - } - - -}
\ No newline at end of file + + @Rule + public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort()); + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void sunnyDay_POST() { + wireMockRule.stubFor(post(urlPathEqualTo("/testUrl")).willReturn(aResponse() + .withHeader("Content-Type", "application/json").withBody("TEST").withStatus(HttpStatus.SC_OK))); + int port = wireMockRule.port(); + HttpClientConnector conector = new HttpClientConnector(); + CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>(); + Deployment deployment = new Deployment(); + deployment.setId("id"); + request.entity(deployment, "application/json"); + request.endpoint("http://localhost:" + port + "/testUrl"); + request.setBasicAuthentication("USER", "PASSWORD"); + request.header("Content-Type", "application/json"); + request.method(HttpMethod.POST); + conector.request(request); + verify(postRequestedFor(urlEqualTo("/testUrl"))); + } + + + @Test + public void sunnyDay_GET() { + wireMockRule.stubFor(get(urlPathEqualTo("/testUrl")).willReturn(aResponse() + .withHeader("Content-Type", "application/json").withBody("TEST").withStatus(HttpStatus.SC_OK))); + int port = wireMockRule.port(); + HttpClientConnector conector = new HttpClientConnector(); + CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>(); + request.endpoint("http://localhost:" + port + "/testUrl"); + request.setBasicAuthentication("USER", "PASSWORD"); + request.method(HttpMethod.GET); + conector.request(request); + verify(getRequestedFor(urlEqualTo("/testUrl"))); + } + + @Test + public void sunnyDay_PUT() { + wireMockRule.stubFor(put(urlPathEqualTo("/testUrl")).willReturn(aResponse() + .withHeader("Content-Type", "application/json").withBody("TEST").withStatus(HttpStatus.SC_OK))); + int port = wireMockRule.port(); + HttpClientConnector conector = new HttpClientConnector(); + CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>(); + request.endpoint("http://localhost:" + port + "/testUrl"); + request.setBasicAuthentication("USER", "PASSWORD"); + request.method(HttpMethod.PUT); + conector.request(request); + verify(putRequestedFor(urlEqualTo("/testUrl"))); + } + + + @Test + public void sunnyDay_DELETE() { + wireMockRule.stubFor(delete(urlPathEqualTo("/testUrl")).willReturn(aResponse() + .withHeader("Content-Type", "application/json").withBody("TEST").withStatus(HttpStatus.SC_OK))); + int port = wireMockRule.port(); + HttpClientConnector conector = new HttpClientConnector(); + CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>(); + request.endpoint("http://localhost:" + port + "/testUrl"); + request.setBasicAuthentication("USER", "PASSWORD"); + request.method(HttpMethod.DELETE); + conector.request(request); + verify(deleteRequestedFor(urlEqualTo("/testUrl"))); + } + + + @Test + public void rainyDay_PATCH() { + thrown.expect(HttpClientException.class); + thrown.expectMessage("Unrecognized HTTP Method: PATCH"); + HttpClientConnector conector = new HttpClientConnector(); + CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>(); + request.endpoint("http://localhost:123123/testUrl"); + request.setBasicAuthentication("USER", "PASSWORD"); + request.method(HttpMethod.PATCH); + conector.request(request); + + } + + @Test + public void rainyDayRunTimeException() { + wireMockRule.stubFor(post(urlEqualTo("/503")) + .willReturn(aResponse().withStatus(503).withHeader("Content-Type", "text/plain").withBody("failure"))); + thrown.expect(RuntimeException.class); + thrown.expectMessage("Unexpected client exception"); + HttpClientConnector conector = new HttpClientConnector(); + CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>(); + request.endpoint("http://localhost:123123/503"); + request.setBasicAuthentication("USER", "PASSWORD"); + request.method(HttpMethod.POST); + conector.request(request); + + } + + @Test + public void rainyDayBadUri() { + wireMockRule.stubFor(post(urlPathEqualTo("/testUrl")).willReturn(aResponse() + .withHeader("Content-Type", "application/json").withBody("TEST").withStatus(HttpStatus.SC_OK))); + thrown.expect(HttpClientException.class); + int port = wireMockRule.port(); + HttpClientConnector conector = new HttpClientConnector(); + CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>(); + Deployment deployment = new Deployment(); + deployment.setId("id"); + request.entity(deployment, "application/json"); + request.endpoint("(@#$@(#*$&asfasdf"); + request.setBasicAuthentication("USER", "PASSWORD"); + request.header("Content-Type", "application/json"); + request.method(HttpMethod.POST); + conector.request(request); + } + + @Test + public void sunnyDayWithJsonEntity_POST() { + wireMockRule.stubFor(post(urlPathEqualTo("/testUrl")).willReturn(aResponse() + .withHeader("Content-Type", "application/json").withBody("TEST").withStatus(HttpStatus.SC_OK))); + int port = wireMockRule.port(); + HttpClientConnector conector = new HttpClientConnector(); + + Deployment deployment = new Deployment(); + deployment.setId("id"); + + CloudifyRequest<Deployment> request = + new CloudifyRequest<Deployment>(null, HttpMethod.POST, "/", Entity.json(deployment), null); + + request.endpoint("http://localhost:" + port); + request.path("testUrl"); + request.header("Content-Type", "application/json"); + request.header("Content-Type", null); + + request.returnType(Deployment.class); + assertEquals(Deployment.class, request.returnType()); + + Entity<Deployment> t = request.json(deployment); + assertEquals(t.getEntity().getId(), "id"); + + request.queryParam("test", "one").queryParam("test", "two"); + + conector.request(request); + + verify(postRequestedFor(urlEqualTo("/testUrl?test=two"))); + } + + @Test + public void sunnyDayWithStreamEntity_POST() { + wireMockRule.stubFor(post(urlPathEqualTo("/testUrl")).willReturn(aResponse() + .withHeader("Content-Type", "application/json").withBody("TEST").withStatus(HttpStatus.SC_OK))); + int port = wireMockRule.port(); + HttpClientConnector conector = new HttpClientConnector(); + + InputStream is = new ByteArrayInputStream("{}".getBytes(StandardCharsets.UTF_8)); + + CloudifyRequest<Deployment> request = + new CloudifyRequest<Deployment>(null, HttpMethod.POST, "/testUrl", Entity.stream(is), null); + + request.endpoint("http://localhost:" + port); + request.setBasicAuthentication("USER", "PASSWORD"); + request.header("Content-Type", "application/json"); + + conector.request(request); + verify(postRequestedFor(urlEqualTo("/testUrl"))); + } + + @Test + public void rainyDayGarbageData() { + wireMockRule.stubFor( + get(urlPathEqualTo("/testUrl")).willReturn(aResponse().withFault(Fault.RANDOM_DATA_THEN_CLOSE))); + thrown.expect(CloudifyConnectException.class); + int port = wireMockRule.port(); + HttpClientConnector conector = new HttpClientConnector(); + CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>(); + request.endpoint("http://localhost:" + port + "/testUrl"); + request.setBasicAuthentication("USER", "PASSWORD"); + request.method(HttpMethod.GET); + conector.request(request); + } + + @Test + public void rainyDayEmptyResponse() { + wireMockRule.stubFor(get(urlPathEqualTo("/testUrl")).willReturn(aResponse() + .withHeader("Content-Type", "application/json").withBody("TEST").withStatus(HttpStatus.SC_NOT_FOUND))); + + thrown.expect(HttpClientException.class); + int port = wireMockRule.port(); + HttpClientConnector conector = new HttpClientConnector(); + CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>(); + request.endpoint("http://localhost:" + port + "/testUrl"); + request.setBasicAuthentication("USER", "PASSWORD"); + request.method(HttpMethod.GET); + conector.request(request); // gets down to "Get here on an error response (4XX-5XX)", then tries to throw a + // CloudifyResponseException, which calls getEntity, which tries to parse an HTML + // error page as a JSON, which causes the HttpClientException. + } + + +} |