Search in sources :

Example 1 with Response

use of javax.ws.rs.core.Response in project dropwizard by dropwizard.

the class LoggingExceptionMapperTest method handlesMethodNotAllowedWithHeaders.

@Test
public void handlesMethodNotAllowedWithHeaders() {
    final Throwable thrown = catchThrowable(() -> target("/exception/json-mapping-exception").request(MediaType.APPLICATION_JSON).post(Entity.json("A"), String.class));
    assertThat(thrown).isInstanceOf(WebApplicationException.class);
    final Response resp = ((WebApplicationException) thrown).getResponse();
    assertThat(resp.getStatus()).isEqualTo(405);
    assertThat(resp.getHeaders()).contains(entry("Allow", ImmutableList.of("GET,OPTIONS")));
    assertThat(resp.readEntity(String.class)).isEqualTo("{\"code\":405,\"message\":\"HTTP 405 Method Not Allowed\"}");
}
Also used : Response(javax.ws.rs.core.Response) WebApplicationException(javax.ws.rs.WebApplicationException) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) AbstractJerseyTest(io.dropwizard.jersey.AbstractJerseyTest) Test(org.junit.Test)

Example 2 with Response

use of javax.ws.rs.core.Response in project druid by druid-io.

the class NamespaceLookupExtractorFactoryTest method validateNotFound.

private void validateNotFound(String method, LookupIntrospectHandler handler, Class<? extends LookupIntrospectHandler> clazz) throws Exception {
    mockVerify();
    mockReset();
    expectEntryGetCacheStateOnce(versionedCache);
    expectEmptyCache();
    EasyMock.expect(versionedCache.getVersion()).andThrow(new ISE("some exception")).once();
    mockReplay();
    final Response response = (Response) clazz.getMethod(method).invoke(handler);
    Assert.assertEquals(404, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) ISE(io.druid.java.util.common.ISE)

Example 3 with Response

use of javax.ws.rs.core.Response in project druid by druid-io.

the class OverlordResource method taskPost.

@POST
@Path("/task")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response taskPost(final Task task, @Context final HttpServletRequest req) {
    if (authConfig.isEnabled()) {
        // This is an experimental feature, see - https://github.com/druid-io/druid/pull/2424
        final String dataSource = task.getDataSource();
        final AuthorizationInfo authorizationInfo = (AuthorizationInfo) req.getAttribute(AuthConfig.DRUID_AUTH_TOKEN);
        Preconditions.checkNotNull(authorizationInfo, "Security is enabled but no authorization info found in the request");
        Access authResult = authorizationInfo.isAuthorized(new Resource(dataSource, ResourceType.DATASOURCE), Action.WRITE);
        if (!authResult.isAllowed()) {
            return Response.status(Response.Status.FORBIDDEN).header("Access-Check-Result", authResult).build();
        }
    }
    return asLeaderWith(taskMaster.getTaskQueue(), new Function<TaskQueue, Response>() {

        @Override
        public Response apply(TaskQueue taskQueue) {
            try {
                taskQueue.add(task);
                return Response.ok(ImmutableMap.of("task", task.getId())).build();
            } catch (EntryExistsException e) {
                return Response.status(Response.Status.BAD_REQUEST).entity(ImmutableMap.of("error", String.format("Task[%s] already exists!", task.getId()))).build();
            }
        }
    });
}
Also used : Response(javax.ws.rs.core.Response) Access(io.druid.server.security.Access) Resource(io.druid.server.security.Resource) TaskQueue(io.druid.indexing.overlord.TaskQueue) EntryExistsException(io.druid.metadata.EntryExistsException) AuthorizationInfo(io.druid.server.security.AuthorizationInfo) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 4 with Response

use of javax.ws.rs.core.Response in project druid by druid-io.

the class SupervisorResourceTest method testShutdown.

@Test
public void testShutdown() throws Exception {
    EasyMock.expect(taskMaster.getSupervisorManager()).andReturn(Optional.of(supervisorManager)).times(2);
    EasyMock.expect(supervisorManager.stopAndRemoveSupervisor("my-id")).andReturn(true);
    EasyMock.expect(supervisorManager.stopAndRemoveSupervisor("my-id-2")).andReturn(false);
    replayAll();
    Response response = supervisorResource.shutdown("my-id");
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(ImmutableMap.of("id", "my-id"), response.getEntity());
    response = supervisorResource.shutdown("my-id-2");
    Assert.assertEquals(404, response.getStatus());
    verifyAll();
    resetAll();
    EasyMock.expect(taskMaster.getSupervisorManager()).andReturn(Optional.<SupervisorManager>absent());
    replayAll();
    response = supervisorResource.shutdown("my-id");
    verifyAll();
    Assert.assertEquals(503, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) Test(org.junit.Test)

Example 5 with Response

use of javax.ws.rs.core.Response in project druid by druid-io.

the class SupervisorResourceTest method testSpecGetStatus.

@Test
public void testSpecGetStatus() throws Exception {
    SupervisorReport report = new SupervisorReport("id", DateTime.now()) {

        @Override
        public Object getPayload() {
            return null;
        }
    };
    EasyMock.expect(taskMaster.getSupervisorManager()).andReturn(Optional.of(supervisorManager)).times(2);
    EasyMock.expect(supervisorManager.getSupervisorStatus("my-id")).andReturn(Optional.of(report));
    EasyMock.expect(supervisorManager.getSupervisorStatus("my-id-2")).andReturn(Optional.<SupervisorReport>absent());
    replayAll();
    Response response = supervisorResource.specGetStatus("my-id");
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(report, response.getEntity());
    response = supervisorResource.specGetStatus("my-id-2");
    Assert.assertEquals(404, response.getStatus());
    verifyAll();
    resetAll();
    EasyMock.expect(taskMaster.getSupervisorManager()).andReturn(Optional.<SupervisorManager>absent());
    replayAll();
    response = supervisorResource.specGetStatus("my-id");
    verifyAll();
    Assert.assertEquals(503, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) Test(org.junit.Test)

Aggregations

Response (javax.ws.rs.core.Response)10204 Test (org.junit.Test)5764 Test (org.testng.annotations.Test)1113 JerseyTest (org.glassfish.jersey.test.JerseyTest)870 Test (org.junit.jupiter.api.Test)701 WebTarget (javax.ws.rs.client.WebTarget)667 Builder (javax.ws.rs.client.Invocation.Builder)638 WebClient (org.apache.cxf.jaxrs.client.WebClient)620 DBUnitTest (org.orcid.test.DBUnitTest)611 ResteasyClientBuilder (org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder)577 Parameters (org.testng.annotations.Parameters)537 URI (java.net.URI)515 HashMap (java.util.HashMap)508 Path (javax.ws.rs.Path)506 List (java.util.List)465 ArrayList (java.util.ArrayList)419 Produces (javax.ws.rs.Produces)396 IOException (java.io.IOException)393 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)354 GET (javax.ws.rs.GET)348