OadrHttpClient20b { public <T, I extends JAXBElement<?>> T post(I payload, String path, Class<T> responseKlass) throws Oadr20bException, Oadr20bHttpLayerException, Oadr20bXMLSignatureException, Oadr20bXMLSignatureValidationException { return this.post(null, path, null, payload, responseKlass); } OadrHttpClient20b(OadrHttpClient client);  OadrHttpClient20b(OadrHttpClient client, String privateKeyPath, String clientCertificatePath,
			Long replayProtectAcceptedDelaySecond);  OadrHttpClient20b(OadrHttpClient client, String privateKeyPath, String clientCertificatePath,
			Long replayProtectAcceptedDelaySecond, Boolean validateXmlPayload); T post(I payload, String path, Class<T> responseKlass); O post(String host, String path, HttpClientContext context, I payload,
			Class<O> responseKlass);  }
@Test public void givenValidUnsignedPost_DoNotRaiseException() throws ClientProtocolException, IOException, JAXBException, Oadr20bException, Oadr20bMarshalException, URISyntaxException, Oadr20bHttpLayerException, Oadr20bXMLSignatureException, Oadr20bXMLSignatureValidationException, OadrSecurityException { OadrHttpClient oadrHttpClient = Mockito.mock(OadrHttpClient.class); int scOk = HttpStatus.SC_OK; OadrResponseType mockOadrResponseType = Oadr20bResponseBuilders.newOadr20bResponseBuilder("", scOk, "venId") .build(); String marshal = jaxbContext.marshalRoot(mockOadrResponseType); HttpResponse response = this.createHttpResponse(scOk, marshal); when(oadrHttpClient.execute(Matchers.<HttpPost>anyObject(), any(), any(), any())).thenReturn(response); OadrHttpClient20b client = new OadrHttpClient20b(oadrHttpClient); OadrDistributeEventType mockDistributeEvent = this.createOadrDistributeEvent(); OadrResponseType post = client.post(Oadr20bFactory.createOadrDistributeEvent(mockDistributeEvent), Oadr20bUrlPath.OADR_BASE_PATH + Oadr20bUrlPath.EI_EVENT_SERVICE, OadrResponseType.class); assertEquals(String.valueOf(scOk), post.getEiResponse().getResponseCode()); }
@Test public void givenNotSignedResponse_RaiseException() throws ClientProtocolException, IOException, JAXBException, Oadr20bException, Oadr20bMarshalException, URISyntaxException, Oadr20bHttpLayerException, Oadr20bXMLSignatureException, Oadr20bXMLSignatureValidationException, OadrSecurityException { OadrHttpClient oadrHttpClient = Mockito.mock(OadrHttpClient.class); int scOk = HttpStatus.SC_OK; OadrResponseType mockOadrResponseType = Oadr20bResponseBuilders.newOadr20bResponseBuilder("", scOk, "venId") .build(); String marshal = jaxbContext.marshalRoot(mockOadrResponseType); HttpResponse response = this.createHttpResponse(scOk, marshal); when(oadrHttpClient.execute(Matchers.<HttpPost>anyObject(), any(), any(), any())).thenReturn(response); String certPath = "src/test/resources/cert/test"; OadrHttpClient20b client = new OadrHttpClient20b(oadrHttpClient, certPath + ".key", certPath + ".crt", 1200L); OadrDistributeEventType mockDistributeEvent = this.createOadrDistributeEvent(); boolean exception = false; try { client.post(Oadr20bFactory.createOadrDistributeEvent(mockDistributeEvent), Oadr20bUrlPath.OADR_BASE_PATH + Oadr20bUrlPath.EI_EVENT_SERVICE, OadrResponseType.class); } catch (Oadr20bException e) { exception = true; } assertTrue(exception); }
@Test public void givenHttpError_RaiseException() throws ClientProtocolException, IOException, JAXBException, Oadr20bException, Oadr20bMarshalException, URISyntaxException, Oadr20bHttpLayerException, Oadr20bXMLSignatureException, Oadr20bXMLSignatureValidationException, OadrSecurityException { OadrHttpClient oadrHttpClient = Mockito.mock(OadrHttpClient.class); HttpResponse response = this.createHttpResponse(HttpStatus.SC_FORBIDDEN, ""); when(oadrHttpClient.execute(Matchers.<HttpPost>anyObject(), any(), any(), any())).thenReturn(response); OadrHttpClient20b client = new OadrHttpClient20b(oadrHttpClient); OadrDistributeEventType mockDistributeEvent = this.createOadrDistributeEvent(); boolean exception = false; try { client.post(Oadr20bFactory.createOadrDistributeEvent(mockDistributeEvent), Oadr20bUrlPath.OADR_BASE_PATH + Oadr20bUrlPath.EI_EVENT_SERVICE, OadrResponseType.class); } catch (Oadr20bHttpLayerException e) { exception = true; } assertTrue(exception); }
@Test public void givenApplicationError_DoNotRaiseException() throws ClientProtocolException, IOException, JAXBException, Oadr20bException, Oadr20bMarshalException, URISyntaxException, Oadr20bHttpLayerException, Oadr20bXMLSignatureException, Oadr20bXMLSignatureValidationException, OadrSecurityException { OadrHttpClient oadrHttpClient = Mockito.mock(OadrHttpClient.class); OadrResponseType mockOadrResponseType = Oadr20bResponseBuilders .newOadr20bResponseBuilder("", HttpStatus.SC_FORBIDDEN, "venId").build(); String marshal = jaxbContext.marshalRoot(mockOadrResponseType); HttpResponse response = this.createHttpResponse(HttpStatus.SC_OK, marshal); when(oadrHttpClient.execute(Matchers.<HttpPost>anyObject(), any(), any(), any())).thenReturn(response); OadrHttpClient20b client = new OadrHttpClient20b(oadrHttpClient); OadrDistributeEventType mockDistributeEvent = this.createOadrDistributeEvent(); client.post(Oadr20bFactory.createOadrDistributeEvent(mockDistributeEvent), Oadr20bUrlPath.OADR_BASE_PATH + Oadr20bUrlPath.EI_EVENT_SERVICE, OadrResponseType.class); }
@Test public void givenUnmarshallingRequest_RaiseException() throws ClientProtocolException, IOException, JAXBException, Oadr20bException, Oadr20bMarshalException, URISyntaxException, Oadr20bHttpLayerException, Oadr20bXMLSignatureException, Oadr20bXMLSignatureValidationException, OadrSecurityException { OadrHttpClient oadrHttpClient = Mockito.mock(OadrHttpClient.class); int scOk = HttpStatus.SC_OK; OadrResponseType mockOadrResponseType = Oadr20bResponseBuilders.newOadr20bResponseBuilder("", scOk, "venId") .build(); String marshal = jaxbContext.marshalRoot(mockOadrResponseType); HttpResponse response = this.createHttpResponse(scOk, marshal); when(oadrHttpClient.execute(Matchers.<HttpPost>anyObject(), any(), any(), any())).thenReturn(response); OadrHttpClient20b client = new OadrHttpClient20b(oadrHttpClient, null, null, null, true); OadrDistributeEventType mockDistributeEvent = this.createOadrDistributeEvent(); mockDistributeEvent.setVtnID(null); boolean exception = false; try { client.post(Oadr20bFactory.createOadrDistributeEvent(mockDistributeEvent), Oadr20bUrlPath.OADR_BASE_PATH + Oadr20bUrlPath.EI_EVENT_SERVICE, OadrResponseType.class); } catch (Oadr20bException e) { exception = true; } assertTrue(exception); }
@Test public void givenUnmarshallingResponse_RaiseException() throws ClientProtocolException, IOException, JAXBException, Oadr20bException, Oadr20bMarshalException, URISyntaxException, Oadr20bHttpLayerException, Oadr20bXMLSignatureException, Oadr20bXMLSignatureValidationException, OadrSecurityException { 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); OadrHttpClient20b client = new OadrHttpClient20b(oadrHttpClient); OadrDistributeEventType mockDistributeEvent = this.createOadrDistributeEvent(); boolean exception = false; try { client.post(Oadr20bFactory.createOadrDistributeEvent(mockDistributeEvent), Oadr20bUrlPath.OADR_BASE_PATH + Oadr20bUrlPath.EI_EVENT_SERVICE, OadrResponseType.class); } catch (Oadr20bException e) { exception = true; } assertTrue(exception); }
@Test public void responseNotSignedErrorPostTest() throws ClientProtocolException, IOException, JAXBException, Oadr20bException, Oadr20bMarshalException, URISyntaxException, Oadr20bHttpLayerException, Oadr20bXMLSignatureException, Oadr20bXMLSignatureValidationException, OadrSecurityException { OadrHttpClient oadrHttpClient = Mockito.mock(OadrHttpClient.class); int scOk = HttpStatus.SC_OK; OadrResponseType mockOadrResponseType = Oadr20bResponseBuilders.newOadr20bResponseBuilder("", scOk, "venId") .build(); String marshal = jaxbContext.marshalRoot(mockOadrResponseType); HttpResponse response = this.createHttpResponse(scOk, marshal); when(oadrHttpClient.execute(Matchers.<HttpPost>anyObject(), any(), any(), any())).thenReturn(response); String keyFile = CERT_FOLDER_PATH + "test.key"; String certFile = CERT_FOLDER_PATH + "test.crt"; OadrHttpClient20b client = new OadrHttpClient20b(oadrHttpClient, keyFile, certFile, 1200L); OadrDistributeEventType mockDistributeEvent = this.createOadrDistributeEvent(); mockDistributeEvent.setVtnID("vtnId"); boolean exception = false; try { client.post(Oadr20bFactory.createOadrDistributeEvent(mockDistributeEvent), Oadr20bUrlPath.OADR_BASE_PATH + Oadr20bUrlPath.EI_EVENT_SERVICE, OadrResponseType.class); } catch (Oadr20bException e) { exception = true; } assertTrue(exception); }