org.glassfish.jersey.server.ContainerResponse Java Examples

The following examples show how to use org.glassfish.jersey.server.ContainerResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JRestlessContainerResponseWriterTest.java    From jrestless with Apache License 2.0 6 votes vote down vote up
@Test
public void writeResponseStatusAndHeaders_ContextHeaderAndStatusGiven_ShouldUpdateResponseStatusAndHeaders() {
	MultivaluedMap<String, String> actualHeaders = new MultivaluedHashMap<>();
	actualHeaders.add("header0", "value0_0");
	actualHeaders.add("header0", "value0_1");
	actualHeaders.add("header1", "value1_0");
	MultivaluedMap<String, String> expectedHeaders = new MultivaluedHashMap<>();
	expectedHeaders.add("header0", "value0_0");
	expectedHeaders.add("header0", "value0_1");
	expectedHeaders.add("header1", "value1_0");

	ContainerResponse context = mock(ContainerResponse.class);
	when(context.getStatusInfo()).thenReturn(Status.CONFLICT);
	when(context.getStringHeaders()).thenReturn(actualHeaders);

	containerResponseWriter.writeResponseStatusAndHeaders(-1, context);

	assertEquals(Status.CONFLICT, response.getStatusType());
	assertEquals(expectedHeaders, response.getHeaders());
}
 
Example #2
Source File: OwnerResourceTest.java    From minnal with Apache License 2.0 6 votes vote down vote up
@Test
public void listOwnerTest() {
	org.minnal.examples.petclinic.domain.Owner owner = createDomain(org.minnal.examples.petclinic.domain.Owner.class);
	owner.persist();
	ContainerResponse response = call(request("/owners/",
			HttpMethod.GET));
	assertEquals(response.getStatus(),
			Response.Status.OK.getStatusCode());
	assertEquals(deserializeCollection(
			getContent(response),
			java.util.List.class,
			org.minnal.examples.petclinic.domain.Owner.class)
			.size(),
			(int) org.minnal.examples.petclinic.domain.Owner
					.count());
}
 
Example #3
Source File: OwnerResourceTest.java    From minnal with Apache License 2.0 6 votes vote down vote up
@Test
public void listOwnerPetTest() {
	org.minnal.examples.petclinic.domain.Owner owner = createDomain(org.minnal.examples.petclinic.domain.Owner.class);
	owner.persist();

	org.minnal.examples.petclinic.domain.Pet pet = createDomain(org.minnal.examples.petclinic.domain.Pet.class);
	owner.collection("pets").add(pet);
	owner.persist();

	ContainerResponse response = call(request(
			"/owners/" + owner.getId() + "/pets/",
			HttpMethod.GET));
	assertEquals(response.getStatus(),
			Response.Status.OK.getStatusCode());
	assertEquals(deserializeCollection(getContent(response),
			java.util.List.class,
			org.minnal.examples.petclinic.domain.Pet.class)
			.size(), owner.getPets().size());
}
 
Example #4
Source File: Router.java    From minnal with Apache License 2.0 6 votes vote down vote up
/**
 * Creates the http response from container response</p>
 * 
 * <li> Sets the content length to the http response from the container response. If content length is not available, computes from the response entity
 * <li> 
 * 
 * @param context
 * @param containerResponse
 * @param buffer
 * @return
 */
protected FullHttpResponse createHttpResponse(MessageContext context, ContainerResponse containerResponse, ByteBuf buffer) {
	FullHttpRequest httpRequest = context.getRequest();
	DefaultFullHttpResponse httpResponse = new DefaultFullHttpResponse(httpRequest.getProtocolVersion(), HttpResponseStatus.valueOf(containerResponse.getStatus()), buffer);
	int length = containerResponse.getLength();
	
	// FIXME Hack. is there a better way?
	if (length == -1 && containerResponse.getEntity() instanceof String) {
		final String entity = (String) containerResponse.getEntity();
		final byte[] encodedBytes = entity.getBytes(Charset.forName("UTF-8"));
		length = encodedBytes.length;
	}
	if (! containerResponse.getHeaders().containsKey(HttpHeaders.Names.CONTENT_LENGTH)) {
		HttpHeaders.setContentLength(httpResponse, length);
		logger.trace("Writing response status and headers {}, length {}", containerResponse, containerResponse.getLength());
	}

	for (Map.Entry<String, List<Object>> headerEntry : containerResponse.getHeaders().entrySet()) {
		HttpHeaders.addHeader(httpResponse, headerEntry.getKey(), Joiner.on(", ").join(headerEntry.getValue()));
	}
	return httpResponse;
}
 
