OadrHttpClient20a { public <T> T post(Object payload, String path, Class<T> responseKlass) throws Oadr20aException, Oadr20aHttpLayerException { return this.post(null, path, null, payload, responseKlass); } OadrHttpClient20a(OadrHttpClient client); T post(Object payload, String path, Class<T> responseKlass); T post(String host, String path, HttpClientContext context, Object payload, Class<T> responseKlass);  }
@Test public void validPostTest() throws ClientProtocolException, IOException, JAXBException, Oadr20aException, Oadr20aMarshalException, URISyntaxException, Oadr20aHttpLayerException { OadrHttpClient oadrHttpClient = Mockito.mock(OadrHttpClient.class); int scOk = HttpStatus.SC_OK; OadrResponse mockOadrResponse = Oadr20aBuilders.newOadr20aResponseBuilder("", scOk).build(); String marshal = jaxbContext.marshal(mockOadrResponse); HttpResponse response = this.createHttpResponse(scOk, marshal); when(oadrHttpClient.execute(Matchers.<HttpPost>anyObject(), any(), any(), any())).thenReturn(response); OadrHttpClient20a client = new OadrHttpClient20a(oadrHttpClient); OadrDistributeEvent mockDistributeEvent = this.createOadrDistributeEvent(); OadrResponse post = client.post(mockDistributeEvent, Oadr20aUrlPath.OADR_BASE_PATH + Oadr20aUrlPath.EI_EVENT_SERVICE, OadrResponse.class); assertEquals(String.valueOf(scOk), post.getEiResponse().getResponseCode()); }
@Test public void httpLayerErrorPostTest() throws ClientProtocolException, IOException, JAXBException, Oadr20aException, Oadr20aMarshalException, URISyntaxException, Oadr20aHttpLayerException { OadrHttpClient oadrHttpClient = Mockito.mock(OadrHttpClient.class); int scForbidden = HttpStatus.SC_FORBIDDEN; OadrResponse mockOadrResponse = Oadr20aBuilders.newOadr20aResponseBuilder("", scForbidden).build(); String marshal = jaxbContext.marshal(mockOadrResponse); HttpResponse response = this.createHttpResponse(scForbidden, marshal); when(oadrHttpClient.execute(Matchers.<HttpPost>anyObject(), any(), any(), any())).thenReturn(response); OadrHttpClient20a client = new OadrHttpClient20a(oadrHttpClient); OadrDistributeEvent mockDistributeEvent = this.createOadrDistributeEvent(); boolean exception = false; try { client.post(mockDistributeEvent, Oadr20aUrlPath.OADR_BASE_PATH + Oadr20aUrlPath.EI_EVENT_SERVICE, OadrResponse.class); } catch (Oadr20aHttpLayerException e) { exception = true; } assertTrue(exception); }
@Test public void requestMarshallingErrorPostTest() throws ClientProtocolException, IOException, JAXBException, Oadr20aException, Oadr20aMarshalException, URISyntaxException, Oadr20aHttpLayerException { OadrHttpClient oadrHttpClient = Mockito.mock(OadrHttpClient.class); int scOk = HttpStatus.SC_OK; OadrResponse mockOadrResponse = Oadr20aBuilders.newOadr20aResponseBuilder("", scOk).build(); String marshal = jaxbContext.marshal(mockOadrResponse); HttpResponse response = this.createHttpResponse(scOk, marshal); when(oadrHttpClient.execute(Matchers.<HttpPost>anyObject(), any(), any(), any())).thenReturn(response); OadrHttpClient20a client = new OadrHttpClient20a(oadrHttpClient); OadrDistributeEvent mockDistributeEvent = this.createOadrDistributeEvent(); mockDistributeEvent.setVtnID(null); boolean exception = false; try { client.post(mockDistributeEvent, Oadr20aUrlPath.OADR_BASE_PATH + Oadr20aUrlPath.EI_EVENT_SERVICE, OadrResponse.class); } catch (Oadr20aException e) { exception = true; } assertTrue(exception); }
@Test public void responseUnmarshallingErrorPostTest() throws ClientProtocolException, IOException, JAXBException, Oadr20aException, Oadr20aMarshalException, URISyntaxException, Oadr20aHttpLayerException { OadrHttpClient oadrHttpClient = Mockito.mock(OadrHttpClient.class); int scOk = HttpStatus.SC_OK; String marshal = ""; HttpResponse response = this.createHttpResponse(scOk, marshal); when(oadrHttpClient.execute(Matchers.<HttpPost>anyObject(), any(), any(), any())).thenReturn(response); OadrHttpClient20a client = new OadrHttpClient20a(oadrHttpClient); OadrDistributeEvent mockDistributeEvent = this.createOadrDistributeEvent(); boolean exception = false; try { client.post(mockDistributeEvent, Oadr20aUrlPath.OADR_BASE_PATH + Oadr20aUrlPath.EI_EVENT_SERVICE, OadrResponse.class); } catch (Oadr20aException e) { exception = true; } assertTrue(exception); }