この素晴らしいコントローラー ポイントを作成しました。
@Named
@Path("/awsome")
@Api(value = "An Awsome api")
public class AwsomeEndpoint extends BaseEndpoint {
@GET
@Path("{isThisAwsome}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Get organisation by isthisAwsome",
notes = "Get AwsomeAnswer matching the given isThisAwsome.",
responseContainer = "single result",
response = JsonOrganisation.class)
@ApiResponses(value = {@ApiResponse(code = 200, message = "Awsome found"),
@ApiResponse(code = 404, message = "Awsome not found"),
@ApiResponse(code = 500, message = "Internal Error")})
public Response getAwsomeResponse(@PathParam("isThisAwsome") String isThisAwsome) {
Response response = handleErrors(() -> Response.ok(AwsomeResponseTransformer.transform(awsomeService.getAwsomeByisThisAwsome(isThisAwsome))).build());
return response;
}
}
このコードでは、Lamda を使用してエラー フローを処理します。
public class BaseEndpoint {
protected Response handleErrors(Supplier<Response> responseSupplier) {
Response response;
try {
response = responseSupplier.get();
} catch (AwsomeRuntimeException e) {
response = createExceptionResponse(e);
}
return response;
}
private Response createExceptionResponse(AwsomeRuntimeException e) {
Response.ResponseBuilder response;
if (e.getExceptionStatus() == ExceptionStatus.NOT_FOUND) {
response = Response.status(Status.NOT_FOUND).entity(e.getMessage());
} else if (e.getExceptionStatus() == ExceptionStatus.ILLEGAL_ARGUMENT) {
response = Response.status(Status.BAD_REQUEST).entity(e.getMessage());
} else {
response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage());
}
return response.header("","").header("Pragma", "no-cache, no-store").header("Cache-Control", "no-cache, no-store").header("Expires", "0").build();
}}
このために、いくつかの Pact テストを作成しました。呼び出すサービスによってスローされるエラーをモックする場合を除いて、すべての Pact が機能します。
pact テスト クラスの例:
RunWith(PactRunner.class)
@Provider("awsome")
@PactBroker(authentication=@PactBrokerAuth(username = "${pact.broker.username:x}", password = "${pact.broker.password:x}"),
protocol="${pact.broker.protocol:https}", host="${pact.broker.host:hosted.pact.dius.com.au}", port="${pact.broker.port:443}",
failIfNoPactsFound=false)
public class AccMgtPactIntegrationTest extends JerseyTest {
@TestTarget public final HttpTarget target = new HttpTarget(getPort());
private AwsomeService awsomeService;
private UserService userService;
@Override
protected ResourceConfig configure() {
awsomeService = mock(AwsomeService.class);
return new ResourceConfig().register(new OrganisationsEndpoint(awsomeService)).register(new AwsomeEndpoint())
// .property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, "true")
;
}
@State("Notn found")
public void whenOrganisationWithShortNameDoesNotExist() {
when(awsomeService.getOrganisationByShortName("WME")).thenThrow(new AwsomeRuntimeException(ExceptionStatus.NOT_FOUND));
} }
今、私は Pact からいくつかの磨耗エラーを受け取ります。問題は Pact ではなく、girlzy が Pact でどのように機能するかだと思います。次のエラーが表示されるため:
Verifying a pact between consumer and aswsome Given when
organisation with shortName does not exist Get awsome by isItAwsome
returns a response which
has status code 404 (OK)
includes headers
"Content-Type" with value "application/json" (FAILED)
has a matching body (FAILED)
失敗:
0) 短い名前で組織を取得すると、値が「application/json」のヘッダー「Content-Type」を含む応答が返されますヘッダー「Content-Type」には値「application/json」が必要ですが、「text/html;charset=ISO」でした-8859-1'
1) 短い名前で組織を取得すると、本文の比較が一致する応答が返されます -> 応答タイプが 'application/json' であると予想されていましたが、実際のタイプは 'text/html' でした
(コメントアウトされたプロパティ)を設定するとRESPONSE_SET_STATUS_OVER_SEND_ERROR
次に、協定は次のように失敗します。
Verifying a pact between consumer and aswsome
Given when organisation with shortName does not exist
Get awsome by isItAwsome:
ul 13, 2017 12:54:14 PM org.glassfish.grizzly.servlet.ServletHandler doServletService
SEVERE: service exception:
javax.servlet.ServletException: javax.servlet.ServletException: java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at org.glassfish.grizzly.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:151)
at org.glassfish.grizzly.servlet.FilterChainImpl.invokeFilterChain(FilterChainImpl.java:106)
at org.glassfish.grizzly.servlet.ServletHandler.doServletService(ServletHandler.java:224)
at org.glassfish.grizzly.servlet.ServletHandler.service(ServletHandler.java:173)
at org.glassfish.grizzly.http.server.HttpHandler$1.run(HttpHandler.java:224)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:593)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:573)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.servlet.ServletException: java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:489)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)
at org.glassfish.grizzly.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:147)
... 7 more
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:658)
at org.glassfish.grizzly.http.HttpHeader.isSpecialHeader(HttpHeader.java:925)
at org.glassfish.grizzly.http.HttpHeader.handleGetSpecialHeader(HttpHeader.java:901)
at org.glassfish.grizzly.http.HttpHeader.containsHeader(HttpHeader.java:762)
at org.glassfish.grizzly.http.server.Response.containsHeader(Response.java:1268)
at org.glassfish.grizzly.servlet.HttpServletResponseImpl.containsHeader(HttpServletResponseImpl.java:472)
at org.glassfish.jersey.servlet.internal.ResponseWriter.writeResponseStatusAndHeaders(ResponseWriter.java:159)
at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:683)
at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:444)
at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:434)
at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:329)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)
... 12 more
returns a response which
has status code 404 (FAILED)
includes headers
"Content-Type" with value "application/json" (FAILED)
has a matching body (FAILED)
失敗:
0) Get organisation by short name returns a response which includes headers "Content-Type" with value "application/json"
Expected header 'Content-Type' to have value 'application/json' but was 'text/html;charset=ISO-8859-1'
1) Get organisation by short name returns a response which has a matching body
comparison -> Expected a response type of 'application/json' but the actual type was 'text/html'
2) Get organisation by short name returns a response which has status code
404
assert expectedStatus == actualStatus
| | |
404 | 500
false
grizly は、エラー処理用に作成した Lamda が気に入らないような気がします。しかし、サーバーコンテナーのためにプロバイダーを機能させるためだけにコードを変更したくありません。私がコードをデバッグしようとしてきたアイデアは、コンテナに引き渡されるまで正しく流れます。そして、Jersy注釈を完全に無視しているようです