Search in sources :

Example 11 with SourceDescriptor

use of ddf.catalog.source.SourceDescriptor in project ddf by codice.

the class CatalogFrameworkImplTest method testGetAllSiteNames.

@Test
public void testGetAllSiteNames() {
    String frameworkName = "DDF";
    CatalogProvider provider = new MockMemoryProvider("Provider", "Provider", "v1.0", "DDF", new HashSet<ContentType>(), true, new Date());
    List<FederatedSource> federatedSources = createDefaultFederatedSourceList(true);
    // Expected Set of Names
    Set<String> expectedNameSet = new HashSet<String>();
    for (FederatedSource curSite : federatedSources) {
        expectedNameSet.add(curSite.getId());
    }
    // Mock register the provider in the container
    // Mock the source poller
    SourcePoller mockPoller = mock(SourcePoller.class);
    when(mockPoller.getCachedSource(isA(Source.class))).thenReturn(null);
    FrameworkProperties frameworkProperties = new FrameworkProperties();
    frameworkProperties.setSourcePoller(mockPoller);
    Map<String, FederatedSource> sources = new HashMap<>();
    for (FederatedSource federatedSource : federatedSources) {
        sources.put(federatedSource.getId(), federatedSource);
    }
    frameworkProperties.setFederatedSources(sources);
    frameworkProperties.setCatalogProviders(Collections.singletonList(provider));
    CatalogFrameworkImpl framework = createFramework(frameworkProperties);
    framework.setId(frameworkName);
    // Returned Set of Names
    // Returned Sites
    SourceInfoRequest request = new SourceInfoRequestEnterprise(true);
    SourceInfoResponse response = null;
    try {
        response = framework.getSourceInfo(request);
    } catch (SourceUnavailableException e) {
        LOGGER.debug("SourceUnavilable", e);
        fail();
    }
    assert (response != null);
    Set<SourceDescriptor> sourceDescriptors = response.getSourceInfo();
    // should contain ONLY the original federated sites
    assertEquals(expectedNameSet.size(), sourceDescriptors.size());
    Set<String> returnedSourceIds = new HashSet<String>();
    for (SourceDescriptor sd : sourceDescriptors) {
        returnedSourceIds.add(sd.getSourceId());
    }
    for (String id : returnedSourceIds) {
        LOGGER.debug("returned sourceId: {}", id);
    }
    assertTrue(expectedNameSet.equals(returnedSourceIds));
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) SourceDescriptor(ddf.catalog.source.SourceDescriptor) ContentType(ddf.catalog.data.ContentType) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) SourceInfoRequestEnterprise(ddf.catalog.operation.impl.SourceInfoRequestEnterprise) Date(java.util.Date) Source(ddf.catalog.source.Source) ByteSource(com.google.common.io.ByteSource) CachedSource(ddf.catalog.util.impl.CachedSource) FederatedSource(ddf.catalog.source.FederatedSource) SourcePoller(ddf.catalog.util.impl.SourcePoller) FederatedSource(ddf.catalog.source.FederatedSource) CatalogProvider(ddf.catalog.source.CatalogProvider) SourceInfoRequest(ddf.catalog.operation.SourceInfoRequest) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 12 with SourceDescriptor

use of ddf.catalog.source.SourceDescriptor in project ddf by codice.

the class CatalogFrameworkImplTest method testGetFederatedSources.

@Test
public void testGetFederatedSources() {
    SourceInfoRequest request = new SourceInfoRequestEnterprise(true);
    SourceInfoResponse response = null;
    try {
        response = framework.getSourceInfo(request);
    } catch (SourceUnavailableException e) {
        fail();
    }
    Set<SourceDescriptor> sourceDescriptors = response.getSourceInfo();
    for (SourceDescriptor descriptor : sourceDescriptors) {
        LOGGER.debug("Descriptor id: {}", descriptor.getSourceId());
    }
    // The "+1" is to account for the CatalogFramework source descriptor.
    // Even if no local catalog provider is configured, the catalog framework's
    // site info is included in the SourceDescriptos list.
    assertEquals(federatedSources.size() + 1, sourceDescriptors.size());
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) SourceDescriptor(ddf.catalog.source.SourceDescriptor) SourceInfoRequest(ddf.catalog.operation.SourceInfoRequest) SourceInfoRequestEnterprise(ddf.catalog.operation.impl.SourceInfoRequestEnterprise) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) Test(org.junit.Test)

Example 13 with SourceDescriptor

use of ddf.catalog.source.SourceDescriptor in project ddf by codice.

the class TestRestEndpoint method testGetDocumentSourcesSuccess.

/**
     * Tests getting source information
     *
     * @throws Exception
     */
