Java Code Examples for org.apache.sling.api.scripting.SlingBindings#setResource()

The following examples show how to use org.apache.sling.api.scripting.SlingBindings#setResource() . 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: ProductTeaserImplTest.java    From aem-core-cif-components with Apache License 2.0 6 votes vote down vote up
public void setUp(String resourcePath, boolean deepLink) throws Exception {
    Page page = context.currentPage(PAGE);
    context.currentResource(resourcePath);
    teaserResource = Mockito.spy(context.resourceResolver().getResource(resourcePath));

    Query rootQuery = Utils.getQueryFromResource("graphql/magento-graphql-productteaser-result.json");
    product = rootQuery.getProducts().getItems().get(0);

    GraphqlClient graphqlClient = Utils.setupGraphqlClientWithHttpResponseFrom("graphql/magento-graphql-productteaser-result.json");
    Mockito.when(teaserResource.adaptTo(ComponentsConfiguration.class)).thenReturn(MOCK_CONFIGURATION_OBJECT);
    context.registerAdapter(Resource.class, GraphqlClient.class, (Function<Resource, GraphqlClient>) input -> input.getValueMap().get(
        "cq:graphqlClient", String.class) != null ? graphqlClient : null);

    // This sets the page attribute injected in the models with @Inject or @ScriptVariable
    SlingBindings slingBindings = (SlingBindings) context.request().getAttribute(SlingBindings.class.getName());
    slingBindings.setResource(teaserResource);
    slingBindings.put(WCMBindingsConstants.NAME_CURRENT_PAGE, page);
    slingBindings.put(WCMBindingsConstants.NAME_PROPERTIES, teaserResource.getValueMap());

    if (deepLink) {
        // Configure the component to create deep links to specific pages
        context.request().setAttribute(WCMMode.class.getName(), WCMMode.EDIT);
    }

    productTeaser = context.request().adaptTo(ProductTeaserImpl.class);
}
 
Example 2
Source File: ProductTeaserImplTest.java    From aem-core-cif-components with Apache License 2.0 6 votes vote down vote up
@Test
public void verifyProductTeaserNoGraphqlCLient() {
    Page page = context.currentPage(PAGE);
    context.currentResource(PRODUCTTEASER_NOCLIENT);
    Resource teaserResource = Mockito.spy(context.resourceResolver().getResource(PRODUCTTEASER_NOCLIENT));
    Mockito.when(teaserResource.adaptTo(GraphqlClient.class)).thenReturn(null);

    // This sets the page attribute injected in the models with @Inject or @ScriptVariable
    SlingBindings slingBindings = (SlingBindings) context.request().getAttribute(SlingBindings.class.getName());
    slingBindings.setResource(teaserResource);
    slingBindings.put(WCMBindingsConstants.NAME_CURRENT_PAGE, page);
    slingBindings.put(WCMBindingsConstants.NAME_PROPERTIES, teaserResource.getValueMap());

    ProductTeaserImpl productTeaserNoClient = context.request().adaptTo(ProductTeaserImpl.class);

    Assert.assertNull(productTeaserNoClient.getProductRetriever());
    Assert.assertNull(productTeaserNoClient.getUrl());
}
 
Example 3
Source File: ProductTeaserImplTest.java    From aem-core-cif-components with Apache License 2.0 6 votes vote down vote up
@Test
public void testVirtualProduct() throws IOException {
    Page page = context.currentPage(PAGE);
    context.currentResource(PRODUCTTEASER_VIRTUAL);
    Resource teaserResource = Mockito.spy(context.resourceResolver().getResource(PRODUCTTEASER_VIRTUAL));
    Mockito.when(teaserResource.adaptTo(ComponentsConfiguration.class)).thenReturn(MOCK_CONFIGURATION_OBJECT);

    GraphqlClient graphqlClient = Utils.setupGraphqlClientWithHttpResponseFrom("graphql/magento-graphql-virtualproduct-result.json");
    context.registerAdapter(Resource.class, GraphqlClient.class, (Function<Resource, GraphqlClient>) input -> input.getValueMap().get(
        "cq:graphqlClient", String.class) != null ? graphqlClient : null);

    SlingBindings slingBindings = (SlingBindings) context.request().getAttribute(SlingBindings.class.getName());
    slingBindings.setResource(teaserResource);
    slingBindings.put(WCMBindingsConstants.NAME_CURRENT_PAGE, page);
    slingBindings.put(WCMBindingsConstants.NAME_PROPERTIES, teaserResource.getValueMap());

    productTeaser = context.request().adaptTo(ProductTeaserImpl.class);
    Assert.assertTrue(productTeaser.isVirtualProduct());
}
 