Example #5
Source File: OrderResourceTest.java    From minnal with Apache License 2.0 6 votes vote down vote up
@Test
public void listOrderOrderItemTest() {
	org.minnal.examples.oms.domain.Order order = createDomain(org.minnal.examples.oms.domain.Order.class);
	order.persist();

	org.minnal.examples.oms.domain.OrderItem orderItem = createDomain(org.minnal.examples.oms.domain.OrderItem.class);
	order.collection("orderItems").add(orderItem);
	order.persist();

	ContainerResponse response = call(request(
			"/orders/" + order.getId() + "/order_items/",
			HttpMethod.GET));
	assertEquals(response.getStatus(),
			Response.Status.OK.getStatusCode());
	assertEquals(deserializeCollection(getContent(response),
			java.util.List.class,
			org.minnal.examples.oms.domain.OrderItem.class)
			.size(), order.getOrderItems().size());
}
 
Example #6
Source File: StructuredEventFilterTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Test
void filterWithResponse() throws IOException {
    MultivaluedMap<String, String> headersMap = createRequestHeader();
    ContainerRequest requestContext = createRequestContext(headersMap);
    underTest.filter(requestContext);

    requestContext.setProperty("structuredevent.loggingEnabled", Boolean.TRUE);

    ArgumentCaptor<StructuredRestCallEvent> structuredEventCaptor = ArgumentCaptor.forClass(StructuredRestCallEvent.class);
    doNothing().when(structuredEventClient).sendStructuredEvent(structuredEventCaptor.capture());

    ContainerResponseContext responseContext = new ContainerResponse(requestContext, Response.accepted().build());
    underTest.filter(requestContext, responseContext);

    StructuredRestCallEvent captorValue = structuredEventCaptor.getValue();
    headersMap.forEach((key, value) -> assertEquals(value.get(0), captorValue.getRestCall().getRestRequest().getHeaders().get(key)));
}
 
Example #7
Source File: TestJSONJobDataFilter.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Test
public void testJSONGeneratorUntouched() throws IOException {
  JSONJobDataFilter filter = new JSONJobDataFilter();
  ContainerRequestBuilder request = ContainerRequestBuilder.from("http://localhost/foo/bar", "GET", null);
  if (testData.getHeaderKey() != null) {
    request.header(testData.getHeaderKey(), testData.getHeaderValue());
  }

  ContainerResponse response = new ContainerResponse(request.build(), Response.ok().type(testData.getMediaType()).build());
  filter.filter(request.build(), response);

  ObjectWriterModifier modifier = ObjectWriterInjector.get();
  if (testData.isExpectedToBeTouched()) {
    assertNotNull(modifier);

    ObjectWriter writer = mock(ObjectWriter.class);
    modifier.modify(mock(JsonEndpointConfig.class), new MultivaluedHashMap<String, Object>(), new Object(),
      writer, mock(JsonGenerator.class));
    verify(writer).withAttribute(DataJsonOutput.DREMIO_JOB_DATA_NUMBERS_AS_STRINGS_ATTRIBUTE, true);
  } else {
    assertNull(modifier);
  }
}
 