@Test
public void testGetDocumentSourcesSuccess() throws Exception {
    final String localSourceId = "local";
    final String fed1SourceId = "fed1";
    final String fed2SourceId = "fed2";
    final String version = "4.0";
    final String jsonMimeTypeString = "application/json";
    Set<ContentType> contentTypes = new HashSet<ContentType>();
    contentTypes.add(new ContentTypeImpl("ct1", "v1"));
    contentTypes.add(new ContentTypeImpl("ct2", "v2"));
    contentTypes.add(new ContentTypeImpl("ct3", null));
    JSONArray contentTypesInJSON = new JSONArray();
    for (ContentType ct : contentTypes) {
        JSONObject ob = new JSONObject();
        ob.put("name", ct.getName());
        ob.put("version", ct.getVersion() != null ? ct.getVersion() : "");
        contentTypesInJSON.add(ob);
    }
    Set<SourceDescriptor> sourceDescriptors = new HashSet<SourceDescriptor>();
    SourceDescriptorImpl localDescriptor = new SourceDescriptorImpl(localSourceId, contentTypes);
    localDescriptor.setVersion(version);
    localDescriptor.setAvailable(true);
    SourceDescriptorImpl fed1Descriptor = new SourceDescriptorImpl(fed1SourceId, contentTypes);
    fed1Descriptor.setVersion(version);
    fed1Descriptor.setAvailable(true);
    SourceDescriptorImpl fed2Descriptor = new SourceDescriptorImpl(fed2SourceId, null);
    fed2Descriptor.setAvailable(true);
    sourceDescriptors.add(localDescriptor);
    sourceDescriptors.add(fed1Descriptor);
    sourceDescriptors.add(fed2Descriptor);
    SourceInfoResponse sourceInfoResponse = new SourceInfoResponseImpl(null, null, sourceDescriptors);
    CatalogFramework framework = mock(CatalogFramework.class);
    when(framework.getSourceInfo(isA(SourceInfoRequestEnterprise.class))).thenReturn(sourceInfoResponse);
    RESTEndpoint restEndpoint = new RESTEndpoint(framework);
    Response response = restEndpoint.getDocument(null, null);
    assertEquals(OK, response.getStatus());
    assertEquals(jsonMimeTypeString, response.getMetadata().get("Content-Type").get(0));
    String responseMessage = IOUtils.toString((ByteArrayInputStream) response.getEntity());
    JSONArray srcList = (JSONArray) new JSONParser().parse(responseMessage);
    assertEquals(3, srcList.size());
    for (Object o : srcList) {
        JSONObject src = (JSONObject) o;
        assertEquals(true, src.get("available"));
        String id = (String) src.get("id");
        if (id.equals(localSourceId)) {
            assertThat((Iterable<Object>) src.get("contentTypes"), hasItems(contentTypesInJSON.toArray()));
            assertEquals(contentTypes.size(), ((JSONArray) src.get("contentTypes")).size());
            assertEquals(version, src.get("version"));
        } else if (id.equals(fed1SourceId)) {
            assertThat((Iterable<Object>) src.get("contentTypes"), hasItems(contentTypesInJSON.toArray()));
            assertEquals(contentTypes.size(), ((JSONArray) src.get("contentTypes")).size());
            assertEquals(version, src.get("version"));
        } else if (id.equals(fed2SourceId)) {
            assertEquals(0, ((JSONArray) src.get("contentTypes")).size());
            assertEquals("", src.get("version"));
        } else {
            fail("Invalid ID returned");
        }
    }
}
Also used : ContentTypeImpl(ddf.catalog.data.impl.ContentTypeImpl) SourceDescriptor(ddf.catalog.source.SourceDescriptor) ContentType(ddf.catalog.data.ContentType) SourceDescriptorImpl(ddf.catalog.source.impl.SourceDescriptorImpl) JSONArray(net.minidev.json.JSONArray) Matchers.anyString(org.mockito.Matchers.anyString) SourceInfoRequestEnterprise(ddf.catalog.operation.impl.SourceInfoRequestEnterprise) QueryResponse(ddf.catalog.operation.QueryResponse) Response(javax.ws.rs.core.Response) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) JSONObject(net.minidev.json.JSONObject) CatalogFramework(ddf.catalog.CatalogFramework) SourceInfoResponseImpl(ddf.catalog.operation.impl.SourceInfoResponseImpl) JSONParser(net.minidev.json.parser.JSONParser) JSONObject(net.minidev.json.JSONObject) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 14 with SourceDescriptor

use of ddf.catalog.source.SourceDescriptor in project ddf by codice.

the class RESTEndpoint method getDocument.

/**
     * REST Get. Retrieves information regarding sources available.
     *
     * @param uriInfo
     * @param httpRequest
     * @return
     */
