Search in sources :

Example 96 with UriInfo

use of javax.ws.rs.core.UriInfo in project ddf by codice.

the class MetricsEndpointTest method testGetMetricsGraphWithIOException.

// NOTE: "expected" annotation does not work when test case extends XMLTestCase,
// hence the usage of the try/catch/fail approach for the expected exception
@Test
public // (expected = MetricsEndpointException.class)
void testGetMetricsGraphWithIOException() throws Exception {
    UriInfo uriInfo = createUriInfo();
    RrdMetricsRetriever metricsRetriever = mock(RrdMetricsRetriever.class);
    when(metricsRetriever.createGraph(anyString(), anyString(), anyLong(), anyLong(), anyString(), anyString())).thenThrow(IOException.class);
    MetricsEndpoint endpoint = getEndpoint();
    endpoint.setMetricsDir(TEST_DIR);
    endpoint.setMetricsRetriever(metricsRetriever);
    try {
        endpoint.getMetricsData("uptime", "png", "2013-03-25T06:00:00-07:00", "2013-03-25T07:10:00-07:00", null, "my label", "my title", uriInfo);
        fail();
    } catch (MetricsEndpointException e) {
    }
}
Also used : RrdMetricsRetriever(ddf.metrics.reporting.internal.rrd4j.RrdMetricsRetriever) MetricsEndpointException(ddf.metrics.reporting.internal.MetricsEndpointException) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Example 97 with UriInfo

use of javax.ws.rs.core.UriInfo in project ddf by codice.

the class MetricsEndpointTest method testGetMetricsDataAsJson.

@Test
public void testGetMetricsDataAsJson() throws Exception {
    // Create RRD file that Metrics Endpoint will detect
    // 15 minutes in seconds
    int dateOffset = 900;
    createRrdFile(dateOffset);
    UriInfo uriInfo = createUriInfo();
    // Get the metrics data from the endpoint
    MetricsEndpoint endpoint = getEndpoint();
    endpoint.setMetricsDir(TEST_DIR);
    Response response = endpoint.getMetricsData("uptime", "json", null, null, Integer.toString(dateOffset), "my label", "my title", uriInfo);
    cleanupRrd();
    String json = (String) response.getEntity();
    LOGGER.debug("json = {}", json);
    JSONParser parser = new JSONParser();
    JSONObject jsonObj = (JSONObject) parser.parse(json);
    // Verify the title, totalCount, and data (i.e., samples) are present
    assertThat(jsonObj.size(), equalTo(3));
    assertThat(jsonObj.get("title"), not(nullValue()));
    assertThat(jsonObj.get("totalCount"), not(nullValue()));
    // Verify 2 samples were retrieved from the RRD file and put in the JSON fetch results
    JSONArray samples = (JSONArray) jsonObj.get("data");
    assertThat(samples.size(), equalTo(2));
    // Verify each retrieved sample has a timestamp and value
    for (int i = 0; i < samples.size(); i++) {
        JSONObject sample = (JSONObject) samples.get(i);
        LOGGER.debug("timestamp = {},   value = {}", (String) sample.get("timestamp"), sample.get("value"));
        assertThat(sample.get("timestamp"), not(nullValue()));
        assertThat(sample.get("value"), not(nullValue()));
    }
}
Also used : Response(javax.ws.rs.core.Response) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) Matchers.anyString(org.mockito.Matchers.anyString) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Example 98 with UriInfo

use of javax.ws.rs.core.UriInfo in project ddf by codice.

the class OpenSearchEndpointTest method testProcessQueryForProperHandlingOfSiteNameLOCAL.

/**
     * Test method for
     * {@link org.codice.ddf.endpoints.OpenSearchEndpoint#processQuery(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, javax.ws.rs.core.UriInfo, java.lang.String, java.lang.String)}
     * .
     * <p>
     * This test will verify that the string "local" in the sources passed to
     * OpenSearchEndpoint.processQuery is replaced with the local site name (in this case the mocked
     * name "TestSiteName"). The QueryRequest object is checked when the framework.query is called
     * to retrieve the OpenSearchQuery, which contains the Set of sites. An assertion within the
     * Answer object for the call framework.query checks that the sites Set is contains the
     * TEST_SITE_NAME.
     *
     * @throws URISyntaxException
     * @throws FederationException
     * @throws SourceUnavailableException
     * @throws UnsupportedQueryException
     * @throws UnsupportedEncodingException
     * @throws CatalogTransformerException
     */
