-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #322 from adobe/dev
Sync dev to merge before release
- Loading branch information
Showing
17 changed files
with
520 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
core/src/main/java/com/venia/core/models/commerce/MySearchResultsImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/******************************************************************************* | ||
* | ||
* Copyright 2023 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
* | ||
******************************************************************************/ | ||
package com.venia.core.models.commerce; | ||
|
||
import com.adobe.cq.commerce.core.components.models.common.ProductListItem; | ||
import com.adobe.cq.commerce.core.components.models.searchresults.SearchResults; | ||
import com.adobe.cq.commerce.core.components.storefrontcontext.SearchResultsStorefrontContext; | ||
import com.adobe.cq.commerce.core.components.storefrontcontext.SearchStorefrontContext; | ||
import com.adobe.cq.commerce.core.search.models.SearchResultsSet; | ||
import com.adobe.cq.commerce.core.search.models.SorterKey; | ||
import com.adobe.cq.commerce.magento.graphql.ProductAttributeFilterInput; | ||
import com.adobe.cq.commerce.magento.graphql.ProductInterfaceQuery; | ||
import com.adobe.cq.wcm.core.components.models.Component; | ||
import com.adobe.cq.wcm.core.components.models.datalayer.ComponentData; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.apache.sling.api.SlingHttpServletRequest; | ||
import org.apache.sling.models.annotations.Model; | ||
import org.apache.sling.models.annotations.Via; | ||
import org.apache.sling.models.annotations.injectorspecific.Self; | ||
import org.apache.sling.models.annotations.via.ResourceSuperType; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.lang.reflect.Proxy; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* This class shows how to customize the sort fields of the product search results. | ||
* As am example we remove the support for sorting by position. | ||
* See the initModel() method for details. | ||
*/ | ||
@Model(adaptables = SlingHttpServletRequest.class, adapters = SearchResults.class, resourceType = MySearchResultsImpl.RESOURCE_TYPE) | ||
public class MySearchResultsImpl implements SearchResults , Component { | ||
protected static final String RESOURCE_TYPE = "venia/components/commerce/searchresults"; | ||
@Self | ||
@Via(type = ResourceSuperType.class) | ||
SearchResults searchResults; | ||
|
||
public MySearchResultsImpl() { | ||
System.out.println("TEST"); | ||
} | ||
|
||
@PostConstruct | ||
public void initModel() { | ||
// remove sort key with the name "position" | ||
List<SorterKey> keys = searchResults.getSearchResultsSet().getSorter().getKeys(); | ||
if (keys != null) { | ||
keys.removeIf(sorterKey -> sorterKey.getName().equals("position")); | ||
} | ||
} | ||
|
||
@Override | ||
public SearchStorefrontContext getSearchStorefrontContext() { | ||
return searchResults.getSearchStorefrontContext(); | ||
} | ||
|
||
@Override | ||
public SearchResultsStorefrontContext getSearchResultsStorefrontContext() { | ||
return searchResults.getSearchResultsStorefrontContext(); | ||
} | ||
|
||
@Override | ||
public void extendProductQueryWith(Consumer<ProductInterfaceQuery> consumer) { | ||
searchResults.extendProductQueryWith(consumer); | ||
} | ||
|
||
@Override | ||
public void extendProductFilterWith(Function<ProductAttributeFilterInput, ProductAttributeFilterInput> function) { | ||
searchResults.extendProductFilterWith(function); | ||
} | ||
|
||
@Override | ||
public Collection<ProductListItem> getProducts() { | ||
return searchResults.getProducts(); | ||
} | ||
|
||
@Override | ||
public SearchResultsSet getSearchResultsSet() { | ||
return searchResults.getSearchResultsSet(); | ||
} | ||
|
||
@Override | ||
public boolean loadClientPrice() { | ||
return searchResults.loadClientPrice(); | ||
} | ||
|
||
@Override | ||
public String getPaginationType() { | ||
return searchResults.getPaginationType(); | ||
} | ||
|
||
@Override | ||
public boolean isAddToCartEnabled() { | ||
return searchResults.isAddToCartEnabled(); | ||
} | ||
|
||
@Override | ||
public boolean isAddToWishListEnabled() { | ||
return searchResults.isAddToWishListEnabled(); | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return ((Component)searchResults).getId(); | ||
} | ||
|
||
@Override | ||
public ComponentData getData() { | ||
final ComponentData data = ((Component) searchResults).getData(); | ||
final AtomicReference<ComponentData> dataRef = new AtomicReference<>(); | ||
ComponentData componentData = (ComponentData) Proxy.newProxyInstance(this.getClass().getClassLoader(), | ||
new Class[]{ComponentData.class}, (proxy, method, args) -> { | ||
if (method.getName().equals("getJson")) { | ||
return String.format("{\"%s\":%s}", getId(), new ObjectMapper().writeValueAsString(dataRef.get())); | ||
} else if (method.getName().equals("getType")) { | ||
return getExportedType(); | ||
} | ||
return method.invoke(data, args); | ||
}); | ||
dataRef.set(componentData); | ||
return componentData; | ||
} | ||
|
||
@Override | ||
public String getAppliedCssClasses() { | ||
return ((Component)searchResults).getAppliedCssClasses(); | ||
} | ||
|
||
@Override | ||
public String getExportedType() { | ||
return "venia/components/commerce/searchresults"; | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
core/src/test/java/com/venia/core/models/commerce/MySearchResultsImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/******************************************************************************* | ||
* | ||
* Copyright 2023 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
* | ||
******************************************************************************/ | ||
package com.venia.core.models.commerce; | ||
|
||
import com.adobe.cq.commerce.core.components.models.searchresults.SearchResults; | ||
import com.adobe.cq.commerce.core.components.services.urls.UrlProvider; | ||
import com.adobe.cq.commerce.core.search.models.SearchResultsSet; | ||
import com.adobe.cq.commerce.core.search.models.Sorter; | ||
import com.adobe.cq.commerce.core.search.models.SorterKey; | ||
import com.adobe.cq.commerce.core.search.services.SearchResultsService; | ||
import com.adobe.cq.commerce.magento.graphql.ProductAttributeFilterInput; | ||
import com.day.cq.wcm.api.Page; | ||
import io.wcm.testing.mock.aem.junit5.AemContext; | ||
import io.wcm.testing.mock.aem.junit5.AemContextExtension; | ||
import org.apache.sling.testing.mock.sling.ResourceResolverType; | ||
import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(AemContextExtension.class) | ||
public class MySearchResultsImplTest { | ||
private static final String PAGE = "/content/page"; | ||
private final AemContext context = new AemContext(ResourceResolverType.JCR_MOCK); | ||
|
||
private SearchResults underTest; | ||
|
||
@BeforeEach | ||
void beforeEach() { | ||
MockitoAnnotations.initMocks(this); | ||
Page page = context.create().page(PAGE); | ||
Map<String, Object> props = new HashMap<>(); | ||
props.put("sling:resourceType", "venia/components/commerce/searchresults"); | ||
props.put("sling:resourceSuperType", "core/cif/components/commerce/searchresults/v1/searchresults"); | ||
context.create().resource(page, "test", props); | ||
context.currentResource(PAGE + "/jcr:content/test"); | ||
|
||
SearchResultsService searchResultsService = mock(SearchResultsService.class); | ||
SearchResultsSet searchResultsSet = mock(SearchResultsSet.class); | ||
Function<ProductAttributeFilterInput, ProductAttributeFilterInput> a = any(); | ||
when(searchResultsService.performSearch(any(), any(), any(), any(), any(), a)).thenReturn(searchResultsSet); | ||
Sorter sorter = mock(Sorter.class); | ||
when(searchResultsSet.getSorter()).thenReturn(sorter); | ||
SorterKey k1 = mock(SorterKey.class); | ||
when(k1.getName()).thenReturn("k1"); | ||
SorterKey k2 = mock(SorterKey.class); | ||
when(k2.getName()).thenReturn("k2"); | ||
SorterKey k3 = mock(SorterKey.class); | ||
when(k3.getName()).thenReturn("position"); | ||
when(sorter.getKeys()).thenReturn(new ArrayList<>(Arrays.asList(k1, k2, k3))); | ||
|
||
context.registerService(SearchResultsService.class, searchResultsService); | ||
context.registerService(UrlProvider.class, mock(UrlProvider.class)); | ||
context.addModelsForClasses(MySearchResultsImpl.class); | ||
|
||
MockSlingHttpServletRequest request = context.request(); | ||
request.addRequestParameter("search_query", "test"); | ||
|
||
underTest = request.adaptTo(MySearchResultsImpl.class); | ||
Assertions.assertNotNull(underTest); | ||
} | ||
|
||
@Test | ||
void testSorterKeys() { | ||
List<SorterKey> keys = underTest.getSearchResultsSet().getSorter().getKeys(); | ||
assertFalse(keys.stream().anyMatch(sorterKey -> sorterKey.getName().equals("position"))); | ||
assertEquals(2, keys.size()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
it.tests/src/main/java/com/venia/it/category/IgnoreOn65.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/******************************************************************************* | ||
* | ||
* Copyright 2023 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
* | ||
******************************************************************************/ | ||
package com.venia.it.category; | ||
|
||
public interface IgnoreOn65 {} |
16 changes: 16 additions & 0 deletions
16
it.tests/src/main/java/com/venia/it/category/IgnoreOnCloud.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/******************************************************************************* | ||
* | ||
* Copyright 2023 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
* | ||
******************************************************************************/ | ||
package com.venia.it.category; | ||
|
||
public interface IgnoreOnCloud {} |
Oops, something went wrong.