com.adobe.granite.ui.components.ds.DataSource Java Examples

The following examples show how to use com.adobe.granite.ui.components.ds.DataSource. 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: CatalogIdentifierDatasourceTest.java    From commerce-cif-connector with Apache License 2.0 7 votes vote down vote up
@Test
public void testDoGetWithData() throws ServletException, IOException {

    List<String> testResourceProperties = Arrays.asList("key1:value1", "key1:value2", "key2:value3", "key2:value4");

    classUnderTest.doGet(mockRequest, mockResponse);
    DataSource result = (DataSource) mockRequest.getAttribute(DataSource.class.getName());
    List<Resource> elementsInDatsource = new ArrayList<>();

    result.iterator().forEachRemaining(resourceToCheck -> {
        elementsInDatsource.add(resourceToCheck);
    });

    Assert.assertEquals("The datasource contains the correct number of elements", testResourceProperties.size(), elementsInDatsource
        .size());
}
 
Example #2
Source File: RelationTypesDataSourceServlet.java    From aem-core-cif-components with Apache License 2.0 7 votes vote down vote up
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {

    ResourceBundle resourceBundle = request.getResourceBundle(null);
    List<Resource> values = new ArrayList<>();

    for (RelationType relationType : RelationType.values()) {
        ValueMap vm = new ValueMapDecorator(new HashMap<String, Object>());
        vm.put("value", relationType);
        vm.put("text", toText(resourceBundle, relationType.getText()));
        values.add(new ValueMapResource(request.getResourceResolver(), new ResourceMetadata(), JcrConstants.NT_UNSTRUCTURED, vm));
    }

    DataSource ds = new SimpleDataSource(values.iterator());
    request.setAttribute(DataSource.class.getName(), ds);
}
 
Example #3
Source File: SearchDataSourceServletTest.java    From commerce-cif-connector with Apache License 2.0 6 votes vote down vote up
@Test
public void testSomeResult() throws IOException {
    dataSourceProperties.put("path", TEST_PATH);
    dataSourceProperties.put("itemResourceType", "itemRT");
    requestParameterMap.put("fulltext", new String[] { "something" });
    results.add(mock(Resource.class));
    when(request.getParameter("root")).thenReturn("rootPath");

    servlet.doGet(request, response);

    DataSource dataSource = (DataSource) request.getAttribute(DataSource.class.getName());
    assertNotNull(dataSource);
    final Iterator<Resource> iterator = dataSource.iterator();
    assertTrue(iterator.hasNext());
    assertEquals("itemRT", iterator.next().getResourceType());
    assertFalse(iterator.hasNext());
    assertFalse(queryStringSample.contains("\"" + CATEGORY_PATH_PARAMETER + "\""));
    assertFalse(queryStringSample.contains("\"" + CATEGORY_ID_PARAMETER + "\""));
}
 
Example #4
Source File: ChildrenDataSourceServletTest.java    From commerce-cif-connector with Apache License 2.0 6 votes vote down vote up
@Test
public void testQuerySimple() {
    dataSourceProperties.put("query", "variant");
    dataSourceProperties.put("rootPath", TEST_PATH);
    dataSourceProperties.put("path", TEST_PATH);
    dataSourceProperties.put("filter", Filter.folderOrProductOrVariant);
    children.add(mockVariantResource());
    children.add(mockFolderResource());

    servlet.doGet(request, response);

    DataSource dataSource = (DataSource) request.getAttribute(DataSource.class.getName());
    assertNotNull(dataSource);
    assertTrue(dataSource.iterator().hasNext());
    assertTrue(dataSource.iterator().next().getName().startsWith("variant"));
    assertEquals(1, IteratorUtils.toList(dataSource.iterator()).size());
}
 
Example #5
Source File: ChildrenDataSourceServletTest.java    From commerce-cif-connector with Apache License 2.0 6 votes vote down vote up
@Test
public void testQueryWithPath() {
    dataSourceProperties.put("query", TEST_PATH + "variant");
    dataSourceProperties.put("rootPath", TEST_PATH);
    dataSourceProperties.put("path", TEST_PATH);
    dataSourceProperties.put("filter", Filter.folderOrProductOrVariant);
    children.add(mockVariantResource());
    children.add(mockFolderResource());

    servlet.doGet(request, response);

    DataSource dataSource = (DataSource) request.getAttribute(DataSource.class.getName());
    assertNotNull(dataSource);
    assertTrue(dataSource.iterator().hasNext());
    assertTrue(dataSource.iterator().next().getName().startsWith("variant"));
    assertEquals(1, IteratorUtils.toList(dataSource.iterator()).size());
}
 