@SuppressWarnings("unchecked")
@Test
public void testProcessQueryForProperHandlingOfSiteNameLOCAL() throws URISyntaxException, UnsupportedQueryException, SourceUnavailableException, FederationException, UnsupportedEncodingException, CatalogTransformerException {
    // ***Test setup***
    final String testSiteName = "TestSiteName";
    CatalogFramework mockFramework = mock(CatalogFramework.class);
    FilterBuilder mockFilterBuilder = mock(FilterBuilder.class);
    AttributeBuilder mockAB = mock(AttributeBuilder.class);
    ExpressionBuilder mockEB = mock(ExpressionBuilder.class);
    ContextualExpressionBuilder mockCEB = mock(ContextualExpressionBuilder.class);
    Filter mockFilter = mock(Filter.class);
    when(mockFilterBuilder.attribute(anyString())).thenReturn(mockAB);
    when(mockAB.is()).thenReturn(mockEB);
    when(mockEB.like()).thenReturn(mockCEB);
    when(mockCEB.text(anyString())).thenReturn(mockFilter);
    String searchTerms = "searchForThis";
    // "local" MUST be included
    String sources = "test, local";
    String count = "200";
    // dummy UriInfo, not really used functionally
    UriInfo mockUriInfo = mock(UriInfo.class);
    URI uri = new URI("test");
    when(mockUriInfo.getRequestUri()).thenReturn(uri);
    MultivaluedMap<String, String> mockMVMap = mock(MultivaluedMap.class);
    when(mockMVMap.get("subscription")).thenReturn(null);
    when(mockMVMap.get("interval")).thenReturn(null);
    when(mockUriInfo.getQueryParameters()).thenReturn(mockMVMap);
    @SuppressWarnings("unused") BinaryContent mockByteContent = mock(BinaryContent.class);
    // Check on the sites passed in to framework.query
    when(mockFramework.query(any(QueryRequest.class))).thenAnswer(new Answer<Object>() {

        public Object answer(InvocationOnMock invocation) {
            QueryRequest queryRequest = (QueryRequest) invocation.getArguments()[0];
            // ***Test verification***
            // This assert is the whole point of this unit test
            Assert.assertTrue(((OpenSearchQuery) queryRequest.getQuery()).getSiteIds().contains(testSiteName));
            return new QueryResponseImpl(queryRequest);
        }
    });
    // setup the BinaryContent for the call to Response.ok(...)
    // This is just needed to get the method to complete, the
    BinaryContent mockBinaryContent = mock(BinaryContent.class);
    InputStream is = new ByteArrayInputStream("Test String From InputStream".getBytes("UTF-8"));
    when(mockBinaryContent.getInputStream()).thenReturn(is);
    when(mockBinaryContent.getMimeTypeValue()).thenReturn("text/plain");
    when(mockFramework.transform(any(QueryResponse.class), anyString(), anyMap())).thenReturn(mockBinaryContent);
    OpenSearchEndpoint osEndPoint = new OpenSearchEndpoint(mockFramework, mockFilterBuilder);
    System.setProperty(SystemInfo.SITE_NAME, testSiteName);
    // ***Test Execution***
    osEndPoint.processQuery(searchTerms, null, sources, null, null, count, null, null, null, null, null, null, null, null, null, null, null, null, mockUriInfo, null, null, null);
}
Also used : AttributeBuilder(ddf.catalog.filter.AttributeBuilder) QueryRequest(ddf.catalog.operation.QueryRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Matchers.anyString(org.mockito.Matchers.anyString) ContextualExpressionBuilder(ddf.catalog.filter.ContextualExpressionBuilder) ContextualExpressionBuilder(ddf.catalog.filter.ContextualExpressionBuilder) ExpressionBuilder(ddf.catalog.filter.ExpressionBuilder) BinaryContent(ddf.catalog.data.BinaryContent) URI(java.net.URI) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) Filter(org.opengis.filter.Filter) ByteArrayInputStream(java.io.ByteArrayInputStream) FilterBuilder(ddf.catalog.filter.FilterBuilder) InvocationOnMock(org.mockito.invocation.InvocationOnMock) QueryResponse(ddf.catalog.operation.QueryResponse) CatalogFramework(ddf.catalog.CatalogFramework) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Example 99 with UriInfo