Example #8
Source File: UserWebServiceImplTest.java    From jweb-cms with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void login() {

    LoginRequest loginRequest = new LoginRequest();
    loginRequest.username = "some";
    loginRequest.password = "some";
    loginRequest.autoLogin = true;
    loginRequest.requestBy = "test";
    ContainerResponse response = app.post("/api/user/login").setEntity(loginRequest).execute();
    assertEquals(200, response.getStatus());

    LoginResponse loginResponse = (LoginResponse) response.getEntity();
    TokenLoginRequest tokenLoginRequest = new TokenLoginRequest();
    tokenLoginRequest.token = loginResponse.autoLoginToken;
    tokenLoginRequest.requestBy = "test";
    response = app.post("/api/user/login-token").setEntity(tokenLoginRequest).execute();
    assertEquals(200, response.getStatus());
}
 
Example #9
Source File: VetResourceTest.java    From minnal with Apache License 2.0 6 votes vote down vote up
@Test
public void updateVetSpecialtyTest() {
	org.minnal.examples.petclinic.domain.Vet vet = createDomain(org.minnal.examples.petclinic.domain.Vet.class);
	vet.persist();
	org.minnal.examples.petclinic.domain.Specialty specialty = createDomain(org.minnal.examples.petclinic.domain.Specialty.class);
	vet.collection("specialties").add(specialty);
	vet.persist();
	org.minnal.examples.petclinic.domain.Specialty modifiedspecialty = createDomain(
			org.minnal.examples.petclinic.domain.Specialty.class,
			1);
	ContainerResponse response = call(request(
			"/vets/" + vet.getId() + "/specialties/"
					+ specialty.getId(),
			HttpMethod.PUT, serialize(modifiedspecialty)));
	assertEquals(response.getStatus(),
			Response.Status.NO_CONTENT.getStatusCode());
	specialty.merge();
	assertTrue(compare(modifiedspecialty, specialty, 1));
}
 
Example #10
Source File: DefaultJerseyTagsProviderTest.java    From micrometer with Apache License 2.0 6 votes vote down vote up
private static RequestEvent event(Integer status, Exception exception, String baseUri, String... uriTemplateStrings) {
    Builder builder = new RequestEventImpl.Builder();

    ContainerRequest containerRequest = mock(ContainerRequest.class);
    when(containerRequest.getMethod()).thenReturn("GET");
    builder.setContainerRequest(containerRequest);

    ContainerResponse containerResponse = mock(ContainerResponse.class);
    when(containerResponse.getStatus()).thenReturn(status);
    builder.setContainerResponse(containerResponse);

    builder.setException(exception, null);

    ExtendedUriInfo extendedUriInfo = mock(ExtendedUriInfo.class);
    when(extendedUriInfo.getBaseUri()).thenReturn(
        URI.create("http://localhost:8080" + (baseUri == null ? "/" : baseUri)));
    List<UriTemplate> uriTemplates = uriTemplateStrings == null ? Collections.emptyList()
        : Arrays.stream(uriTemplateStrings).map(uri -> new UriTemplate(uri))
        .collect(Collectors.toList());
    // UriTemplate are returned in reverse order
    Collections.reverse(uriTemplates);
    when(extendedUriInfo.getMatchedTemplates()).thenReturn(uriTemplates);
    builder.setExtendedUriInfo(extendedUriInfo);

    return builder.build(Type.FINISHED);
}
 
Example #11
Source File: OwnerResourceTest.java    From minnal with Apache License 2.0 6 votes vote down vote up
@Test
public void readOwnerPetVisitTest() {
	org.minnal.examples.petclinic.domain.Owner owner = createDomain(org.minnal.examples.petclinic.domain.Owner.class);
	owner.persist();
	org.minnal.examples.petclinic.domain.Pet pet = createDomain(org.minnal.examples.petclinic.domain.Pet.class);
	owner.collection("pets").add(pet);
	owner.persist();
	org.minnal.examples.petclinic.domain.Visit visit = createDomain(org.minnal.examples.petclinic.domain.Visit.class);
	pet.collection("visits").add(visit);
	pet.persist();
	ContainerResponse response = call(request(
			"/owners/" + owner.getId() + "/pets/"
					+ pet.getId() + "/visits/"
					+ visit.getId(), HttpMethod.GET));
	assertEquals(response.getStatus(),
			Response.Status.OK.getStatusCode());
	assertEquals(deserialize(
			getContent(response),
			org.minnal.examples.petclinic.domain.Visit.class)
			.getId(), visit.getId());
}
 