Example #6
Source File: ChildrenDataSourceServletTest.java    From commerce-cif-connector with Apache License 2.0 6 votes vote down vote up
@Test
public void testItemResourceType() {
    dataSourceProperties.put("path", TEST_PATH);
    dataSourceProperties.put("filter", Filter.folderOrProductOrVariant);
    dataSourceProperties.put("itemResourceType", "testRT");
    children.add(mockVariantResource());
    children.add(mockFolderResource());
    children.add(mockVariantResource());

    servlet.doGet(request, response);

    DataSource dataSource = (DataSource) request.getAttribute(DataSource.class.getName());
    assertNotNull(dataSource);
    assertTrue(dataSource.iterator().hasNext());
    for (Iterator it = dataSource.iterator(); it.hasNext();) {
        Resource resource = (Resource) it.next();
        assertTrue("testRT".equals(resource.getResourceType()));
    }
}
 
Example #7
Source File: ChildrenDataSourceServletTest.java    From commerce-cif-connector with Apache License 2.0 6 votes vote down vote up
@Test
public void testItemResourceTypeNoResourceType() {
    dataSourceProperties.put("path", TEST_PATH);
    dataSourceProperties.put("filter", Filter.folderOrProductOrVariant);
    children.add(mockVariantResource());

    servlet.doGet(request, response);

    DataSource dataSource = (DataSource) request.getAttribute(DataSource.class.getName());
    assertNotNull(dataSource);
    assertTrue(dataSource.iterator().hasNext());
    for (Iterator it = dataSource.iterator(); it.hasNext();) {
        Resource resource = (Resource) it.next();
        assertEquals("commerce/components/product", resource.getResourceType());
    }
}
 
Example #8
Source File: ChildrenDataSourceServletTest.java    From commerce-cif-connector with Apache License 2.0 6 votes vote down vote up
@Test
public void testFolderOrProductOrVariantFilter() {
    dataSourceProperties.put("path", TEST_PATH);
    dataSourceProperties.put("filter", Filter.folderOrProductOrVariant);
    children.add(mockSomeResource());
    children.add(mockVariantResource());
    children.add(mockFolderResource());
    children.add(mockSomeResource());

    servlet.doGet(request, response);

    DataSource dataSource = (DataSource) request.getAttribute(DataSource.class.getName());
    assertNotNull(dataSource);
    assertTrue(dataSource.iterator().hasNext());
    assertEquals(2, IteratorUtils.toList(dataSource.iterator()).size());
}
 
Example #9
Source File: ChildrenDataSourceServletTest.java    From commerce-cif-connector with Apache License 2.0 6 votes vote down vote up
@Test
public void testFolderOrProductFilter() {
    dataSourceProperties.put("path", TEST_PATH);
    dataSourceProperties.put("filter", Filter.folderOrProduct);
    children.add(mockSomeResource());
    children.add(mockProductResource());
    children.add(mockSomeResource());
    children.add(mockFolderResource());
    children.add(mockSomeResource());

    servlet.doGet(request, response);

    DataSource dataSource = (DataSource) request.getAttribute(DataSource.class.getName());
    assertNotNull(dataSource);
    assertTrue(dataSource.iterator().hasNext());
    assertEquals(2, IteratorUtils.toList(dataSource.iterator()).size());
}
 
Example #10
Source File: GraphqlClientDataSourceServletTest.java    From aem-core-cif-components with Apache License 2.0 6 votes vote down vote up
@Test
public void testDataSource() {
    // Stub out getGraphqlClients call
    List<Resource> resources = new ArrayList<Resource>();
    resources.add(new GraphqlClientDataSourceServlet.GraphqlClientResource("my-name", "my-value", null));
    GraphqlClientDataSourceServlet spyServlet = spy(servlet);
    Mockito.doReturn(resources).when(spyServlet).getGraphqlClients(any());

    // Test doGet
    spyServlet.doGet(context.request(), context.response());

    // Verify data source
    DataSource dataSource = (DataSource) context.request().getAttribute(DataSource.class.getName());
    Assert.assertNotNull(dataSource);

    AtomicInteger size = new AtomicInteger(0);
    dataSource.iterator().forEachRemaining(resource -> {
        GraphqlClientDataSourceServlet.GraphqlClientResource client = (GraphqlClientDataSourceServlet.GraphqlClientResource) resource;
        Assert.assertEquals("my-name", client.getText());
        Assert.assertEquals("my-value", client.getValue());
        Assert.assertFalse(client.getSelected());
        size.incrementAndGet();
    });
    Assert.assertEquals(1, size.get());
}
 