Example 4
Source File: CommerceTeaserImplTest.java    From aem-core-cif-components with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() {
    Page page = context.currentPage(PAGE);
    context.currentResource(TEASER);
    commerceTeaserResource = context.resourceResolver().getResource(TEASER);

    // This sets the page attribute injected in the models with @Inject or @ScriptVariable
    SlingBindings slingBindings = (SlingBindings) context.request().getAttribute(SlingBindings.class.getName());
    slingBindings.setResource(commerceTeaserResource);
    slingBindings.put(WCMBindingsConstants.NAME_CURRENT_PAGE, page);

    // Configure the component to create deep links to specific pages
    context.request().setAttribute(WCMMode.class.getName(), WCMMode.EDIT);

    commerceTeaser = context.request().adaptTo(CommerceTeaserImpl.class);
}
 
Example 5
Source File: ProductCarouselImplTest.java    From aem-core-cif-components with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    Page page = context.currentPage(PAGE);
    context.currentResource(PRODUCTCAROUSEL);
    carouselResource = Mockito.spy(context.resourceResolver().getResource(PRODUCTCAROUSEL));

    Query rootQuery = Utils.getQueryFromResource("graphql/magento-graphql-productcarousel-result.json");
    products = rootQuery.getProducts().getItems();

    GraphqlClient graphqlClient = Utils.setupGraphqlClientWithHttpResponseFrom("graphql/magento-graphql-productcarousel-result.json");
    Mockito.when(carouselResource.adaptTo(ComponentsConfiguration.class)).thenReturn(MOCK_CONFIGURATION_OBJECT);
    context.registerAdapter(Resource.class, GraphqlClient.class, (Function<Resource, GraphqlClient>) input -> input.getValueMap().get(
        "cq:graphqlClient") != null ? graphqlClient : null);

    // This sets the page attribute injected in the models with @Inject or @ScriptVariable
    SlingBindings slingBindings = (SlingBindings) context.request().getAttribute(SlingBindings.class.getName());
    slingBindings.setResource(carouselResource);
    slingBindings.put(WCMBindingsConstants.NAME_CURRENT_PAGE, page);

    productSkuArray = (String[]) carouselResource.getValueMap().get("product"); // The HTL script uses an alias here
    slingBindings.put("productSkuList", productSkuArray);

    productCarousel = context.request().adaptTo(ProductCarouselImpl.class);
}
 
Example 6
Source File: GraphqlServletTest.java    From aem-core-cif-components with Apache License 2.0 5 votes vote down vote up
private Resource prepareModel(String resourcePath) throws ServletException {
    Page page = Mockito.spy(context.currentPage(PAGE));
    context.currentPage(page);
    context.currentResource(resourcePath);
    Resource resource = Mockito.spy(context.currentResource());

    GraphqlClient graphqlClient = new MockGraphqlClient();
    Mockito.when(resource.adaptTo(GraphqlClient.class)).thenReturn(graphqlClient);

    Resource pageContent = Mockito.spy(page.getContentResource());
    when(page.getContentResource()).thenReturn(pageContent);
    context.registerAdapter(Resource.class, GraphqlClient.class, (Function<Resource, GraphqlClient>) input -> input.getValueMap().get(
        "cq:graphqlClient", String.class) != null ? graphqlClient : null);

    // This sets the page attribute injected in the models with @Inject or @ScriptVariable
    SlingBindings slingBindings = (SlingBindings) context.request().getAttribute(SlingBindings.class.getName());
    slingBindings.setResource(resource);
    slingBindings.put(WCMBindingsConstants.NAME_CURRENT_PAGE, page);
    slingBindings.put(WCMBindingsConstants.NAME_PROPERTIES, resource.getValueMap());

    XSSAPI xssApi = mock(XSSAPI.class);
    when(xssApi.filterHTML(Mockito.anyString())).then(i -> i.getArgumentAt(0, String.class));
    slingBindings.put("xssApi", xssApi);

    Style style = mock(Style.class);
    when(style.get(Mockito.anyString(), Mockito.isA(Boolean.class))).then(i -> i.getArgumentAt(1, Boolean.class));
    when(style.get(Mockito.anyString(), Mockito.isA(Integer.class))).then(i -> i.getArgumentAt(1, Integer.class));
    slingBindings.put("currentStyle", style);

    SightlyWCMMode wcmMode = mock(SightlyWCMMode.class);
    when(wcmMode.isDisabled()).thenReturn(false);
    slingBindings.put("wcmmode", wcmMode);

    return resource;
}
 