Example #12
Source File: OwnerResourceTest.java    From minnal with Apache License 2.0 6 votes vote down vote up
@Test
public void updateOwnerPetVisitTest() {
	org.minnal.examples.petclinic.domain.Owner owner = createDomain(org.minnal.examples.petclinic.domain.Owner.class);
	owner.persist();
	org.minnal.examples.petclinic.domain.Pet pet = createDomain(org.minnal.examples.petclinic.domain.Pet.class);
	owner.collection("pets").add(pet);
	owner.persist();
	org.minnal.examples.petclinic.domain.Visit visit = createDomain(org.minnal.examples.petclinic.domain.Visit.class);
	pet.collection("visits").add(visit);
	pet.persist();
	org.minnal.examples.petclinic.domain.Visit modifiedvisit = createDomain(
			org.minnal.examples.petclinic.domain.Visit.class,
			1);
	ContainerResponse response = call(request(
			"/owners/" + owner.getId() + "/pets/"
					+ pet.getId() + "/visits/"
					+ visit.getId(),
			HttpMethod.PUT, serialize(modifiedvisit)));
	assertEquals(response.getStatus(),
			Response.Status.NO_CONTENT.getStatusCode());
	visit.merge();
	assertTrue(compare(modifiedvisit, visit, 1));
}
 
Example #13
Source File: UndertowResponseWriter.java    From jweb-cms with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public OutputStream writeResponseStatusAndHeaders(long contentLength, ContainerResponse responseContext) throws ContainerException {
    if (error != null) {
        exchange.setStatusCode(500);
        exchange.getResponseHeaders().add(new HttpString("Content-Type"), "text/plain");
        exchange.getResponseSender().send(Exceptions.stackTrace(error));
        return null;
    } else {
        exchange.startBlocking();
        exchange.setStatusCode(responseContext.getStatus());
        HeaderMap responseHeaders = exchange.getResponseHeaders();
        responseContext.getStringHeaders().forEach((key, value) -> {
            for (String v : value) {
                responseHeaders.add(new HttpString(key), v);
            }
        });
        exchange.setResponseContentLength(contentLength);
        return exchange.getOutputStream();
    }
}
 
Example #14
Source File: OrderResourceTest.java    From minnal with Apache License 2.0 6 votes vote down vote up
@Test
public void readOrderPaymentTest() {
	org.minnal.examples.oms.domain.Order order = createDomain(org.minnal.examples.oms.domain.Order.class);
	order.persist();
	org.minnal.examples.oms.domain.Payment payment = createDomain(org.minnal.examples.oms.domain.Payment.class);
	order.collection("payments").add(payment);
	order.persist();
	ContainerResponse response = call(request(
			"/orders/" + order.getId() + "/payments/"
					+ payment.getId(),
			HttpMethod.GET));
	assertEquals(response.getStatus(),
			Response.Status.OK.getStatusCode());
	assertEquals(deserialize(getContent(response),
			org.minnal.examples.oms.domain.Payment.class)
			.getId(), payment.getId());
}
 
Example #15
Source File: PetTypeResourceTest.java    From minnal with Apache License 2.0 6 votes vote down vote up
@Test
public void listPetTypeTest() {
	org.minnal.examples.petclinic.domain.PetType petType = createDomain(org.minnal.examples.petclinic.domain.PetType.class);
	petType.persist();
	ContainerResponse response = call(request("/pet_types/",
			HttpMethod.GET));
	assertEquals(response.getStatus(),
			Response.Status.OK.getStatusCode());
	assertEquals(deserializeCollection(
			getContent(response),
			java.util.List.class,
			org.minnal.examples.petclinic.domain.PetType.class)
			.size(),
			(int) org.minnal.examples.petclinic.domain.PetType
					.count());
}
 
