Ejemplo n.º 1
0
  @Test(dataProvider = "annotatedRequestHandlerData")
  public void testCreateAnnotatedRequestHandler(
      Class<?> requestHandler,
      boolean collection,
      boolean create,
      boolean read,
      boolean update,
      boolean delete,
      boolean patch,
      boolean resourceAction,
      boolean collectionAction,
      boolean query)
      throws Exception {

    // Given
    Object provider = requestHandler.newInstance();
    Connection connection = Resources.newInternalConnection(createHandler(collection, provider));
    CreateRequest req = Requests.newCreateRequest("/test", json(object(field("dummy", "test"))));

    // When
    Promise<Resource, ResourceException> promise = connection.createAsync(new RootContext(), req);

    // Then
    if (create && collection) {
      assertThat(promise).succeeded().withId().isEqualTo("create");
    } else if (collection) {
      assertThat(promise).failedWithException().isInstanceOf(NotSupportedException.class);
    } else {
      assertThat(promise).failedWithException().isInstanceOf(BadRequestException.class);
    }
  }
  @Test(dataProvider = "dataWithoutVersionHeader")
  @SuppressWarnings("unchecked")
  public void shouldRouteCreateRequestsUsingDefaultVersionBehaviour(
      String resource,
      DefaultVersionBehaviour versionBehaviour,
      boolean expectException,
      RequestHandler provider) {

    // Given
    AcceptAPIVersionContext apiVersionContext =
        new AcceptAPIVersionContext(
            new RootContext(),
            "PROTOCOL_NAME",
            AcceptAPIVersion.newBuilder().withDefaultProtocolVersion("1.0").build());
    ServerContext context = new ServerContext(apiVersionContext);
    CreateRequest request = Requests.newCreateRequest(resource, json(object()));
    ResultHandler<Resource> handler = mock(ResultHandler.class);
    setDefaultVersionBehaviour(router, versionBehaviour);

    // When
    router.handleCreate(context, request, handler);

    // Then
    if (expectException) {
      ArgumentCaptor<ResourceException> exceptionCaptor =
          ArgumentCaptor.forClass(ResourceException.class);
      verify(handler).handleError(exceptionCaptor.capture());
      assertThat(exceptionCaptor.getValue()).isInstanceOf(BadRequestException.class);
      verifyZeroInteractions(
          usersHandlerOne,
          usersHandlerTwo,
          usersHandlerThree,
          groupsHandlerOne,
          groupsHandlerTwo,
          groupsHandlerThree);
    } else {
      verify(provider)
          .handleCreate(
              Matchers.<ServerContext>anyObject(),
              Matchers.<CreateRequest>anyObject(),
              eq(handler));
    }
  }