Skip to content

Commit

Permalink
MGR-138 more distinct error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Müller authored and Matthias Müller committed Aug 19, 2022
1 parent 3df93ec commit 0eb3656
Show file tree
Hide file tree
Showing 8 changed files with 512 additions and 385 deletions.
6 changes: 4 additions & 2 deletions application-home/dictionary/manager-messages.properties
Expand Up @@ -351,8 +351,10 @@ site.deleted=Site has been deleted.
site.domain.exists=A different site with this domain already exists!
site.edit=Site {0}
site.grant=Grant access to sites
site.host.exists=Hostname overlaps with hostnames of site "{0}"!
site.hostalias.exists=Hostname aliases overlap with hostnames of site "{0}"!
site.host.inUse=Hostname ''{0}'' is used by site ''{1}''!
site.host.isAlias=Hostname ''{0}'' is used as an alias by site ''{1}''!
site.hostalias.isHostname=Hostname alias ''{0}'' is the hostname for site ''{1}''!
site.hostalias.inUse=The following aliases are used by site ''{1}'': {0}
site.hostalias.invalid=Hostname alias "{0}" is not a valid hostname.
site.name.exists=A different site with this name already exists!
site.not.exists=Site does not exist.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.appng</groupId>
<artifactId>appng-application-parent</artifactId>
<version>1.24.6-SNAPSHOT</version>
<version>1.25.0-SNAPSHOT</version>
</parent>

