Advertisement
Guest User

Untitled

a guest
Jul 14th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.74 KB | None | 0 0
  1. @Named
  2. @Path("/awsome")
  3. @Api(value = "An Awsome api")
  4. public class AwsomeEndpoint extends BaseEndpoint {
  5.  
  6. @GET
  7. @Path("{isThisAwsome}")
  8. @Produces(MediaType.APPLICATION_JSON)
  9. @ApiOperation(value = "Get organisation by isthisAwsome",
  10. notes = "Get AwsomeAnswer matching the given isThisAwsome.",
  11. responseContainer = "single result",
  12. response = JsonOrganisation.class)
  13. @ApiResponses(value = {@ApiResponse(code = 200, message = "Awsome found"),
  14. @ApiResponse(code = 404, message = "Awsome not found"),
  15. @ApiResponse(code = 500, message = "Internal Error")})
  16. public Response getAwsomeResponse(@PathParam("isThisAwsome") String isThisAwsome) {
  17. Response response = handleErrors(() -> Response.ok(AwsomeResponseTransformer.transform(awsomeService.getAwsomeByisThisAwsome(isThisAwsome))).build());
  18. return response;
  19. }
  20. }
  21.  
  22. public class BaseEndpoint {
  23.  
  24. protected Response handleErrors(Supplier<Response> responseSupplier) {
  25. Response response;
  26. try {
  27. response = responseSupplier.get();
  28. } catch (AwsomeRuntimeException e) {
  29. response = createExceptionResponse(e);
  30. }
  31. return response;
  32. }
  33.  
  34. private Response createExceptionResponse(AwsomeRuntimeException e) {
  35. Response.ResponseBuilder response;
  36.  
  37. if (e.getExceptionStatus() == ExceptionStatus.NOT_FOUND) {
  38. response = Response.status(Status.NOT_FOUND).entity(e.getMessage());
  39. } else if (e.getExceptionStatus() == ExceptionStatus.ILLEGAL_ARGUMENT) {
  40. response = Response.status(Status.BAD_REQUEST).entity(e.getMessage());
  41. } else {
  42. response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage());
  43. }
  44.  
  45. return response.header("","").header("Pragma", "no-cache, no-store").header("Cache-Control", "no-cache, no-store").header("Expires", "0").build();
  46. }}
  47.  
  48. RunWith(PactRunner.class)
  49. @Provider("awsome")
  50. @PactBroker(authentication=@PactBrokerAuth(username = "${pact.broker.username:x}", password = "${pact.broker.password:x}"),
  51. protocol="${pact.broker.protocol:https}", host="${pact.broker.host:hosted.pact.dius.com.au}", port="${pact.broker.port:443}",
  52. failIfNoPactsFound=false)
  53. public class AccMgtPactIntegrationTest extends JerseyTest {
  54.  
  55. @TestTarget public final HttpTarget target = new HttpTarget(getPort());
  56. private AwsomeService awsomeService;
  57. private UserService userService;
  58.  
  59. @Override
  60. protected ResourceConfig configure() {
  61. awsomeService = mock(AwsomeService.class);
  62. return new ResourceConfig().register(new OrganisationsEndpoint(awsomeService)).register(new AwsomeEndpoint())
  63. // .property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, "true")
  64. ;
  65. }
  66.  
  67. @State("Notn found")
  68. public void whenOrganisationWithShortNameDoesNotExist() {
  69. when(awsomeService.getOrganisationByShortName("WME")).thenThrow(new AwsomeRuntimeException(ExceptionStatus.NOT_FOUND));
  70. } }
  71.  
  72. Verifying a pact between consumer and aswsome Given when
  73. organisation with shortName does not exist Get awsome by isItAwsome
  74. returns a response which
  75. has status code 404 (OK)
  76. includes headers
  77. "Content-Type" with value "application/json" (FAILED)
  78. has a matching body (FAILED)
  79.  
  80. Verifying a pact between consumer and aswsome
  81. Given when organisation with shortName does not exist
  82. Get awsome by isItAwsome:
  83. ul 13, 2017 12:54:14 PM org.glassfish.grizzly.servlet.ServletHandler doServletService
  84. SEVERE: service exception:
  85. javax.servlet.ServletException: javax.servlet.ServletException: java.lang.StringIndexOutOfBoundsException: String index out of range: 0
  86. at org.glassfish.grizzly.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:151)
  87. at org.glassfish.grizzly.servlet.FilterChainImpl.invokeFilterChain(FilterChainImpl.java:106)
  88. at org.glassfish.grizzly.servlet.ServletHandler.doServletService(ServletHandler.java:224)
  89. at org.glassfish.grizzly.servlet.ServletHandler.service(ServletHandler.java:173)
  90. at org.glassfish.grizzly.http.server.HttpHandler$1.run(HttpHandler.java:224)
  91. at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:593)
  92. at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:573)
  93. at java.lang.Thread.run(Thread.java:745)
  94. Caused by: javax.servlet.ServletException: java.lang.StringIndexOutOfBoundsException: String index out of range: 0
  95. at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:489)
  96. at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
  97. at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)
  98. at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)
  99. at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)
  100. at org.glassfish.grizzly.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:147)
  101. ... 7 more
  102. Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: 0
  103. at java.lang.String.charAt(String.java:658)
  104. at org.glassfish.grizzly.http.HttpHeader.isSpecialHeader(HttpHeader.java:925)
  105. at org.glassfish.grizzly.http.HttpHeader.handleGetSpecialHeader(HttpHeader.java:901)
  106. at org.glassfish.grizzly.http.HttpHeader.containsHeader(HttpHeader.java:762)
  107. at org.glassfish.grizzly.http.server.Response.containsHeader(Response.java:1268)
  108. at org.glassfish.grizzly.servlet.HttpServletResponseImpl.containsHeader(HttpServletResponseImpl.java:472)
  109. at org.glassfish.jersey.servlet.internal.ResponseWriter.writeResponseStatusAndHeaders(ResponseWriter.java:159)
  110. at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:683)
  111. at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:444)
  112. at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:434)
  113. at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:329)
  114. at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
  115. at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
  116. at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
  117. at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
  118. at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
  119. at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
  120. at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
  121. at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
  122. at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)
  123. ... 12 more
  124.  
  125. returns a response which
  126. has status code 404 (FAILED)
  127. includes headers
  128. "Content-Type" with value "application/json" (FAILED)
  129. has a matching body (FAILED)
  130.  
  131. 0) Get organisation by short name returns a response which includes headers "Content-Type" with value "application/json"
  132. Expected header 'Content-Type' to have value 'application/json' but was 'text/html;charset=ISO-8859-1'
  133.  
  134. 1) Get organisation by short name returns a response which has a matching body
  135. comparison -> Expected a response type of 'application/json' but the actual type was 'text/html'
  136.  
  137. 2) Get organisation by short name returns a response which has status code
  138.  
  139. 404
  140. assert expectedStatus == actualStatus
  141. | | |
  142. 404 | 500
  143. false
  144.  
  145. when(awsomeService.getOrganisationByShortName("WME")).thenThrow(new AwsomeRuntimeException(ExceptionStatus.NOT_FOUND));
  146.  
  147. when(awsomeService.getOrganisationByShortName(anyString()).thenThrow(new AwsomeRuntimeException(ExceptionStatus.NOT_FOUND));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement