Search in sources :

Example 96 with GenericType

use of javax.ws.rs.core.GenericType in project syncope by apache.

the class RestClientExceptionMapper method checkSyncopeClientCompositeException.

private SyncopeClientCompositeException checkSyncopeClientCompositeException(final Response response) {
    SyncopeClientCompositeException compException = SyncopeClientException.buildComposite();
    // Attempts to read ErrorTO or List<ErrorTO> as entity...
    List<ErrorTO> errors = null;
    try {
        ErrorTO error = response.readEntity(ErrorTO.class);
        if (error != null) {
            errors = Collections.singletonList(error);
        }
    } catch (Exception e) {
        LOG.debug("Could not read {}, attempting to read composite...", ErrorTO.class.getName(), e);
    }
    if (errors == null) {
        try {
            errors = response.readEntity(new GenericType<List<ErrorTO>>() {
            });
        } catch (Exception e) {
            LOG.debug("Could not read {} list, attempting to read headers...", ErrorTO.class.getName(), e);
        }
    }
    // ...if not possible, attempts to parse response headers
    if (errors == null) {
        List<String> exTypesInHeaders = response.getStringHeaders().get(RESTHeaders.ERROR_CODE);
        if (exTypesInHeaders == null) {
            LOG.debug("No " + RESTHeaders.ERROR_CODE + " provided");
            return null;
        }
        List<String> exInfos = response.getStringHeaders().get(RESTHeaders.ERROR_INFO);
        Set<String> handledExceptions = new HashSet<>();
        exTypesInHeaders.forEach(exTypeAsString -> {
            ClientExceptionType exceptionType = null;
            try {
                exceptionType = ClientExceptionType.fromHeaderValue(exTypeAsString);
            } catch (IllegalArgumentException e) {
                LOG.error("Unexpected value of " + RESTHeaders.ERROR_CODE + ": " + exTypeAsString, e);
            }
            if (exceptionType != null) {
                handledExceptions.add(exTypeAsString);
                SyncopeClientException clientException = SyncopeClientException.build(exceptionType);
                if (exInfos != null && !exInfos.isEmpty()) {
                    for (String element : exInfos) {
                        if (element.startsWith(exceptionType.name())) {
                            clientException.getElements().add(StringUtils.substringAfter(element, ":"));
                        }
                    }
                }
                compException.addException(clientException);
            }
        });
        exTypesInHeaders.removeAll(handledExceptions);
        if (!exTypesInHeaders.isEmpty()) {
            LOG.error("Unmanaged exceptions: " + exTypesInHeaders);
        }
    } else {
        for (ErrorTO error : errors) {
            SyncopeClientException clientException = SyncopeClientException.build(error.getType());
            clientException.getElements().addAll(error.getElements());
            compException.addException(clientException);
        }
    }
    if (compException.hasExceptions()) {
        return compException;
    }
    return null;
}
Also used : SyncopeClientCompositeException(org.apache.syncope.common.lib.SyncopeClientCompositeException) ErrorTO(org.apache.syncope.common.lib.to.ErrorTO) GenericType(javax.ws.rs.core.GenericType) ClientExceptionType(org.apache.syncope.common.lib.types.ClientExceptionType) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) ForbiddenException(javax.ws.rs.ForbiddenException) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) WebServiceException(javax.xml.ws.WebServiceException) AccessControlException(java.security.AccessControlException) BadRequestException(javax.ws.rs.BadRequestException) SyncopeClientCompositeException(org.apache.syncope.common.lib.SyncopeClientCompositeException) HashSet(java.util.HashSet)

Example 97 with GenericType

use of javax.ws.rs.core.GenericType in project vespa by vespa-engine.

the class ApplicationSuspensionResourceTest method list_applications_returns_the_correct_list_of_suspended_applications.

@Test
public void list_applications_returns_the_correct_list_of_suspended_applications() throws Exception {
    // Test that initially we have the empty set
    Response reply = webTarget.request(MediaType.APPLICATION_JSON).get();
    assertEquals(200, reply.getStatus());
    assertEquals("[]", reply.readEntity(String.class));
    // Add a couple of applications to maintenance
    webTarget.request().post(Entity.entity(RESOURCE_1, MediaType.APPLICATION_JSON_TYPE));
    webTarget.request().post(Entity.entity(RESOURCE_2, MediaType.APPLICATION_JSON_TYPE));
    assertEquals(200, reply.getStatus());
    // Test that we get them back
    Set<String> responses = webTarget.request(MediaType.APPLICATION_JSON_TYPE).get(new GenericType<Set<String>>() {
    });
    assertEquals(2, responses.size());
    // Remove suspend for the first resource
    webTarget.path(RESOURCE_1).request().delete();
    // Test that we are back to the start with the empty set
    responses = webTarget.request(MediaType.APPLICATION_JSON_TYPE).get(new GenericType<Set<String>>() {
    });
    assertEquals(1, responses.size());
    assertEquals(RESOURCE_2, responses.iterator().next());
}
Also used : Response(javax.ws.rs.core.Response) GenericType(javax.ws.rs.core.GenericType) Set(java.util.Set) Test(org.junit.Test)

Example 98 with GenericType

use of javax.ws.rs.core.GenericType in project pravega by pravega.

the class StreamMetaDataTests method testGetScalingEvents.

/**
 * Test for getScalingEvents REST API.
 */
@Test(timeout = 30000)
public void testGetScalingEvents() throws Exception {
    String resourceURI = getURI() + "v1/scopes/scope1/streams/stream1/scaling-events";
    List<ScaleMetadata> scaleMetadataList = new ArrayList<>();
    /* Test to get scaling events

        There are 5 scale events in final list.
        Filter 'from' and 'to' is also tested here.
        Event1 is before 'from'
        Event2 is before 'from'
        Event3 and Event4 are between 'from' and 'to'
        Event5 is after 'to'
        Response contains 3 events : Event2 acts as reference event. Event3 and Event4 fall between 'from' and 'to'.
         */
    Segment segment1 = new Segment(0, 0, System.currentTimeMillis(), 0.00, 0.50);
    Segment segment2 = new Segment(1, 0, System.currentTimeMillis(), 0.50, 1.00);
    List<Segment> segmentList1 = Arrays.asList(segment1, segment2);
    ScaleMetadata scaleMetadata1 = new ScaleMetadata(System.currentTimeMillis() / 2, segmentList1, 0L, 0L);
    Segment segment3 = new Segment(2, 0, System.currentTimeMillis(), 0.00, 0.40);
    Segment segment4 = new Segment(3, 0, System.currentTimeMillis(), 0.40, 1.00);
    List<Segment> segmentList2 = Arrays.asList(segment3, segment4);
    ScaleMetadata scaleMetadata2 = new ScaleMetadata(1 + System.currentTimeMillis() / 2, segmentList2, 1L, 1L);
    long fromDateTime = System.currentTimeMillis();
    Segment segment5 = new Segment(4, 0, System.currentTimeMillis(), 0.00, 0.50);
    Segment segment6 = new Segment(5, 0, System.currentTimeMillis(), 0.50, 1.00);
    List<Segment> segmentList3 = Arrays.asList(segment5, segment6);
    ScaleMetadata scaleMetadata3 = new ScaleMetadata(System.currentTimeMillis(), segmentList3, 1L, 1L);
    Segment segment7 = new Segment(6, 0, System.currentTimeMillis(), 0.00, 0.25);
    Segment segment8 = new Segment(7, 0, System.currentTimeMillis(), 0.25, 1.00);
    List<Segment> segmentList4 = Arrays.asList(segment7, segment8);
    ScaleMetadata scaleMetadata4 = new ScaleMetadata(System.currentTimeMillis(), segmentList4, 1L, 1L);
    long toDateTime = System.currentTimeMillis();
    Segment segment9 = new Segment(8, 0, System.currentTimeMillis(), 0.00, 0.40);
    Segment segment10 = new Segment(9, 0, System.currentTimeMillis(), 0.40, 1.00);
    List<Segment> segmentList5 = Arrays.asList(segment9, segment10);
    ScaleMetadata scaleMetadata5 = new ScaleMetadata(toDateTime * 2, segmentList5, 1L, 1L);
    // HistoryRecords.readAllRecords returns a list of records in decreasing order
    // so we add the elements in reverse order as well to simulate that behavior
    scaleMetadataList.add(scaleMetadata5);
    scaleMetadataList.add(scaleMetadata4);
    scaleMetadataList.add(scaleMetadata3);
    scaleMetadataList.add(scaleMetadata2);
    scaleMetadataList.add(scaleMetadata1);
    doAnswer(x -> CompletableFuture.completedFuture(scaleMetadataList)).when(mockControllerService).getScaleRecords(anyString(), anyString(), anyLong(), anyLong(), anyLong());
    Response response = addAuthHeaders(client.target(resourceURI).queryParam("from", fromDateTime).queryParam("to", toDateTime).request()).buildGet().invoke();
    assertEquals("Get Scaling Events response code", 200, response.getStatus());
    assertTrue(response.bufferEntity());
    List<ScaleMetadata> scaleMetadataListResponse = response.readEntity(new GenericType<List<ScaleMetadata>>() {
    });
    assertEquals("List Size", 3, scaleMetadataListResponse.size());
    scaleMetadataListResponse.forEach(data -> {
        log.warn("Here");
        data.getSegments().forEach(segment -> {
            log.debug("Checking segment number: " + segment.segmentId());
            assertTrue("Event 1 shouldn't be included", segment.segmentId() != 0L);
        });
    });
    // Test for large number of scaling events.
    scaleMetadataList.clear();
    scaleMetadataList.addAll(Collections.nCopies(50000, scaleMetadata3));
    doAnswer(x -> CompletableFuture.completedFuture(scaleMetadataList)).when(mockControllerService).getScaleRecords(anyString(), anyString(), anyLong(), anyLong(), anyLong());
    response = addAuthHeaders(client.target(resourceURI).queryParam("from", fromDateTime).queryParam("to", toDateTime).request()).buildGet().invoke();
    assertEquals("Get Scaling Events response code", 200, response.getStatus());
    assertTrue(response.bufferEntity());
    scaleMetadataListResponse = response.readEntity(new GenericType<List<ScaleMetadata>>() {
    });
    assertEquals("List Size", 50000, scaleMetadataListResponse.size());
    // Test for getScalingEvents for invalid scope/stream.
    final CompletableFuture<List<ScaleMetadata>> completableFuture1 = new CompletableFuture<>();
    completableFuture1.completeExceptionally(StoreException.create(StoreException.Type.DATA_NOT_FOUND, "stream1"));
    doAnswer(x -> completableFuture1).when(mockControllerService).getScaleRecords(anyString(), anyString(), anyLong(), anyLong(), anyLong());
    response = addAuthHeaders(client.target(resourceURI).queryParam("from", fromDateTime).queryParam("to", toDateTime).request()).buildGet().invoke();
    assertEquals("Get Scaling Events response code", 404, response.getStatus());
    // Test for getScalingEvents for bad request.
    // 1. Missing query parameters are validated here.
    response = addAuthHeaders(client.target(resourceURI).request()).buildGet().invoke();
    assertEquals("Get Scaling Events response code", 400, response.getStatus());
    response = addAuthHeaders(client.target(resourceURI).queryParam("from", fromDateTime).request()).buildGet().invoke();
    assertEquals("Get Scaling Events response code", 400, response.getStatus());
    // 2. from > to is tested here.
    doAnswer(x -> CompletableFuture.completedFuture(scaleMetadataList)).when(mockControllerService).getScaleRecords(anyString(), anyString(), anyLong(), anyLong(), anyLong());
    response = addAuthHeaders(client.target(resourceURI).queryParam("from", fromDateTime * 2).queryParam("to", fromDateTime).request()).buildGet().invoke();
    assertEquals("Get Scaling Events response code", 400, response.getStatus());
    // Test for getScalingEvents failure.
    final CompletableFuture<List<ScaleMetadata>> completableFuture = new CompletableFuture<>();
    completableFuture.completeExceptionally(new Exception());
    doAnswer(x -> completableFuture).when(mockControllerService).getScaleRecords(anyString(), anyString(), anyLong(), anyLong(), anyLong());
    response = addAuthHeaders(client.target(resourceURI).queryParam("from", fromDateTime).queryParam("to", toDateTime).request()).buildGet().invoke();
    assertEquals("Get Scaling Events response code", 500, response.getStatus());
}
Also used : ScaleMetadata(io.pravega.controller.store.stream.ScaleMetadata) GenericType(javax.ws.rs.core.GenericType) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Segment(io.pravega.controller.store.stream.Segment) StoreException(io.pravega.controller.store.stream.StoreException) ExecutionException(java.util.concurrent.ExecutionException) Response(javax.ws.rs.core.Response) CompletableFuture(java.util.concurrent.CompletableFuture) StreamsList(io.pravega.controller.server.rest.generated.model.StreamsList) List(java.util.List) ScopesList(io.pravega.controller.server.rest.generated.model.ScopesList) ArrayList(java.util.ArrayList) ReaderGroupsList(io.pravega.controller.server.rest.generated.model.ReaderGroupsList) Test(org.junit.Test)

Example 99 with GenericType

use of javax.ws.rs.core.GenericType in project assembly64fx by freabemania.

the class PlaylistService method getPublicPlaylists.

public List<PlaylistInfo> getPublicPlaylists() {
    if (publicPlaylists == null) {
        publicPlaylists = getClient().target(getBackendServer()).path("/leet/playlistsnew/publicplaylists").request().header("email", getSessionInfo().getEmail()).header("token", getSessionInfo().getToken()).get(new GenericType<List<PlaylistInfo>>() {
        });
        Set<String> myPlaylistIds = myPlaylistIds();
        for (PlaylistInfo item : publicPlaylists) {
            if (!myPlaylistIds.contains(item.getId())) {
                item.setMine(false);
            }
            item.setPublicList(true);
        }
        flushPlaylistInfo();
        flushPublicPlaylistInfo(publicPlaylists);
    }
    return publicPlaylists;
}
Also used : GenericType(javax.ws.rs.core.GenericType) PlaylistInfo(se.light.assembly64.model.PlaylistInfo)

Example 100 with GenericType

use of javax.ws.rs.core.GenericType in project batfish by batfish.

the class WorkMgrServiceV2Test method getContainers.

@Test
public void getContainers() {
    Response response = getContainersTarget().request().get();
    assertThat(response.getStatus(), equalTo(OK.getStatusCode()));
    assertThat(response.readEntity(new GenericType<List<Container>>() {
    }), empty());
    Main.getWorkMgr().initContainer("someContainer", null);
    response = getContainersTarget().request().get();
    assertThat(response.getStatus(), equalTo(OK.getStatusCode()));
    assertThat(response.readEntity(new GenericType<List<Container>>() {
    }), hasSize(1));
}
Also used : Response(javax.ws.rs.core.Response) GenericType(javax.ws.rs.core.GenericType) Container(org.batfish.common.Container) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Aggregations

GenericType (javax.ws.rs.core.GenericType)126 List (java.util.List)64 WebTarget (javax.ws.rs.client.WebTarget)64 Response (javax.ws.rs.core.Response)60 Test (org.junit.Test)51 Client (javax.ws.rs.client.Client)24 ArrayList (java.util.ArrayList)17 Message (com.remswork.project.alice.model.support.Message)16 WebClient (org.apache.cxf.jaxrs.client.WebClient)16 GenericEntity (javax.ws.rs.core.GenericEntity)15 MediaType (javax.ws.rs.core.MediaType)15 Collection (java.util.Collection)14 Set (java.util.Set)14 LinkedList (java.util.LinkedList)12 HashSet (java.util.HashSet)11 Test (org.junit.jupiter.api.Test)11 JerseyTest (org.glassfish.jersey.test.JerseyTest)10 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)9 Collections (java.util.Collections)9 Entity (javax.ws.rs.client.Entity)9