Example #16
Source File: OwnerResourceTest.java    From minnal with Apache License 2.0 6 votes vote down vote up
@Test
public void deleteOwnerPetTest() {
	org.minnal.examples.petclinic.domain.Owner owner = createDomain(org.minnal.examples.petclinic.domain.Owner.class);
	owner.persist();
	org.minnal.examples.petclinic.domain.Pet pet = createDomain(org.minnal.examples.petclinic.domain.Pet.class);
	owner.collection("pets").add(pet);
	owner.persist();
	ContainerResponse response = call(request(
			"/owners/" + owner.getId() + "/pets/"
					+ pet.getId(),
			HttpMethod.DELETE));
	assertEquals(response.getStatus(),
			Response.Status.NO_CONTENT.getStatusCode());
	response = call(request("/owners/" + owner.getId() + "/pets/"
			+ pet.getId(), HttpMethod.GET, serialize(pet)));
	assertEquals(response.getStatus(),
			Response.Status.NOT_FOUND.getStatusCode());
}
 
Example #17
Source File: OrderResourceTest.java    From minnal with Apache License 2.0 6 votes vote down vote up
@Test
public void listOrderPaymentTest() {
	org.minnal.examples.oms.domain.Order order = createDomain(org.minnal.examples.oms.domain.Order.class);
	order.persist();

	org.minnal.examples.oms.domain.Payment payment = createDomain(org.minnal.examples.oms.domain.Payment.class);
	order.collection("payments").add(payment);
	order.persist();

	ContainerResponse response = call(request(
			"/orders/" + order.getId() + "/payments/",
			HttpMethod.GET));
	assertEquals(response.getStatus(),
			Response.Status.OK.getStatusCode());
	assertEquals(deserializeCollection(getContent(response),
			java.util.List.class,
			org.minnal.examples.oms.domain.Payment.class)
			.size(), order.getPayments().size());
}
 
Example #18
Source File: OrderResourceTest.java    From minnal with Apache License 2.0 6 votes vote down vote up
@Test
public void deleteOrderPaymentTest() {
	org.minnal.examples.oms.domain.Order order = createDomain(org.minnal.examples.oms.domain.Order.class);
	order.persist();
	org.minnal.examples.oms.domain.Payment payment = createDomain(org.minnal.examples.oms.domain.Payment.class);
	order.collection("payments").add(payment);
	order.persist();
	ContainerResponse response = call(request(
			"/orders/" + order.getId() + "/payments/"
					+ payment.getId(),
			HttpMethod.DELETE));
	assertEquals(response.getStatus(),
			Response.Status.NO_CONTENT.getStatusCode());
	response = call(request("/orders/" + order.getId()
			+ "/payments/" + payment.getId(),
			HttpMethod.GET, serialize(payment)));
	assertEquals(response.getStatus(),
			Response.Status.NOT_FOUND.getStatusCode());
}
 
Example #19
Source File: OrderResourceTest.java    From minnal with Apache License 2.0 6 votes vote down vote up
@Test
public void deleteOrderOrderItemTest() {
	org.minnal.examples.oms.domain.Order order = createDomain(org.minnal.examples.oms.domain.Order.class);
	order.persist();
	org.minnal.examples.oms.domain.OrderItem orderItem = createDomain(org.minnal.examples.oms.domain.OrderItem.class);
	order.collection("orderItems").add(orderItem);
	order.persist();
	ContainerResponse response = call(request(
			"/orders/" + order.getId() + "/order_items/"
					+ orderItem.getId(),
			HttpMethod.DELETE));
	assertEquals(response.getStatus(),
			Response.Status.NO_CONTENT.getStatusCode());
	response = call(request("/orders/" + order.getId()
			+ "/order_items/" + orderItem.getId(),
			HttpMethod.GET, serialize(orderItem)));
	assertEquals(response.getStatus(),
			Response.Status.NOT_FOUND.getStatusCode());
}
 