Example 7
Source File: ProductListImplTest.java    From aem-core-cif-components with Apache License 2.0 5 votes vote down vote up
@Test
public void testProductListNoGraphqlClient() throws IOException {
    SlingBindings slingBindings = (SlingBindings) context.request().getAttribute(SlingBindings.class.getName());
    slingBindings.setResource(context.resourceResolver().getResource("/content/pageB"));
    slingBindings.put(WCMBindingsConstants.NAME_CURRENT_PAGE, context.pageManager().getPage("/content/pageB"));

    productListModel = context.request().adaptTo(ProductListImpl.class);

    Assert.assertTrue(productListModel.getTitle().isEmpty());
    Assert.assertTrue(productListModel.getImage().isEmpty());
    Assert.assertTrue(productListModel.getProducts().isEmpty());
}
 
Example 8
Source File: ProductCollectionImplTest.java    From aem-core-cif-components with Apache License 2.0 5 votes vote down vote up
private SlingBindings getSlingBindings(String resourcePath) {
    Page page = context.currentPage(PAGE);
    Resource productCollectionResource = context.resourceResolver().getResource(resourcePath);
    SlingBindings slingBindings = (SlingBindings) context.request().getAttribute(SlingBindings.class.getName());
    slingBindings.setResource(productCollectionResource);
    slingBindings.put(WCMBindingsConstants.NAME_CURRENT_PAGE, page);
    slingBindings.put(WCMBindingsConstants.NAME_PROPERTIES, productCollectionResource.getValueMap());
    return slingBindings;
}
 
Example 9
Source File: ProductCarouselImplEmptyTest.java    From aem-core-cif-components with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    carouselResource = Mockito.spy(context.resourceResolver().getResource(PRODUCTCAROUSEL));
    // GraphQL client is not available
    Mockito.when(carouselResource.adaptTo(GraphqlClient.class)).thenReturn(null);
    // This sets the page attribute injected in the models with @Inject or @ScriptVariable
    slingBindings = (SlingBindings) context.request().getAttribute(SlingBindings.class.getName());
    slingBindings.setResource(carouselResource);
    Page page = context.currentPage(PAGE);
    slingBindings.put(WCMBindingsConstants.NAME_CURRENT_PAGE, page);
}
 
Example 10
Source File: RelatedProductsImplTest.java    From aem-core-cif-components with Apache License 2.0 5 votes vote down vote up
private void setUp(RelationType relationType, String jsonResponsePath, boolean addSlugInSelector) throws Exception {
    Page page = context.currentPage(PAGE);
    String resourcePath = RESOURCES_PATHS.get(relationType);
    context.currentResource(resourcePath);
    relatedProductsResource = Mockito.spy(context.resourceResolver().getResource(resourcePath));

    if (addSlugInSelector) {
        MockRequestPathInfo requestPathInfo = (MockRequestPathInfo) context.request().getRequestPathInfo();
        requestPathInfo.setSelectorString("endurance-watch");
    }

    GraphqlClient graphqlClient = null;
    if (jsonResponsePath != null) {
        Query rootQuery = Utils.getQueryFromResource(jsonResponsePath);
        ProductInterface product = rootQuery.getProducts().getItems().get(0);
        products = PRODUCTS_GETTER.get(relationType).apply(product);
        graphqlClient = Utils.setupGraphqlClientWithHttpResponseFrom(jsonResponsePath);
        GraphqlClient finalGraphqlClient = graphqlClient;
        context.registerAdapter(Resource.class, GraphqlClient.class,
            (Function<Resource, GraphqlClient>) input -> input.getValueMap().get("cq:graphqlClient", String.class) != null
                ? finalGraphqlClient
                : null);
    }

    // This sets the page attribute injected in the models with @Inject or @ScriptVariable
    SlingBindings slingBindings = (SlingBindings) context.request().getAttribute(SlingBindings.class.getName());
    slingBindings.setResource(relatedProductsResource);
    slingBindings.put(WCMBindingsConstants.NAME_CURRENT_PAGE, page);
    slingBindings.put("properties", relatedProductsResource.getValueMap());

    relatedProducts = context.request().adaptTo(RelatedProductsImpl.class);
}
 