@GET
@Path(SOURCES_PATH)
public Response getDocument(@Context UriInfo uriInfo, @Context HttpServletRequest httpRequest) {
    BinaryContent content;
    ResponseBuilder responseBuilder;
    String sourcesString = null;
    JSONArray resultsList = new JSONArray();
    SourceInfoResponse sources;
    try {
        SourceInfoRequestEnterprise sourceInfoRequestEnterprise = new SourceInfoRequestEnterprise(true);
        sources = catalogFramework.getSourceInfo(sourceInfoRequestEnterprise);
        for (SourceDescriptor source : sources.getSourceInfo()) {
            JSONObject sourceObj = new JSONObject();
            sourceObj.put("id", source.getSourceId());
            sourceObj.put("version", source.getVersion() != null ? source.getVersion() : "");
            sourceObj.put("available", Boolean.valueOf(source.isAvailable()));
            JSONArray contentTypesObj = new JSONArray();
            if (source.getContentTypes() != null) {
                for (ContentType contentType : source.getContentTypes()) {
                    if (contentType != null && contentType.getName() != null) {
                        JSONObject contentTypeObj = new JSONObject();
                        contentTypeObj.put("name", contentType.getName());
                        contentTypeObj.put("version", contentType.getVersion() != null ? contentType.getVersion() : "");
                        contentTypesObj.add(contentTypeObj);
                    }
                }
            }
            sourceObj.put("contentTypes", contentTypesObj);
            resultsList.add(sourceObj);
        }
    } catch (SourceUnavailableException e) {
        LOGGER.info("Unable to retrieve Sources. {}", e.getMessage());
        LOGGER.debug("Unable to retrieve Sources", e);
    }
    sourcesString = JSONValue.toJSONString(resultsList);
    content = new BinaryContentImpl(new ByteArrayInputStream(sourcesString.getBytes(StandardCharsets.UTF_8)), jsonMimeType);
    responseBuilder = Response.ok(content.getInputStream(), content.getMimeTypeValue());
    // Add the Accept-ranges header to let the client know that we accept ranges in bytes
    responseBuilder.header(HEADER_ACCEPT_RANGES, BYTES);
    return responseBuilder.build();
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) SourceDescriptor(ddf.catalog.source.SourceDescriptor) JSONObject(net.minidev.json.JSONObject) ContentType(ddf.catalog.data.ContentType) ByteArrayInputStream(java.io.ByteArrayInputStream) JSONArray(net.minidev.json.JSONArray) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) BinaryContent(ddf.catalog.data.BinaryContent) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) SourceInfoRequestEnterprise(ddf.catalog.operation.impl.SourceInfoRequestEnterprise) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 15 with SourceDescriptor

use of ddf.catalog.source.SourceDescriptor in project ddf by codice.

the class SearchService method getSourceIds.

private Set<String> getSourceIds(String sources) {
    Set<String> sourceIds;
    if (StringUtils.equalsIgnoreCase(sources, LOCAL_SOURCE)) {
        LOGGER.debug("Received local query");
        sourceIds = new HashSet<String>(Arrays.asList(searchController.getFramework().getId()));
    } else if (!(StringUtils.isEmpty(sources))) {
        LOGGER.debug("Received source names from client: {}", sources);
        sourceIds = new HashSet<String>(Arrays.asList(StringUtils.stripAll(sources.split(","))));
    } else {
        LOGGER.debug("Received enterprise query");
        SourceInfoResponse sourceInfo = null;
        try {
            sourceInfo = searchController.getFramework().getSourceInfo(new SourceInfoRequestEnterprise(true));
        } catch (SourceUnavailableException e) {
            LOGGER.debug("Exception while getting source status. Defaulting to all sources. " + "This could include unavailable sources.", e);
        }
        if (sourceInfo != null) {
            sourceIds = new HashSet<String>();
            for (SourceDescriptor source : sourceInfo.getSourceInfo()) {
                if (source.isAvailable()) {
                    sourceIds.add(source.getSourceId());
                }
            }
        } else {
            sourceIds = searchController.getFramework().getSourceIds();
        }
    }
    return sourceIds;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) SourceDescriptor(ddf.catalog.source.SourceDescriptor) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) SourceInfoRequestEnterprise(ddf.catalog.operation.impl.SourceInfoRequestEnterprise) HashSet(java.util.HashSet)

Aggregations

SourceDescriptor (ddf.catalog.source.SourceDescriptor)18 SourceInfoResponse (ddf.catalog.operation.SourceInfoResponse)13 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)13 SourceInfoRequestEnterprise (ddf.catalog.operation.impl.SourceInfoRequestEnterprise)10 FederatedSource (ddf.catalog.source.FederatedSource)8 Test (org.junit.Test)8 SourceInfoRequest (ddf.catalog.operation.SourceInfoRequest)6 CatalogProvider (ddf.catalog.source.CatalogProvider)6 Source (ddf.catalog.source.Source)6 HashSet (java.util.HashSet)6 SourceInfoResponseImpl (ddf.catalog.operation.impl.SourceInfoResponseImpl)5 SourceDescriptorImpl (ddf.catalog.source.impl.SourceDescriptorImpl)5 Matchers.anyString (org.mockito.Matchers.anyString)5 ContentType (ddf.catalog.data.ContentType)4 SourceInfoRequestLocal (ddf.catalog.operation.impl.SourceInfoRequestLocal)4 HashMap (java.util.HashMap)4 CatalogFramework (ddf.catalog.CatalogFramework)3 SourceDescriptorComparator (ddf.catalog.util.impl.SourceDescriptorComparator)3 SourcePoller (ddf.catalog.util.impl.SourcePoller)3 Set (java.util.Set)3