use of javax.ws.rs.core.UriInfo in project ddf by codice.

the class MetricsEndpointTest method testGetMetricsDataAsCsv.

@Test
public void testGetMetricsDataAsCsv() throws Exception {
    // Create RRD file that Metrics Endpoint will detect
    // 15 minutes in seconds
    int dateOffset = 900;
    createRrdFile(dateOffset);
    UriInfo uriInfo = createUriInfo();
    // Get the metrics data from the endpoint
    MetricsEndpoint endpoint = getEndpoint();
    endpoint.setMetricsDir(TEST_DIR);
    Response response = endpoint.getMetricsData("uptime", "csv", null, null, Integer.toString(dateOffset), "my label", "my title", uriInfo);
    cleanupRrd();
    String csv = (String) response.getEntity();
    LOGGER.debug("csv = {}", csv);
    // Break up CSV data into its individual lines
    // Each line should have 2 parts (cells)
    // The first line should be the column headers
    String[] csvLines = csv.split("\n");
    // Verify that number of lines should be the column headers + (number of samples - 1)
    // Since 3 samples were taken, but only 2 are added to the RRD file due to averaging,
    // expect 2 lines of data and the one line of column headers.
    assertThat(csvLines.length, equalTo(3));
    // column headers
    assertThat(csvLines[0], equalTo("Timestamp,Value"));
    String[] cells = csvLines[1].split(",");
    // each line of data has the 2 values
    assertThat(cells.length, equalTo(2));
}
Also used : Response(javax.ws.rs.core.Response) Matchers.anyString(org.mockito.Matchers.anyString) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Example 100 with UriInfo

use of javax.ws.rs.core.UriInfo in project ddf by codice.

the class MetricsEndpointTest method testGetMetricsDataAsPpt.

@Test
public void testGetMetricsDataAsPpt() throws Exception {
    // Create RRD file that Metrics Endpoint will detect
    // 15 minutes in seconds
    int dateOffset = 900;
    createRrdFile(dateOffset);
    UriInfo uriInfo = createUriInfo();
    // Get the metrics data from the endpoint
    MetricsEndpoint endpoint = getEndpoint();
    endpoint.setMetricsDir(TEST_DIR);
    Response response = endpoint.getMetricsData("uptime", "ppt", null, null, Integer.toString(dateOffset), "my label", "my title", uriInfo);
    cleanupRrd();
    InputStream is = (InputStream) response.getEntity();
    assertThat(is, not(nullValue()));
    SlideShow ppt = new SlideShow(is);
    Slide[] slides = ppt.getSlides();
    assertThat(slides.length, equalTo(1));
}
Also used : Response(javax.ws.rs.core.Response) Slide(org.apache.poi.hslf.model.Slide) InputStream(java.io.InputStream) SlideShow(org.apache.poi.hslf.usermodel.SlideShow) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Aggregations

UriInfo (javax.ws.rs.core.UriInfo)105 Response (javax.ws.rs.core.Response)50 Test (org.junit.Test)49 URI (java.net.URI)32 Test (org.testng.annotations.Test)21 Map (java.util.Map)15 MediaType (javax.ws.rs.core.MediaType)15 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 GET (javax.ws.rs.GET)12 Path (javax.ws.rs.Path)12 Context (javax.ws.rs.core.Context)12 Request (org.apache.atlas.catalog.Request)12 ResourceProvider (org.apache.atlas.catalog.ResourceProvider)12 TaxonomyResourceProvider (org.apache.atlas.catalog.TaxonomyResourceProvider)12 MetadataService (org.apache.atlas.services.MetadataService)12 AtlasTypeDefStore (org.apache.atlas.store.AtlasTypeDefStore)12 PathParam (javax.ws.rs.PathParam)11 ArrayList (java.util.ArrayList)10 Api (io.swagger.annotations.Api)8 ApiOperation (io.swagger.annotations.ApiOperation)8