Example #20
Source File: HttpHandlerContainer.java    From metrics with Apache License 2.0 6 votes vote down vote up
@Override
public OutputStream writeResponseStatusAndHeaders(final long contentLength, final ContainerResponse context)
        throws ContainerException {
    final MultivaluedMap<String, String> responseHeaders = context.getStringHeaders();
    final Headers serverHeaders = exchange.getResponseHeaders();
    for (final Map.Entry<String, List<String>> e : responseHeaders.entrySet()) {
        for (final String value : e.getValue()) {
            serverHeaders.add(e.getKey(), value);
        }
    }

    try {
        if (context.getStatus() == Response.Status.NO_CONTENT.getStatusCode()) {
            // Work around bug in LW HTTP server
            // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6886436
            exchange.sendResponseHeaders(context.getStatus(), -1);
        } else {
            exchange.sendResponseHeaders(context.getStatus(),
                    getResponseLength(contentLength));
        }
    } catch (final IOException ioe) {
        throw new ContainerException(LocalizationMessages.ERROR_RESPONSEWRITER_WRITING_HEADERS(), ioe);
    }

    return exchange.getResponseBody();
}
 
Example #21
Source File: ProductResourceTest.java    From minnal with Apache License 2.0 5 votes vote down vote up
@Test
public void createProductTest() {
	org.minnal.examples.oms.domain.Product product = createDomain(org.minnal.examples.oms.domain.Product.class);
	ContainerResponse response = call(request("/products/",
			HttpMethod.POST, serialize(product)));
	assertEquals(response.getStatus(),
			Response.Status.CREATED.getStatusCode());
}
 
Example #22
Source File: OwnerResourceTest.java    From minnal with Apache License 2.0 5 votes vote down vote up
@Test
public void createOwnerTest() {
	org.minnal.examples.petclinic.domain.Owner owner = createDomain(org.minnal.examples.petclinic.domain.Owner.class);
	ContainerResponse response = call(request("/owners/",
			HttpMethod.POST, serialize(owner)));
	assertEquals(response.getStatus(),
			Response.Status.CREATED.getStatusCode());
}
 
Example #23
Source File: OwnerResourceTest.java    From minnal with Apache License 2.0 5 votes vote down vote up
@Test
public void updateOwnerTest() {
	org.minnal.examples.petclinic.domain.Owner owner = createDomain(org.minnal.examples.petclinic.domain.Owner.class);
	owner.persist();
	org.minnal.examples.petclinic.domain.Owner modifiedowner = createDomain(
			org.minnal.examples.petclinic.domain.Owner.class,
			1);
	ContainerResponse response = call(request(
			"/owners/" + owner.getId(), HttpMethod.PUT,
			serialize(modifiedowner)));
	assertEquals(response.getStatus(),
			Response.Status.NO_CONTENT.getStatusCode());
	owner.merge();
	assertTrue(compare(modifiedowner, owner, 1));
}
 
Example #24
Source File: PetTypeResourceTest.java    From minnal with Apache License 2.0 5 votes vote down vote up
@Test
public void readPetTypeTest() {
	org.minnal.examples.petclinic.domain.PetType petType = createDomain(org.minnal.examples.petclinic.domain.PetType.class);
	petType.persist();
	ContainerResponse response = call(request("/pet_types/"
			+ petType.getId(), HttpMethod.GET));
	assertEquals(response.getStatus(),
			Response.Status.OK.getStatusCode());
	assertEquals(deserialize(
			getContent(response),
			org.minnal.examples.petclinic.domain.PetType.class)
			.getId(), petType.getId());
}
 
Example #25
Source File: FileWebServiceImplTest.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
void deleteAndRevert() {
    ContainerResponse containerResponse = app.delete("/api/file/" + file.id + "?requestBy=" + UUID.randomUUID().toString()).execute();
    assertEquals(204, containerResponse.getStatus());
    FileResponse fileResponse = fileWebService.get(file.id);
    assertEquals(FileStatus.INACTIVE, fileResponse.status);
    containerResponse = app.put("/api/file/" + file.id + "/revert?requestBy=" + UUID.randomUUID().toString()).execute();
    assertEquals(204, containerResponse.getStatus());
    fileResponse = fileWebService.get(file.id);
    assertEquals(FileStatus.ACTIVE, fileResponse.status);
}
 