<scm>
Expand Down
Expand Up @@ -32,16 +32,19 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TimeZone;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.apache.commons.collections.comparators.ComparatorChain;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.appng.api.ApplicationException;
Expand Down Expand Up @@ -119,6 +122,7 @@
import org.appng.core.xml.repository.Packages;
import org.appng.forms.FormUpload;
import org.appng.persistence.repository.SearchQuery;
import org.appng.xml.platform.FieldDef;
import org.appng.xml.platform.Label;
import org.appng.xml.platform.Option;
import org.appng.xml.platform.Selection;
Expand Down Expand Up @@ -1085,19 +1089,32 @@ private void checkSite(Request request, Site site, FieldProcessor fp, Site curre
if (fp.hasField("site.name") && !siteRepository.isUnique(site.getId(), "name", site.getName())) {
fp.addErrorMessage(fp.getField("site.name"), request.getMessage(MessageConstants.SITE_NAME_EXISTS));
}
Set<String> hostnames = new HashSet<String>(site.getHostAliases());
Set<String> hostnames = new HashSet<>(site.getHostAliases());
hostnames.add(site.getHost());
List<SiteImpl> hostOverlapSites = siteRepository.findSitesForHostNames(hostnames);
for (SiteImpl ovlpSite : hostOverlapSites) {
if (site.getId() == ovlpSite.getId())
continue;
else {
if (ovlpSite.getHost().equals(site.getHost()) || ovlpSite.getHostAliases().contains(site.getHost()))
fp.addErrorMessage(fp.getField("site.host"),
request.getMessage(MessageConstants.SITE_HOST_EXISTS, ovlpSite.getName()));
else
fp.addErrorMessage(fp.getField("hostAliases"),
request.getMessage(MessageConstants.SITE_HOSTALIAS_EXISTS, ovlpSite.getName()));
if (!Objects.equals(site.getId(), ovlpSite.getId())) {
FieldDef field = fp.getField("site.host");
Object arg = site.getHost();
String messageKey;
if (ovlpSite.getHost().equals(site.getHost())) {
messageKey = MessageConstants.SITE_HOST_IN_USE;
} else if (ovlpSite.getHostAliases().contains(site.getHost())) {
messageKey = MessageConstants.SITE_HOST_IS_ALIAS;
} else {
field = fp.getField("hostAliases");
if (site.getHostAliases().contains(ovlpSite.getHost())) {
messageKey = MessageConstants.SITE_HOSTALIAS_IS_HOSTNAME;
arg = ovlpSite.getHost();
} else {
messageKey = MessageConstants.SITE_HOSTALIAS_IN_USE;
Stream<String> aliasesInUse = CollectionUtils
.intersection(site.getHostAliases(), ovlpSite.getHostAliases()).stream()
.map(s -> String.format("'%s'", s));
arg = StringUtils.join(aliasesInUse.iterator(), ", ");
}
}
fp.addErrorMessage(field, request.getMessage(messageKey, arg, ovlpSite.getName()));
}
}
if (!siteRepository.isUnique(site.getId(), "domain", site.getDomain())) {
Expand Down
71 changes: 31 additions & 40 deletions src/test/java/org/appng/application/manager/business/SitesTest.java
Expand Up @@ -16,19 +16,13 @@
package org.appng.application.manager.business;

import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.appng.api.FieldProcessor;
import org.appng.api.Platform;
import org.appng.api.ProcessingException;
import org.appng.api.model.Property;
import org.appng.api.model.Site.SiteState;
import org.appng.api.support.CallableAction;
import org.appng.api.support.CallableDataSource;
import org.appng.api.support.PropertyHolder;
import org.appng.application.manager.form.PropertyForm;
import org.appng.application.manager.form.SiteForm;
import org.appng.core.domain.PropertyImpl;
Expand All @@ -37,10 +31,10 @@
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.mockito.Mockito;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.google.common.collect.Sets;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@ContextConfiguration(inheritInitializers = false, initializers = SitesTest.class)
public class SitesTest extends AbstractTest {
Expand All @@ -55,21 +49,15 @@ public class SitesTest extends AbstractTest {
public void testCreateSite01() throws Exception {
propertyRepository.save(new PropertyImpl("platform." + Platform.Property.MESSAGING_ENABLED, null, "false"));

SiteImpl siteToCreate = new SiteImpl();
SiteForm siteForm = new SiteForm(siteToCreate);
siteToCreate.setName("site1");
siteToCreate.setHost("hostname1.domain.tld");
siteToCreate.setHostAliases(new HashSet<>(Arrays.asList("doppeltes", "lottchen")));
siteToCreate.setDomain("https://hostname1.domain.tld");
siteToCreate.setActive(true);

// prepares using appNG >= 1.19.1
PropertyForm form = new PropertyForm();
form.getProperty().setName(Platform.Property.MESSAGING_ENABLED);
form.getProperty().setDefaultString(Boolean.FALSE.toString());
getAction("propertyEvent", "create-platform-property").withParam(FORM_ACTION, "create-platform-property")
.getCallableAction(form).perform();

SiteForm siteForm = getSiteForm("site1", "hostname1.domain.tld", "https://hostname1.domain.tld",
Sets.newHashSet("doppeltes", "lottchen"));
CallableAction callableAction = getAction(SITE_EVENT, "create").withParam(FORM_ACTION, "create")
.getCallableAction(siteForm);

Expand All @@ -90,13 +78,8 @@ public void testCreateSite02ValidationFail() throws Exception {

@Test
public void testCreateSite03NameDuplicateFail() throws Exception {
SiteImpl siteToCreate = new SiteImpl();
SiteForm siteForm = new SiteForm(siteToCreate);
siteToCreate.setName("site1");
siteToCreate.setHost("other-hostname.domain.tld");
siteToCreate.setDomain("https://other-hostname.domain.tld");
siteToCreate.setActive(true);

SiteForm siteForm = getSiteForm("site1", "other-hostname.domain.tld", "https://other-hostname.domain.tld",
null);
CallableAction callableAction = getAction(SITE_EVENT, "create").withParam(FORM_ACTION, "create")
.getCallableAction(siteForm);
callableAction.perform();
Expand All @@ -105,13 +88,7 @@ public void testCreateSite03NameDuplicateFail() throws Exception {

@Test
public void testCreateSite04HostDuplicateFail() throws Exception {
SiteImpl siteToCreate = new SiteImpl();
SiteForm siteForm = new SiteForm(siteToCreate);
siteToCreate.setName("other-site");
siteToCreate.setHost("lottchen");
siteToCreate.setDomain("https://other-hostname.domain.tld");
siteToCreate.setActive(true);

SiteForm siteForm = getSiteForm("other-site", "lottchen", "https://other-hostname.domain.tld", null);
CallableAction callableAction = getAction(SITE_EVENT, "create").withParam(FORM_ACTION, "create")
.getCallableAction(siteForm);
callableAction.perform();
Expand All @@ -120,14 +97,18 @@ public void testCreateSite04HostDuplicateFail() throws Exception {

@Test
public void testCreateSite05AliasDuplicateFail() throws Exception {
SiteImpl siteToCreate = new SiteImpl();
SiteForm siteForm = new SiteForm(siteToCreate);
siteToCreate.setName("other-site");
siteToCreate.setHost("other-hostname.domain.tld");
siteToCreate.setHostAliases(new HashSet<>(Arrays.asList("", "hostname1.domain.tld")));
siteToCreate.setDomain("https://other-hostname.domain.tld");
siteToCreate.setActive(true);
SiteForm siteForm = getSiteForm("other-site", "other-hostname.domain.tld", "https://other-hostname.domain.tld",
Sets.newHashSet("", "hostname1.domain.tld"));
CallableAction callableAction = getAction(SITE_EVENT, "create").withParam(FORM_ACTION, "create")
.getCallableAction(siteForm);
callableAction.perform();
validate(callableAction.getAction());
}

@Test
public void testCreateSite06AliasDuplicateFail() throws Exception {
SiteForm siteForm = getSiteForm("other-site", "other-hostname.domain.tld", "https://other-hostname.domain.tld",
Sets.newHashSet("doppeltes", "lottchen"));
CallableAction callableAction = getAction(SITE_EVENT, "create").withParam(FORM_ACTION, "create")
.getCallableAction(siteForm);
callableAction.perform();
Expand Down Expand Up @@ -161,10 +142,8 @@ public void testShowSite() throws Exception {
@Test
public void testShowSites() throws Exception {
createSite();

CallableDataSource siteDatasource = getDataSource("sites").getCallableDataSource();
siteDatasource.perform("test");

validate(siteDatasource.getDatasource());
}

Expand All @@ -177,6 +156,18 @@ public void testShowSitesFiltered() throws Exception {
validate(siteDatasource.getDatasource());
}

private SiteForm getSiteForm(String name, String host, String domain, Set<String> aliases) {
SiteImpl siteToCreate = new SiteImpl();
siteToCreate.setName(name);
siteToCreate.setHost(host);
if (null != aliases) {
siteToCreate.setHostAliases(aliases);
}
siteToCreate.setDomain(domain);
siteToCreate.setActive(true);
return new SiteForm(siteToCreate);
}

private void createSite() {
SiteImpl realSite = new SiteImpl();
realSite.setName("site2");
Expand Down

0 comments on commit 0eb3656

Please sign in to comment.