Example #11
Source File: RelationTypesDataSourceServletTest.java    From aem-core-cif-components with Apache License 2.0 6 votes vote down vote up
@Test
public void testDataSource() throws ServletException, IOException {
    servlet.doGet(context.request(), context.response());

    // Verify data source
    DataSource dataSource = (DataSource) context.request().getAttribute(DataSource.class.getName());
    Assert.assertNotNull(dataSource);

    AtomicInteger size = new AtomicInteger(0);
    dataSource.iterator().forEachRemaining(resource -> {
        ValueMapResource vmr = (ValueMapResource) resource;
        String value = vmr.getValueMap().get("value", String.class);
        String text = vmr.getValueMap().get("text", String.class);

        // Check that the value/text is a valid RelationType
        RelationType relationType = RelationType.valueOf(value);
        Assert.assertNotNull(relationType);
        Assert.assertEquals(relationType.toString(), value);
        Assert.assertEquals(relationType.getText(), text);

        size.incrementAndGet();
    });
    Assert.assertEquals(RelationType.values().length, size.get()); // Check that all RelationType enums have been collected
}
 
Example #12
Source File: CatalogIdentifierDatasource.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
private DataSource buildValuesList(ResourceResolver resourceResolver) {
    Map<String, Collection<String>> identifiersMap = catalogIdentifierService.getCatalogIdentifiersForAllCommerceProviders();

    List<String> providerIdentifiers = flattenMapToList(identifiersMap);
    List<Resource> syntheticResources = generateResources(resourceResolver, providerIdentifiers);

    return toDataSource(syntheticResources);
}
 
Example #13
Source File: SearchDataSourceServletTest.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoResult() throws IOException {
    servlet.doGet(request, response);

    DataSource dataSource = (DataSource) request.getAttribute(DataSource.class.getName());
    assertNotNull(dataSource);
    assertFalse(dataSource.iterator().hasNext());
}
 
Example #14
Source File: ChildrenDataSourceServletTest.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
@Test
public void testQueryWithMissingTerm() {
    dataSourceProperties.put("query", "/other/path/");
    dataSourceProperties.put("rootPath", "/other/path/");
    dataSourceProperties.put("path", TEST_PATH);
    dataSourceProperties.put("filter", Filter.folderOrProductOrVariant);
    children.add(mockVariantResource());
    children.add(mockFolderResource());

    servlet.doGet(request, response);

    DataSource dataSource = (DataSource) request.getAttribute(DataSource.class.getName());
    assertNotNull(dataSource);
    assertFalse(dataSource.iterator().hasNext());
}
 
Example #15
Source File: ChildrenDataSourceServletTest.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
@Test
public void testQueryWithDifferentPath() {
    dataSourceProperties.put("query", TEST_PATH + "variant");
    dataSourceProperties.put("rootPath", "/other/path/");
    dataSourceProperties.put("path", TEST_PATH);
    dataSourceProperties.put("filter", Filter.folderOrProductOrVariant);
    children.add(mockVariantResource());
    children.add(mockFolderResource());

    servlet.doGet(request, response);

    DataSource dataSource = (DataSource) request.getAttribute(DataSource.class.getName());
    assertNotNull(dataSource);
    assertFalse(dataSource.iterator().hasNext());
}
 
Example #16
Source File: ScriptsDatasourceServlet.java    From APM with Apache License 2.0 5 votes vote down vote up
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
  String path = request.getRequestPathInfo().getSuffix();
  List<Resource> scripts = new ArrayList<>();
  Resource resource = request.getResourceResolver().getResource(path);
  for (Resource child : resource.getChildren()) {
    if (ScriptsRowModel.isFolder(child) || ScriptModel.isScript(child)) {
      scripts.add(new ResourceTypeWrapper(child));
    }
  }
  DataSource dataSource = new SimpleDataSource(scripts.iterator());
  request.setAttribute(DataSource.class.getName(), dataSource);
}
 