Example 11
Source File: SearchResultsImplTest.java    From aem-core-cif-components with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    Page page = Mockito.spy(context.currentPage(PAGE));
    context.currentResource(SEARCHRESULTS);
    Resource searchResultsResource = context.resourceResolver().getResource(SEARCHRESULTS);

    GraphqlClient graphqlClient = new GraphqlClientImpl();
    Whitebox.setInternalState(graphqlClient, "gson", QueryDeserializer.getGson());
    Whitebox.setInternalState(graphqlClient, "client", httpClient);
    Whitebox.setInternalState(graphqlClient, "httpMethod", HttpMethod.POST);

    Utils.setupHttpResponse("graphql/magento-graphql-introspection-result.json", httpClient, HttpStatus.SC_OK, "{__type");
    Utils.setupHttpResponse("graphql/magento-graphql-attributes-result.json", httpClient, HttpStatus.SC_OK, "{customAttributeMetadata");
    Utils.setupHttpResponse("graphql/magento-graphql-search-result.json", httpClient, HttpStatus.SC_OK, "{products");

    // This is needed by the SearchResultsService used by the productlist component
    pageResource = Mockito.spy(page.adaptTo(Resource.class));
    when(page.adaptTo(Resource.class)).thenReturn(pageResource);
    context.registerAdapter(Resource.class, GraphqlClient.class, (Function<Resource, GraphqlClient>) input -> input.getValueMap().get(
        "cq:graphqlClient") != null ? graphqlClient : null);

    // This sets the page attribute injected in the models with @Inject or @ScriptVariable
    SlingBindings slingBindings = (SlingBindings) context.request().getAttribute(SlingBindings.class.getName());
    slingBindings.setResource(searchResultsResource);
    slingBindings.put(WCMBindingsConstants.NAME_CURRENT_PAGE, page);
    slingBindings.put(WCMBindingsConstants.NAME_PROPERTIES, searchResultsResource.getValueMap());

    XSSAPI xssApi = mock(XSSAPI.class);
    when(xssApi.filterHTML(Mockito.anyString())).then(i -> i.getArgumentAt(0, String.class));
    slingBindings.put("xssApi", xssApi);

    Style style = mock(Style.class);
    when(style.get(Mockito.anyString(), Mockito.anyInt())).then(i -> i.getArgumentAt(1, Object.class));
    slingBindings.put("currentStyle", style);

    SightlyWCMMode wcmMode = mock(SightlyWCMMode.class);
    when(wcmMode.isDisabled()).thenReturn(false);
    slingBindings.put("wcmmode", wcmMode);
}
 
Example 12
Source File: ProductImplTest.java    From aem-core-cif-components with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
    Page page = context.currentPage(PAGE);
    httpClient = mock(HttpClient.class);

    context.currentResource(PRODUCT);
    productResource = Mockito.spy(context.resourceResolver().getResource(PRODUCT));

    Query rootQuery = Utils.getQueryFromResource("graphql/magento-graphql-product-result.json");
    product = rootQuery.getProducts().getItems().get(0);
    storeConfig = rootQuery.getStoreConfig();

    GraphqlClient graphqlClient = new GraphqlClientImpl();
    Whitebox.setInternalState(graphqlClient, "gson", QueryDeserializer.getGson());
    Whitebox.setInternalState(graphqlClient, "client", httpClient);
    Whitebox.setInternalState(graphqlClient, "httpMethod", HttpMethod.POST);

    Utils.setupHttpResponse("graphql/magento-graphql-product-result.json", httpClient, 200);

    context.registerAdapter(Resource.class, GraphqlClient.class, (Function<Resource, GraphqlClient>) input -> input.getValueMap().get(
        "cq:graphqlClient", String.class) != null ? graphqlClient : null);

    MockRequestPathInfo requestPathInfo = (MockRequestPathInfo) context.request().getRequestPathInfo();
    requestPathInfo.setSelectorString("beaumont-summit-kit");

    // This sets the page attribute injected in the models with @Inject or @ScriptVariable
    SlingBindings slingBindings = (SlingBindings) context.request().getAttribute(SlingBindings.class.getName());
    slingBindings.setResource(productResource);
    slingBindings.put(WCMBindingsConstants.NAME_CURRENT_PAGE, page);
    slingBindings.put(WCMBindingsConstants.NAME_PROPERTIES, productResource.getValueMap());

    XSSAPI xssApi = mock(XSSAPI.class);
    when(xssApi.filterHTML(Mockito.anyString())).then(i -> i.getArgumentAt(0, String.class));
    slingBindings.put("xssApi", xssApi);

    Style style = mock(Style.class);
    when(style.get(Mockito.anyString(), Mockito.anyBoolean())).then(i -> i.getArgumentAt(1, Boolean.class));
    slingBindings.put("currentStyle", style);

    SightlyWCMMode wcmMode = mock(SightlyWCMMode.class);
    when(wcmMode.isDisabled()).thenReturn(false);
    slingBindings.put("wcmmode", wcmMode);

    // context.request().adaptTo(ProductImpl.class); is moved to each test because it uses an internal cache
    // and we want to override the "slug" in testEditModePlaceholderData()

}
 