Example #26
Source File: OrderResourceTest.java    From minnal with Apache License 2.0 5 votes vote down vote up
@Test
public void createOrderOrderItemTest() {
	org.minnal.examples.oms.domain.Order order = createDomain(org.minnal.examples.oms.domain.Order.class);
	order.persist();
	org.minnal.examples.oms.domain.OrderItem orderItem = createDomain(org.minnal.examples.oms.domain.OrderItem.class);
	ContainerResponse response = call(request(
			"/orders/" + order.getId() + "/order_items/",
			HttpMethod.POST, serialize(orderItem)));
	assertEquals(response.getStatus(),
			Response.Status.CREATED.getStatusCode());
}
 
Example #27
Source File: OwnerResourceTest.java    From minnal with Apache License 2.0 5 votes vote down vote up
@Test
public void createOwnerPetTest() {
	org.minnal.examples.petclinic.domain.Owner owner = createDomain(org.minnal.examples.petclinic.domain.Owner.class);
	owner.persist();
	org.minnal.examples.petclinic.domain.Pet pet = createDomain(org.minnal.examples.petclinic.domain.Pet.class);
	ContainerResponse response = call(request(
			"/owners/" + owner.getId() + "/pets/",
			HttpMethod.POST, serialize(pet)));
	assertEquals(response.getStatus(),
			Response.Status.CREATED.getStatusCode());
}
 
Example #28
Source File: FileWebServiceImplTest.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
void list() {
    FileListQuery fileListQuery = new FileListQuery();
    fileListQuery.limit = 10;
    fileListQuery.page = 1;
    ContainerResponse containerResponse = app.put("/api/file/list").setEntity(fileListQuery).execute();
    assertEquals(200, containerResponse.getStatus());
    QueryResponse<FileListResponse> queryResponse = (QueryResponse) containerResponse.getEntity();
    assertTrue(queryResponse.total.intValue() >= 2);
    queryResponse.items.forEach(fileListResponse -> {
        if (!fileListResponse.isDirectory) {
            assertEquals(file.description, fileListResponse.description);
        }
    });
}
 
Example #29
Source File: StatusMapper.java    From ameba with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void filter(ContainerRequestContext requestContext,
                   ContainerResponseContext responseContext) throws IOException {
    if (!responseContext.hasEntity()
            && !((ContainerResponse) responseContext).isMappedFromException()
            && responseContext.getStatus() >= 400
            && responseContext.getStatus() < 600) {
        throw new WebApplicationException(responseContext.getStatus());
    }

    List<MediaType> types = null;
    // 找不到 writer/reader, 提供系统有的 writer/reader 输出错误
    if (responseContext.getStatus() == 406) {
        types = workersProvider.get()
                .getMessageBodyWriterMediaTypes(
                        responseContext.getEntityClass(),
                        responseContext.getEntityType(),
                        responseContext.getEntityAnnotations());
    } else if (responseContext.getStatus() == 415) {
        types = workersProvider.get()
                .getMessageBodyReaderMediaTypes(
                        responseContext.getEntityClass(),
                        responseContext.getEntityType(),
                        responseContext.getEntityAnnotations());
    }
    if (types != null) {
        ((ContainerResponse) responseContext).setMediaType(parseMediaType(types));
    }
}
 
Example #30
Source File: VetResourceTest.java    From minnal with Apache License 2.0 5 votes vote down vote up
@Test
public void listVetTest() {
	org.minnal.examples.petclinic.domain.Vet vet = createDomain(org.minnal.examples.petclinic.domain.Vet.class);
	vet.persist();
	ContainerResponse response = call(request("/vets/",
			HttpMethod.GET));
	assertEquals(response.getStatus(),
			Response.Status.OK.getStatusCode());
	assertEquals(deserializeCollection(getContent(response),
			java.util.List.class,
			org.minnal.examples.petclinic.domain.Vet.class)
			.size(),
			(int) org.minnal.examples.petclinic.domain.Vet
					.count());
}