Example #17
Source File: HistoryDatasourceServlet.java    From APM with Apache License 2.0 5 votes vote down vote up
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
  Pagination pagination = createPagination(request);
  List<Resource> allHistoryResources = pagination.getPage(history.findAllResources(request.getResourceResolver()))
      .stream()
      .map(ResourceTypeWrapper::new)
      .collect(Collectors.toList());
  DataSource dataSource = new SimpleDataSource(allHistoryResources.iterator());
  request.setAttribute(DataSource.class.getName(), dataSource);
}
 
Example #18
Source File: ChildrenDatasourceServlet.java    From APM with Apache License 2.0 5 votes vote down vote up
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
  Iterator<Resource> iterator = Optional.ofNullable(request.getResource())
      .flatMap(parent -> Optional.ofNullable(parent.getChild("items")))
      .map(items -> items.listChildren())
      .orElse(Collections.emptyIterator());
  DataSource dataSource = new SimpleDataSource(iterator);
  request.setAttribute(DataSource.class.getName(), dataSource);
}
 
Example #19
Source File: AbstractDatasourceServlet.java    From APM with Apache License 2.0 5 votes vote down vote up
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
  SimpleDataSourceBuilder builder = new SimpleDataSourceBuilder(request.getResourceResolver());
  if (emptyOption != null) {
    builder.addOption(emptyOption);
  }
  options.forEach(item -> builder.addOption(getName(item.toString()), item.toString()));
  request.setAttribute(DataSource.class.getName(), builder.build());
}
 
Example #20
Source File: ChildrenDataSourceServletTest.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
@Test
public void testQuerySimpleNotFound() {
    dataSourceProperties.put("query", "product");
    dataSourceProperties.put("rootPath", TEST_PATH);
    dataSourceProperties.put("path", TEST_PATH);
    dataSourceProperties.put("filter", Filter.folderOrProductOrVariant);
    children.add(mockVariantResource());
    children.add(mockFolderResource());

    servlet.doGet(request, response);

    DataSource dataSource = (DataSource) request.getAttribute(DataSource.class.getName());
    assertNotNull(dataSource);
    assertFalse(dataSource.iterator().hasNext());
}
 
Example #21
Source File: ChildrenDataSourceServletTest.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
@Test
public void testProductFilterNoResult() {
    dataSourceProperties.put("path", TEST_PATH);
    dataSourceProperties.put("filter", Filter.product);
    children.add(mockSomeResource());
    children.add(mockFolderResource());

    servlet.doGet(request, response);

    DataSource dataSource = (DataSource) request.getAttribute(DataSource.class.getName());
    assertNotNull(dataSource);
    assertFalse(dataSource.iterator().hasNext());
}
 
Example #22
Source File: ChildrenDataSourceServletTest.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
@Test
public void testProductFilter() {
    dataSourceProperties.put("path", TEST_PATH);
    dataSourceProperties.put("filter", Filter.product);
    children.add(mockSomeResource());
    children.add(mockProductResource());
    children.add(mockFolderResource());

    servlet.doGet(request, response);

    DataSource dataSource = (DataSource) request.getAttribute(DataSource.class.getName());
    assertNotNull(dataSource);
    assertTrue(dataSource.iterator().hasNext());
    assertEquals(1, IteratorUtils.toList(dataSource.iterator()).size());
}
 
Example #23
Source File: ChildrenDataSourceServletTest.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
@Test
public void testWrongFilter() {
    dataSourceProperties.put("path", TEST_PATH);
    dataSourceProperties.put("filter", "any");
    children.add(mockProductResource());

    servlet.doGet(request, response);

    DataSource dataSource = (DataSource) request.getAttribute(DataSource.class.getName());
    assertNotNull(dataSource);
    assertTrue(dataSource.iterator().hasNext());
}
 
Example #24
Source File: ChildrenDataSourceServletTest.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoFilter() {
    dataSourceProperties.put("path", TEST_PATH);
    children.add(mockProductResource());

    servlet.doGet(request, response);

    DataSource dataSource = (DataSource) request.getAttribute(DataSource.class.getName());
    assertNotNull(dataSource);
    assertTrue(dataSource.iterator().hasNext());
}
 
Example #25
Source File: ChildrenDataSourceServletTest.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoDataDataSource() {
    dataSourceProperties.put("path", "/test/path");

    servlet.doGet(request, response);

    DataSource dataSource = (DataSource) request.getAttribute(DataSource.class.getName());
    assertNotNull(dataSource);
    assertFalse(dataSource.iterator().hasNext());
}
 
