Skip to content

Commit

Permalink
Automate creation of vanity URLs for each country & language
Browse files Browse the repository at this point in the history
  • Loading branch information
Yassine EL OUARDI committed Sep 14, 2023
1 parent 980d6f3 commit d8e5cf3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@

import javax.servlet.ServletException;

import com.drew.lang.annotations.NotNull;
import org.apache.commons.collections.CollectionUtils;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -63,15 +65,21 @@ protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse
Resource resource = request.getResourceResolver().getResource(ETC_ACS_COMMONS_LISTS_COUNTRIES_JCR_CONTENT_LIST);
if (Objects.nonNull(resource)) {
countries = new HashMap<>();

Check warning on line 67 in bundle/src/main/java/com/adobe/acs/commons/redirectmaps/impl/AddEntryServlet.java

View check run for this annotation

Codecov / codecov/patch

bundle/src/main/java/com/adobe/acs/commons/redirectmaps/impl/AddEntryServlet.java#L67

Added line #L67 was not covered by tests
@NotNull
Iterable<Resource> children = resource.getChildren();

Check warning on line 69 in bundle/src/main/java/com/adobe/acs/commons/redirectmaps/impl/AddEntryServlet.java

View check run for this annotation

Codecov / codecov/patch

bundle/src/main/java/com/adobe/acs/commons/redirectmaps/impl/AddEntryServlet.java#L69

Added line #L69 was not covered by tests
for (Resource childResource : children) {
String title = childResource.getValueMap().get("jcr:title", String.class);
String nodeValue = childResource.getValueMap().get("value", String.class);
countries.put(nodeValue, title);
if(children != null){
for (Resource childResource : children) {
ValueMap valueMap = childResource.getValueMap();

Check warning on line 72 in bundle/src/main/java/com/adobe/acs/commons/redirectmaps/impl/AddEntryServlet.java

View check run for this annotation

Codecov / codecov/patch

bundle/src/main/java/com/adobe/acs/commons/redirectmaps/impl/AddEntryServlet.java#L72

Added line #L72 was not covered by tests
if (valueMap != null) {
String title = valueMap.get("jcr:title", String.class);
String nodeValue = valueMap.get("value", String.class);
countries.put(nodeValue, title);

Check warning on line 76 in bundle/src/main/java/com/adobe/acs/commons/redirectmaps/impl/AddEntryServlet.java

View check run for this annotation

Codecov / codecov/patch

bundle/src/main/java/com/adobe/acs/commons/redirectmaps/impl/AddEntryServlet.java#L74-L76

Added lines #L74 - L76 were not covered by tests
}
}

Check warning on line 78 in bundle/src/main/java/com/adobe/acs/commons/redirectmaps/impl/AddEntryServlet.java

View check run for this annotation

Codecov / codecov/patch

bundle/src/main/java/com/adobe/acs/commons/redirectmaps/impl/AddEntryServlet.java#L78

Added line #L78 was not covered by tests
}
}
List<String> lines = RedirectEntriesUtils.readEntries(request);
if(CollectionUtils.isNotEmpty(lines)){
if(countries != null && !countries.isEmpty()){
for (Map.Entry<String,String> country : countries.entrySet()) {
String genericSource = country.getKey()+":" +source;
String genericTarget= "/"+country.getKey()+"/"+country.getValue()+target;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.*;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -41,12 +40,14 @@
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.testing.sling.MockResourceResolver;
import org.apache.tika.io.IOUtils;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -76,6 +77,10 @@ public class TestServlets {
@Mock
private Resource mockMapContentResource;

@Mock
private ResourceResolver resourceResolver;


private Map<String, String> value = new HashMap<>();

private ModifiableValueMap mvm = new ModifiableValueMap() {
Expand Down Expand Up @@ -292,7 +297,6 @@ public void testAddEntryServlet() throws ServletException, IOException {
log.info("testAddEntryServlet");
AddEntryServlet servlet = new AddEntryServlet();
servlet.doPost(mockSlingRequest, mockSlingResponse);

log.info("REDIRECT MAP:\n" + value.get(JcrConstants.JCR_DATA));
assertTrue(value.get(JcrConstants.JCR_DATA).contains("/source /target"));
log.info("Test successful!");
Expand Down

0 comments on commit d8e5cf3

Please sign in to comment.