Example 13
Source File: ProductListImplTest.java    From aem-core-cif-components with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
    Page page = Mockito.spy(context.currentPage(PAGE));
    context.currentResource(PRODUCTLIST);
    productListResource = Mockito.spy(context.resourceResolver().getResource(PRODUCTLIST));

    category = Utils.getQueryFromResource("graphql/magento-graphql-search-result-with-category.json").getCategory();
    products = Utils.getQueryFromResource("graphql/magento-graphql-search-result-with-category.json").getProducts();

    graphqlClient = Mockito.spy(new GraphqlClientImpl());
    Whitebox.setInternalState(graphqlClient, "gson", QueryDeserializer.getGson());
    Whitebox.setInternalState(graphqlClient, "client", httpClient);
    Whitebox.setInternalState(graphqlClient, "httpMethod", HttpMethod.POST);

    Utils.setupHttpResponse("graphql/magento-graphql-introspection-result.json", httpClient, HttpStatus.SC_OK, "{__type");
    Utils.setupHttpResponse("graphql/magento-graphql-attributes-result.json", httpClient, HttpStatus.SC_OK, "{customAttributeMetadata");
    Utils.setupHttpResponse("graphql/magento-graphql-search-result-with-category.json", httpClient, HttpStatus.SC_OK, "{products");

    when(productListResource.adaptTo(ComponentsConfiguration.class)).thenReturn(MOCK_CONFIGURATION_OBJECT);
    context.registerAdapter(Resource.class, GraphqlClient.class, (Function<Resource, GraphqlClient>) input -> input.getValueMap().get(
        "cq:graphqlClient", String.class) != null ? graphqlClient : null);

    // This is needed by the SearchResultsService used by the productlist component
    pageResource = Mockito.spy(page.adaptTo(Resource.class));
    when(page.adaptTo(Resource.class)).thenReturn(pageResource);
    when(productListResource.adaptTo(ComponentsConfiguration.class)).thenReturn(MOCK_CONFIGURATION_OBJECT);

    Function<Resource, ComponentsConfiguration> adapter = r -> r.getPath().equals(PAGE) ? MOCK_CONFIGURATION_OBJECT
        : ComponentsConfiguration.EMPTY;
    context.registerAdapter(Resource.class, ComponentsConfiguration.class, adapter);

    MockRequestPathInfo requestPathInfo = (MockRequestPathInfo) context.request().getRequestPathInfo();
    requestPathInfo.setSelectorString("6");

    // This sets the page attribute injected in the models with @Inject or @ScriptVariable
    SlingBindings slingBindings = (SlingBindings) context.request().getAttribute(SlingBindings.class.getName());
    slingBindings.setResource(productListResource);
    slingBindings.put(WCMBindingsConstants.NAME_CURRENT_PAGE, page);
    slingBindings.put(WCMBindingsConstants.NAME_PROPERTIES, productListResource.getValueMap());

    XSSAPI xssApi = mock(XSSAPI.class);
    when(xssApi.filterHTML(Mockito.anyString())).then(i -> i.getArgumentAt(0, String.class));
    slingBindings.put("xssApi", xssApi);

    Style style = mock(Style.class);
    when(style.get(Mockito.anyString(), Mockito.anyInt())).then(i -> i.getArgumentAt(1, Object.class));
    slingBindings.put("currentStyle", style);

    SightlyWCMMode wcmMode = mock(SightlyWCMMode.class);
    when(wcmMode.isDisabled()).thenReturn(false);
    slingBindings.put("wcmmode", wcmMode);

    // context.request().adaptTo(ProductListImpl.class); is moved to each test because it uses an internal cache
    // and we want to override the "slug" in testEditModePlaceholderData()
}