Example #26
Source File: ChildrenDataSourceServletTest.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
@Test
public void testEmptyDataSource() {
    servlet.doGet(request, response);

    DataSource dataSource = (DataSource) request.getAttribute(DataSource.class.getName());
    assertNotNull(dataSource);
    assertEquals(EmptyDataSource.instance(), dataSource);
}
 
Example #27
Source File: SearchDataSourceServlet.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
    final String PARAMETER_OFFSET = "_commerce_offset";
    final String PARAMETER_LIMIT = "_commerce_limit";
    final String PARAMETER_COMMERCE_TYPE = "_commerce_commerce_type";
    final Config cfg = new Config(request.getResource().getChild(Config.DATASOURCE));
    final SlingScriptHelper sling = ((SlingBindings) request.getAttribute(SlingBindings.class.getName())).getSling();
    final ExpressionHelper ex = new ExpressionHelper(sling.getService(ExpressionResolver.class), request);
    final String itemRT = cfg.get("itemResourceType", String.class);
    final long offset = ex.get(cfg.get("offset", "0"), long.class);
    final long limit = ex.get(cfg.get("limit", "20"), long.class);
    final String commerceType = ex.get(cfg.get("commerceType", "product"), String.class);

    Map<String, Object> queryParameters = new HashMap<>(request.getParameterMap());
    queryParameters.put(PARAMETER_OFFSET, String.valueOf(offset));
    queryParameters.put(PARAMETER_LIMIT, String.valueOf(limit));
    queryParameters.put(PARAMETER_COMMERCE_TYPE, commerceType);

    final String rootPath = request.getParameter("root");

    String rootCategoryId = new CatalogSearchSupport(request.getResourceResolver()).findCategoryId(rootPath);
    if (rootCategoryId != null) {
        queryParameters.put(CATEGORY_ID_PARAMETER, rootCategoryId);
        queryParameters.put(CATEGORY_PATH_PARAMETER, rootPath);
    }
    String queryString = new ObjectMapper().writeValueAsString(queryParameters);
    try {
        Iterator<Resource> virtualResults = request.getResourceResolver().findResources(queryString, VIRTUAL_PRODUCT_QUERY_LANGUAGE);

        final DataSource ds = new SimpleDataSource(new TransformIterator<>(virtualResults, r -> new ResourceWrapper(r) {
            public String getResourceType() {
                return itemRT;
            }
        }));

        request.setAttribute(DataSource.class.getName(), ds);
    } catch (Exception x) {
        response.sendError(500, x.getMessage());
        LOGGER.error("Error finding resources", x);
    }
}
 
Example #28
Source File: CatalogIdentifierDatasourceTest.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
@Test
public void testDoGetWithNoAvailableService() throws ServletException, IOException {

    CatalogIdentifierDatasource datasourceToTest = new CatalogIdentifierDatasource();
    datasourceToTest.doGet(mockRequest, mockResponse);
    DataSource result = (DataSource) mockRequest.getAttribute(DataSource.class.getName());
    List<Resource> elementsInDatsource = new ArrayList<>();

    result.iterator().forEachRemaining(resourceToCheck -> {
        elementsInDatsource.add(resourceToCheck);
    });
    Assert.assertEquals("The datasource contains no elements when the CatalogIdentifierService reference is not satisfied", 0,
        elementsInDatsource.size());
}
 
Example #29
Source File: CatalogIdentifierDatasourceTest.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
@Test
public void testDoGetWithNoProviders() throws ServletException, IOException {
    Mockito.when(mockCatalogIdentifierService.getCatalogIdentifiersForAllCommerceProviders()).thenReturn(new HashMap<>());

    classUnderTest.doGet(mockRequest, mockResponse);
    DataSource result = (DataSource) mockRequest.getAttribute(DataSource.class.getName());
    List<Resource> elementsInDatsource = new ArrayList<>();

    result.iterator().forEachRemaining(resourceToCheck -> {
        elementsInDatsource.add(resourceToCheck);
    });
    Assert.assertEquals("The datasource contains no elements", 0, elementsInDatsource.size());
}
 
Example #30
Source File: CatalogIdentifierDatasource.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
@Override
protected void doGet(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHttpServletResponse response) throws ServletException,
    IOException {
    DataSource ds;

    if (catalogIdentifierService == null) {
        ds = EmptyDataSource.instance();
    } else {
        ds = buildValuesList(request.getResourceResolver());
    }

    request.setAttribute(DataSource.class.getName(), ds);
}