This is an automated email from the ASF dual-hosted git repository. sseifert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-starter-content.git
commit 4aa87c12031e7a00f779219e69ddafb7e6e23044 Author: Stefan Seifert <[email protected]> AuthorDate: Thu Nov 21 15:04:50 2024 +0100 SLING-12503 apply spotless code formatting --- pom.xml | 226 ++++++++++----------- .../starter/access/models/AccessFormPage.java | 35 ++-- .../apache/sling/starter/access/models/Ace.java | 195 +++++++++--------- .../sling/starter/access/models/AceUtils.java | 36 ++-- .../apache/sling/starter/access/models/Acl.java | 48 ++--- .../starter/access/models/PrincipalPrivilege.java | 27 +-- .../sling/starter/access/models/PrivilegeItem.java | 40 ++-- .../access/models/RestrictionDefinitionInfo.java | 29 +-- .../starter/access/models/RestrictionItem.java | 53 ++--- .../starter/access/models/AccessFormPageTest.java | 38 ++-- .../sling/starter/access/models/AceTest.java | 170 ++++++++-------- .../sling/starter/access/models/AceUtilsTest.java | 70 ++++--- .../sling/starter/access/models/AclTest.java | 48 ++--- .../access/models/PrincipalPrivilegeTest.java | 33 +-- .../starter/access/models/PrivilegeItemTest.java | 84 +++++--- .../models/RestrictionDefinitionInfoTest.java | 34 ++-- .../starter/access/models/RestrictionItemTest.java | 60 +++--- .../starter/access/models/TestGroupPrincipal.java | 29 +-- 18 files changed, 658 insertions(+), 597 deletions(-) diff --git a/pom.xml b/pom.xml index 6348875..1a706d7 100644 --- a/pom.xml +++ b/pom.xml @@ -1,4 +1,4 @@ -<?xml version="1.0" encoding="ISO-8859-1"?> +<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file @@ -19,124 +19,35 @@ --> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> - <modelVersion>4.0.0</modelVersion> - <parent> - <groupId>org.apache.sling</groupId> - <artifactId>sling-bundle-parent</artifactId> - <version>62</version> - <relativePath /> - </parent> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.sling</groupId> + <artifactId>sling-bundle-parent</artifactId> + <version>62</version> + <relativePath /> + </parent> - <artifactId>org.apache.sling.starter.content</artifactId> - <version>1.0.15-SNAPSHOT</version> + <artifactId>org.apache.sling.starter.content</artifactId> + <version>1.0.15-SNAPSHOT</version> - <name>Apache Sling Starter Content</name> - <description> - This bundle provides content that is loaded in the JCR repository - for the Sling Starter. - </description> + <name>Apache Sling Starter Content</name> + <description>This bundle provides content that is loaded in the JCR repository + for the Sling Starter.</description> - <scm> - <connection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-starter-content.git</connection> - <developerConnection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-starter-content.git</developerConnection> - <url>https://github.com/apache/sling-org-apache-sling-starter-content.git</url> - <tag>HEAD</tag> - </scm> - - <properties> - <sling.java.version>11</sling.java.version> - <frontend.target>target/frontend</frontend.target> - <project.build.outputTimestamp>1697761852</project.build.outputTimestamp> - <oak.version>1.16.0</oak.version> - <junit-jupiter.version>5.10.0</junit-jupiter.version> - </properties> + <scm> + <connection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-starter-content.git</connection> + <developerConnection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-starter-content.git</developerConnection> + <tag>HEAD</tag> + <url>https://github.com/apache/sling-org-apache-sling-starter-content.git</url> + </scm> - <build> - - <plugins> - <plugin> - <groupId>biz.aQute.bnd</groupId> - <artifactId>bnd-maven-plugin</artifactId> - <dependencies> - <dependency> - <groupId>org.apache.sling</groupId> - <artifactId>org.apache.sling.bnd.models</artifactId> - <version>1.0.0</version> - </dependency> - </dependencies> - </plugin> - <plugin> - <artifactId>maven-resources-plugin</artifactId> - <executions> - <execution> - <id>copy-resources</id> - <phase>validate</phase> - <goals> - <goal>copy-resources</goal> - </goals> - <configuration> - <outputDirectory>${basedir}/${frontend.target}</outputDirectory> - <resources> - <resource> - <directory>src/main/frontend</directory> - </resource> - </resources> - </configuration> - </execution> - </executions> - </plugin> - - <plugin> - <groupId>com.github.eirslett</groupId> - <artifactId>frontend-maven-plugin</artifactId> - <version>1.15.1</version> - <configuration> - <installDirectory>${frontend.target}</installDirectory> - <workingDirectory>${frontend.target}</workingDirectory> - </configuration> - <executions> - <execution> - <id>install node and npm</id> - <goals> - <goal>install-node-and-npm</goal> - </goals> - <configuration> - <nodeVersion>v22.11.0</nodeVersion> - </configuration> - </execution> - <execution> - <id>npm install</id> - <goals> - <goal>npm</goal> - </goals> - <configuration> - <arguments>install</arguments> - </configuration> - </execution> - <execution> - <id>gulp build</id> - <goals> - <goal>gulp</goal> - </goals> - </execution> - </executions> - </plugin> - </plugins> - </build> - <reporting> - <plugins> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-javadoc-plugin</artifactId> - <configuration> - <!-- No javadocs --> - <excludePackageNames> - org.apache.sling - </excludePackageNames> - </configuration> - </plugin> - </plugins> - </reporting> + <properties> + <sling.java.version>11</sling.java.version> + <frontend.target>target/frontend</frontend.target> + <project.build.outputTimestamp>1697761852</project.build.outputTimestamp> + <oak.version>1.16.0</oak.version> + <junit-jupiter.version>5.10.0</junit-jupiter.version> + </properties> <dependencies> <dependency> <groupId>org.osgi</groupId> @@ -244,4 +155,89 @@ </dependency> </dependencies> + + <build> + + <plugins> + <plugin> + <groupId>biz.aQute.bnd</groupId> + <artifactId>bnd-maven-plugin</artifactId> + <dependencies> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.bnd.models</artifactId> + <version>1.0.0</version> + </dependency> + </dependencies> + </plugin> + <plugin> + <artifactId>maven-resources-plugin</artifactId> + <executions> + <execution> + <id>copy-resources</id> + <goals> + <goal>copy-resources</goal> + </goals> + <phase>validate</phase> + <configuration> + <outputDirectory>${basedir}/${frontend.target}</outputDirectory> + <resources> + <resource> + <directory>src/main/frontend</directory> + </resource> + </resources> + </configuration> + </execution> + </executions> + </plugin> + + <plugin> + <groupId>com.github.eirslett</groupId> + <artifactId>frontend-maven-plugin</artifactId> + <version>1.15.1</version> + <configuration> + <installDirectory>${frontend.target}</installDirectory> + <workingDirectory>${frontend.target}</workingDirectory> + </configuration> + <executions> + <execution> + <id>install node and npm</id> + <goals> + <goal>install-node-and-npm</goal> + </goals> + <configuration> + <nodeVersion>v22.11.0</nodeVersion> + </configuration> + </execution> + <execution> + <id>npm install</id> + <goals> + <goal>npm</goal> + </goals> + <configuration> + <arguments>install</arguments> + </configuration> + </execution> + <execution> + <id>gulp build</id> + <goals> + <goal>gulp</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + <reporting> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <configuration> + <!-- No javadocs --> + <excludePackageNames>org.apache.sling</excludePackageNames> + </configuration> + </plugin> + </plugins> + </reporting> </project> diff --git a/src/main/java/org/apache/sling/starter/access/models/AccessFormPage.java b/src/main/java/org/apache/sling/starter/access/models/AccessFormPage.java index 80d6136..30129b6 100644 --- a/src/main/java/org/apache/sling/starter/access/models/AccessFormPage.java +++ b/src/main/java/org/apache/sling/starter/access/models/AccessFormPage.java @@ -1,27 +1,29 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file 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 + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * 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 + * 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 CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * 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 CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.apache.sling.starter.access.models; -import java.io.IOException; - import javax.annotation.PostConstruct; import javax.jcr.Node; import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.resource.Resource; @@ -72,7 +74,7 @@ public abstract class AccessFormPage { /** * Checks whether the current user has been granted privileges * to modify the access control of the current node. - * + * * @return true if the current user has the privileges, false otherwise */ public boolean getCanReadAccessControl() { @@ -82,7 +84,7 @@ public abstract class AccessFormPage { /** * Checks whether the current user has been granted privileges * to modify the access control of the current node. - * + * * @return true if the current user has the privileges, false otherwise */ public boolean getCanModifyAccessControl() { @@ -96,5 +98,4 @@ public abstract class AccessFormPage { } return location; } - } diff --git a/src/main/java/org/apache/sling/starter/access/models/Ace.java b/src/main/java/org/apache/sling/starter/access/models/Ace.java index 3d286c6..5a32767 100644 --- a/src/main/java/org/apache/sling/starter/access/models/Ace.java +++ b/src/main/java/org/apache/sling/starter/access/models/Ace.java @@ -1,21 +1,30 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file 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 + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * 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 + * 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 CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * 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 CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.apache.sling.starter.access.models; +import javax.annotation.PostConstruct; +import javax.jcr.RepositoryException; +import javax.jcr.Session; +import javax.jcr.Workspace; +import javax.jcr.security.AccessControlManager; +import javax.jcr.security.Privilege; + import java.io.IOException; import java.security.Principal; import java.util.ArrayList; @@ -33,12 +42,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; -import javax.annotation.PostConstruct; -import javax.jcr.RepositoryException; -import javax.jcr.Session; -import javax.jcr.Workspace; -import javax.jcr.security.AccessControlManager; -import javax.jcr.security.Privilege; import jakarta.json.Json; import jakarta.json.JsonArray; import jakarta.json.JsonArrayBuilder; @@ -47,7 +50,6 @@ import jakarta.json.JsonObject; import jakarta.json.JsonObjectBuilder; import jakarta.json.JsonString; import jakarta.json.JsonValue; - import org.apache.jackrabbit.api.JackrabbitSession; import org.apache.jackrabbit.api.JackrabbitWorkspace; import org.apache.jackrabbit.api.security.authorization.PrivilegeManager; @@ -73,18 +75,19 @@ import org.jetbrains.annotations.Nullable; /** * The ace page options. */ -@Model(adaptables=SlingHttpServletRequest.class) +@Model(adaptables = SlingHttpServletRequest.class) public class Ace extends AccessFormPage { // for principal ace protected static final String PATH_REPOSITORY = "/:repository"; // for matching restriction request parameters protected static final Pattern RESTRICTION_PATTERN = Pattern.compile("^restriction@([^@]+)@([^@]+)@(Allow|Deny)$"); - protected static final Pattern RESTRICTION_PATTERN_DELETE = Pattern.compile(String.format("^restriction@([^@]+)@([^@]+)%s$", - SlingPostConstants.SUFFIX_DELETE)); + protected static final Pattern RESTRICTION_PATTERN_DELETE = + Pattern.compile(String.format("^restriction@([^@]+)@([^@]+)%s$", SlingPostConstants.SUFFIX_DELETE)); // use these hints to influence the order that the privileges are displayed protected static final Map<String, Integer> privilegesPriority = createPrivilegesPriorityMap(); + protected static Map<String, Integer> createPrivilegesPriorityMap() { Map<String, Integer> myMap = new HashMap<>(); myMap.put(PrivilegeConstants.JCR_READ, 1); @@ -129,13 +132,13 @@ public class Ace extends AccessFormPage { if (principalId != null && !principalId.isEmpty()) { Session session = request.getResourceResolver().adaptTo(Session.class); if (session instanceof JackrabbitSession) { - UserManager userManager = ((JackrabbitSession)session).getUserManager(); + UserManager userManager = ((JackrabbitSession) session).getUserManager(); if (userManager != null) { Authorizable authorizable = userManager.getAuthorizable(principalId); if (authorizable != null) { isInValidPrincipal = false; } else { - //no user/group matches the supplied principal id + // no user/group matches the supplied principal id } } } @@ -153,26 +156,27 @@ public class Ace extends AccessFormPage { } public Collection<PrivilegeItem> getPrivileges() throws RepositoryException { - Map<Privilege, PrivilegeItem> privilegesMap = getPersistedPrivilegesMap(); + Map<Privilege, PrivilegeItem> privilegesMap = getPersistedPrivilegesMap(); if (privilegesMap == null || privilegesMap.isEmpty()) { return Collections.emptyList(); } - //make a temp map for quick lookup below + // make a temp map for quick lookup below Set<RestrictionDefinition> supportedRestrictions = getSupportedRestrictions(); Map<String, RestrictionDefinition> srMap = toSrMap(supportedRestrictions); - Map<String, List<RestrictionItem>> postedAllowRestrictionsMap = new HashMap<>(); - Map<String, List<RestrictionItem>> postedDenyRestrictionsMap = new HashMap<>(); - Map<String, String[]> fieldValues = populateEntriesFromPreviousFailedPost(postedAllowRestrictionsMap, postedDenyRestrictionsMap, srMap); + Map<String, List<RestrictionItem>> postedAllowRestrictionsMap = new HashMap<>(); + Map<String, List<RestrictionItem>> postedDenyRestrictionsMap = new HashMap<>(); + Map<String, String[]> fieldValues = + populateEntriesFromPreviousFailedPost(postedAllowRestrictionsMap, postedDenyRestrictionsMap, srMap); Map<String, String[]> toDeleteFieldValues = getFieldValuesForPattern(RESTRICTION_PATTERN_DELETE); - //entries from the previous failed POST. + // entries from the previous failed POST. for (PrivilegeItem entry : privilegesMap.values()) { String privilegeName = entry.getName(); // check for any submitted form fields in case of error and redisplay of the page String paramValue = request.getParameter(String.format("privilege@%s", privilegeName)); if (paramValue != null) { - //req param was here from a failed post? + // req param was here from a failed post? if ("granted".equals(paramValue)) { entry.setGranted(true); } else if ("denied".equals(paramValue)) { @@ -182,7 +186,7 @@ public class Ace extends AccessFormPage { // check for delete existing param String paramDeleteValue = request.getParameter(String.format("privilege@%s@Delete", privilegeName)); if (paramDeleteValue != null) { - //req param was here from a failed post? + // req param was here from a failed post? if ("granted".equals(paramDeleteValue)) { entry.setGranted(false); } else if ("denied".equals(paramDeleteValue)) { @@ -191,12 +195,15 @@ public class Ace extends AccessFormPage { } } - for (boolean forAllow : new boolean [] {true, false}) { + for (boolean forAllow : new boolean[] {true, false}) { // first add items for any posted fields - Map<String, List<RestrictionItem>> postedRestrictionsMap = forAllow ? postedAllowRestrictionsMap : postedDenyRestrictionsMap; - List<RestrictionItem> newRestrictionsList = postedRestrictionsMap.computeIfAbsent(privilegeName, pn -> new ArrayList<>()); - //now merge in any declared restrictions that were not posted - List<RestrictionItem> declaredRestrictions = forAllow ? entry.getAllowRestrictions() : entry.getDenyRestrictions(); + Map<String, List<RestrictionItem>> postedRestrictionsMap = + forAllow ? postedAllowRestrictionsMap : postedDenyRestrictionsMap; + List<RestrictionItem> newRestrictionsList = + postedRestrictionsMap.computeIfAbsent(privilegeName, pn -> new ArrayList<>()); + // now merge in any declared restrictions that were not posted + List<RestrictionItem> declaredRestrictions = + forAllow ? entry.getAllowRestrictions() : entry.getDenyRestrictions(); if (declaredRestrictions != null && !declaredRestrictions.isEmpty()) { for (RestrictionItem ri : declaredRestrictions) { String restrictionName = ri.getName(); @@ -206,14 +213,15 @@ public class Ace extends AccessFormPage { String fieldKey = String.format("%s@%s", fieldKeyPrefix, (forAllow ? "Allow" : "Deny")); // skip it if it was requested to be deleted in the previous POST attempt // or already handled above - if (toDeleteFieldValues.containsKey(String.format("%s%s", fieldKeyPrefix, SlingPostConstants.SUFFIX_DELETE))) { + if (toDeleteFieldValues.containsKey( + String.format("%s%s", fieldKeyPrefix, SlingPostConstants.SUFFIX_DELETE))) { addIt = false; } else if (fieldValues.containsKey(fieldKey)) { // mark the form posted item as exists since it also // had a persisted value newRestrictionsList.stream() - .filter(list -> list.getName().equals(restrictionName)) - .forEach(item -> item.setExists(true)); + .filter(list -> list.getName().equals(restrictionName)) + .forEach(item -> item.setExists(true)); addIt = false; } @@ -235,18 +243,18 @@ public class Ace extends AccessFormPage { // populate restrictions to delete here. toDeleteFieldValues.keySet().stream() - .filter(key -> RESTRICTION_PATTERN_DELETE.matcher(key).matches()) - .forEach(key -> { - Matcher matcher = RESTRICTION_PATTERN_DELETE.matcher(key); - if (matcher.matches()) { - String restrictionName = matcher.group(2); - if (forAllow) { - entry.addAllowRestrictionToDelete(restrictionName); - } else { - entry.addDenyRestrictionToDelete(restrictionName); + .filter(key -> RESTRICTION_PATTERN_DELETE.matcher(key).matches()) + .forEach(key -> { + Matcher matcher = RESTRICTION_PATTERN_DELETE.matcher(key); + if (matcher.matches()) { + String restrictionName = matcher.group(2); + if (forAllow) { + entry.addAllowRestrictionToDelete(restrictionName); + } else { + entry.addDenyRestrictionToDelete(restrictionName); + } } - } - }); + }); } } @@ -322,7 +330,8 @@ public class Ace extends AccessFormPage { * @param srMap map where the key is the restriction name and the value is the restriction definition * @return map of field values that were found in the form context */ - protected Map<String, String[]> populateEntriesFromPreviousFailedPost(Map<String, List<RestrictionItem>> allowMap, + protected Map<String, String[]> populateEntriesFromPreviousFailedPost( + Map<String, List<RestrictionItem>> allowMap, Map<String, List<RestrictionItem>> denyMap, Map<String, RestrictionDefinition> srMap) { Map<String, String[]> toDeleteFieldValues = getFieldValuesForPattern(RESTRICTION_PATTERN_DELETE); @@ -331,7 +340,7 @@ public class Ace extends AccessFormPage { for (Entry<String, String[]> entry : fieldValuesEntrySet) { String deleteKey = String.format("%s%s", entry.getKey(), SlingPostConstants.SUFFIX_DELETE); if (toDeleteFieldValues.containsKey(deleteKey)) { - //it was requested to be deleted in the previous POST attempt + // it was requested to be deleted in the previous POST attempt continue; } @@ -348,7 +357,7 @@ public class Ace extends AccessFormPage { if (rd.getRequiredType().isArray()) { value = strings; } else if (strings.length > 0) { - //use the first one? + // use the first one? value = strings[0]; } @@ -368,12 +377,13 @@ public class Ace extends AccessFormPage { * @param list the list of restriction items * @param supportedRestrictions supported restrictions set */ - protected void populateEntriesForMissingMandatoryRestrictions(List<RestrictionItem> list, - Set<RestrictionDefinition> supportedRestrictions) { + protected void populateEntriesForMissingMandatoryRestrictions( + List<RestrictionItem> list, Set<RestrictionDefinition> supportedRestrictions) { if (supportedRestrictions != null) { for (RestrictionDefinition rd : supportedRestrictions) { - if (rd.isMandatory() && list.stream().noneMatch(item -> item.getName().equals(rd.getName()))) { - //missing it, so add an item to the list + if (rd.isMandatory() + && list.stream().noneMatch(item -> item.getName().equals(rd.getName()))) { + // missing it, so add an item to the list RestrictionItem ri = new RestrictionItem(rd, null, false); list.add(ri); } @@ -397,14 +407,14 @@ public class Ace extends AccessFormPage { /** * Calculates the supported privileges in the resource path exists, or the registered * privileges if the resource path does not exist - * + * * @param jcrSession the current session * @param resourcePath the resource path to consider * @return * @throws RepositoryException */ - protected @NotNull Privilege[] getSupportedOrRegisteredPrivileges(@NotNull Session jcrSession, @Nullable String resourcePath) - throws RepositoryException { + protected @NotNull Privilege[] getSupportedOrRegisteredPrivileges( + @NotNull Session jcrSession, @Nullable String resourcePath) throws RepositoryException { Privilege[] supportedPrivileges = null; if (resourcePath != null && jcrSession.nodeExists(resourcePath)) { supportedPrivileges = jcrSession.getAccessControlManager().getSupportedPrivileges(resourcePath); @@ -412,7 +422,7 @@ public class Ace extends AccessFormPage { // non-existing path. We can't determine what is supported there, so consider all registered privileges Workspace workspace = jcrSession.getWorkspace(); if (workspace instanceof JackrabbitWorkspace) { - PrivilegeManager privilegeManager = ((JackrabbitWorkspace)workspace).getPrivilegeManager(); + PrivilegeManager privilegeManager = ((JackrabbitWorkspace) workspace).getPrivilegeManager(); supportedPrivileges = privilegeManager.getRegisteredPrivileges(); } } @@ -425,15 +435,16 @@ public class Ace extends AccessFormPage { Privilege[] supportedPrivileges = null; try { Session jcrSession = request.getResourceResolver().adaptTo(Session.class); - supportedPrivileges = getSupportedOrRegisteredPrivileges(jcrSession, - PATH_REPOSITORY.equals(acePath) ? null : acePath ); + supportedPrivileges = + getSupportedOrRegisteredPrivileges(jcrSession, PATH_REPOSITORY.equals(acePath) ? null : acePath); } catch (RepositoryException e) { - //ignore + // ignore supportedPrivileges = null; } if (supportedPrivileges != null) { for (Privilege privilege : supportedPrivileges) { - PrivilegeItem p1 = new PrivilegeItem(privilege.getName(), false, false, privilegeToLongestPath.get(privilege)); + PrivilegeItem p1 = + new PrivilegeItem(privilege.getName(), false, false, privilegeToLongestPath.get(privilege)); newMap.put(privilege, p1); } } @@ -458,14 +469,15 @@ public class Ace extends AccessFormPage { aceExists = true; AccessControlManager acm = jcrSession.getAccessControlManager(); - //make a temp map for quick lookup below + // make a temp map for quick lookup below Set<RestrictionDefinition> supportedRestrictions = getSupportedRestrictions(); Map<String, RestrictionDefinition> srMap = toSrMap(supportedRestrictions); JsonObject privileges = ace.getJsonObject("privileges"); for (String pn : privileges.keySet()) { Privilege p = acm.privilegeFromName(pn); - PrivilegeItem privilegeItem = persistedPrivilegesMap.computeIfAbsent(p, key -> new PrivilegeItem(key.getName(), false, false, privilegeToLongestPath.get(key))); + PrivilegeItem privilegeItem = persistedPrivilegesMap.computeIfAbsent( + p, key -> new PrivilegeItem(key.getName(), false, false, privilegeToLongestPath.get(key))); JsonObject privilegeObj = privileges.getJsonObject(pn); JsonValue allowJsonValue = privilegeObj.get("allow"); @@ -473,7 +485,8 @@ public class Ace extends AccessFormPage { privilegeItem.setAllowExists(true); privilegeItem.setGranted(true); if (allowJsonValue instanceof JsonObject) { - List<RestrictionItem> restrictionItems = jsonToRestrictionItems(srMap, (JsonObject)allowJsonValue); + List<RestrictionItem> restrictionItems = + jsonToRestrictionItems(srMap, (JsonObject) allowJsonValue); privilegeItem.setAllowRestrictions(restrictionItems); } } @@ -482,7 +495,8 @@ public class Ace extends AccessFormPage { privilegeItem.setDenyExists(true); privilegeItem.setDenied(true); if (denyJsonValue instanceof JsonObject) { - List<RestrictionItem> restrictionItems = jsonToRestrictionItems(srMap, (JsonObject)denyJsonValue); + List<RestrictionItem> restrictionItems = + jsonToRestrictionItems(srMap, (JsonObject) denyJsonValue); privilegeItem.setDenyRestrictions(restrictionItems); } } @@ -492,8 +506,8 @@ public class Ace extends AccessFormPage { return persistedPrivilegesMap; } - protected List<RestrictionItem> jsonToRestrictionItems(Map<String, RestrictionDefinition> srMap, - JsonObject restrictionsObj) { + protected List<RestrictionItem> jsonToRestrictionItems( + Map<String, RestrictionDefinition> srMap, JsonObject restrictionsObj) { List<RestrictionItem> restrictionItems = new ArrayList<>(); for (Entry<String, JsonValue> entry : restrictionsObj.entrySet()) { String rn = entry.getKey(); @@ -502,14 +516,14 @@ public class Ace extends AccessFormPage { Object value = null; JsonValue jsonValue = entry.getValue(); if (jsonValue instanceof JsonArray) { - JsonArray jsonArray = (JsonArray)jsonValue; - String [] values = new String[jsonArray.size()]; + JsonArray jsonArray = (JsonArray) jsonValue; + String[] values = new String[jsonArray.size()]; for (int i = 0; i < values.length; i++) { values[i] = jsonArray.getString(i); } value = values; } else if (jsonValue instanceof JsonString) { - value = ((JsonString)jsonValue).getString(); + value = ((JsonString) jsonValue).getString(); } restrictionItems.add(new RestrictionItem(rd, value, true)); } @@ -527,9 +541,9 @@ public class Ace extends AccessFormPage { public List<RestrictionDefinitionInfo> getSupportedRestrictionsInfo() { return getSupportedRestrictions().stream() - .map(rd -> new RestrictionDefinitionInfo(rd.getName(), rd)) - .sorted(Comparator.comparing(RestrictionDefinitionInfo::getDisplayName)) - .collect(Collectors.toList()); + .map(rd -> new RestrictionDefinitionInfo(rd.getName(), rd)) + .sorted(Comparator.comparing(RestrictionDefinitionInfo::getDisplayName)) + .collect(Collectors.toList()); } /** @@ -550,7 +564,8 @@ public class Ace extends AccessFormPage { if (aggregatePrivileges != null && aggregatePrivileges.length > 0) { // order these so the client side iteration will process from the top down List<Privilege> list = new ArrayList<>(Arrays.asList(aggregatePrivileges)); - list.sort((Privilege o1, Privilege o2) -> privilegeToLongestPath.get(o1).compareTo(privilegeToLongestPath.get(o2))); + list.sort((Privilege o1, Privilege o2) -> + privilegeToLongestPath.get(o1).compareTo(privilegeToLongestPath.get(o2))); JsonArrayBuilder aggregateArray = factory.createArrayBuilder(); @@ -572,9 +587,9 @@ public class Ace extends AccessFormPage { JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder(); Set<String> alreadyProcessed = new HashSet<>(); for (PrivilegeItem pi : getPrivileges()) { - if ((forAllow && pi.getGranted()) || - (!forAllow && pi.getDenied())) { - List<RestrictionItem> restrictions = forAllow ? pi.getAllowRestrictions() : pi.getDenyRestrictions(); + if ((forAllow && pi.getGranted()) || (!forAllow && pi.getDenied())) { + List<RestrictionItem> restrictions = + forAllow ? pi.getAllowRestrictions() : pi.getDenyRestrictions(); for (RestrictionItem ri : restrictions) { if (ri.isExists()) { String name = String.format("%s@%s", pi.getName(), ri.getName()); @@ -599,12 +614,12 @@ public class Ace extends AccessFormPage { public Collection<PrincipalPrivilege> getOrderList() throws RepositoryException { List<PrincipalPrivilege> list = new ArrayList<>(); Session jcrSession = request.getResourceResolver().adaptTo(Session.class); - PrincipalManager principalManager = ((JackrabbitSession)jcrSession).getPrincipalManager(); + PrincipalManager principalManager = ((JackrabbitSession) jcrSession).getPrincipalManager(); String pid = getPrincipalId(); JsonObject acl = getAcl.getAcl(jcrSession, getAcePath()); for (String uid : acl.keySet()) { if (pid != null && pid.equals(uid)) { - //skip it + // skip it continue; } Principal principal = principalManager.getPrincipal(uid); @@ -616,10 +631,10 @@ public class Ace extends AccessFormPage { return list; } - protected String [] fieldValuesFromReqParams(RequestParameter[] paramValues) { + protected String[] fieldValuesFromReqParams(RequestParameter[] paramValues) { String[] fieldValues = null; fieldValues = new String[paramValues.length]; - for (int i=0; i < paramValues.length; i++) { + for (int i = 0; i < paramValues.length; i++) { fieldValues[i] = paramValues[i].getString(); } return fieldValues; @@ -628,22 +643,21 @@ public class Ace extends AccessFormPage { /** * Gets the values of the specified field from the request. If not * supplied the values from the supplied resource are returned. - * + * * @param keyPattern the regular expression to used to match against the field keys * @return the values of the field or empty map if not available */ protected Map<String, String[]> getFieldValuesForPattern(Pattern keyPattern) { Map<String, String[]> resultMap = new HashMap<>(); - @NotNull - RequestParameterMap requestParameterMap = request.getRequestParameterMap(); + @NotNull RequestParameterMap requestParameterMap = request.getRequestParameterMap(); Set<Entry<String, RequestParameter[]>> entrySet2 = requestParameterMap.entrySet(); for (Entry<String, RequestParameter[]> entry : entrySet2) { String key2 = entry.getKey(); if (!resultMap.containsKey(key2)) { Matcher matcher = keyPattern.matcher(key2); if (matcher.matches()) { - //just use the original request value + // just use the original request value RequestParameter[] paramValues = entry.getValue(); if (paramValues != null) { String[] fieldValues = fieldValuesFromReqParams(paramValues); @@ -655,5 +669,4 @@ public class Ace extends AccessFormPage { return resultMap; } - } diff --git a/src/main/java/org/apache/sling/starter/access/models/AceUtils.java b/src/main/java/org/apache/sling/starter/access/models/AceUtils.java index 56384a8..ebd04ff 100644 --- a/src/main/java/org/apache/sling/starter/access/models/AceUtils.java +++ b/src/main/java/org/apache/sling/starter/access/models/AceUtils.java @@ -1,29 +1,31 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file 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 + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * 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 + * 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 CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * 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 CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.apache.sling.starter.access.models; -import java.util.HashMap; -import java.util.Map; - import javax.jcr.RepositoryException; import javax.jcr.Session; import javax.jcr.security.AccessControlManager; import javax.jcr.security.Privilege; +import java.util.HashMap; +import java.util.Map; + import org.apache.jackrabbit.oak.spi.security.privilege.PrivilegeConstants; /** @@ -39,7 +41,8 @@ public class AceUtils { * If the privilege is contained in multiple aggregate privileges, then * calculate the instance with the greatest depth. */ - private static void toLongestPath(String prefix, Privilege parentPrivilege, Map<Privilege, String> privilegeToLongestPath) { + private static void toLongestPath( + String prefix, Privilege parentPrivilege, Map<Privilege, String> privilegeToLongestPath) { Privilege[] declaredAggregatePrivileges = parentPrivilege.getDeclaredAggregatePrivileges(); for (Privilege privilege : declaredAggregatePrivileges) { String candidatePath = String.format("%s/%s", prefix, privilege.getName()); @@ -67,5 +70,4 @@ public class AceUtils { toLongestPath(jcrAll.getName(), jcrAll, privilegeToLongestPath); return privilegeToLongestPath; } - } diff --git a/src/main/java/org/apache/sling/starter/access/models/Acl.java b/src/main/java/org/apache/sling/starter/access/models/Acl.java index 179a265..becffae 100644 --- a/src/main/java/org/apache/sling/starter/access/models/Acl.java +++ b/src/main/java/org/apache/sling/starter/access/models/Acl.java @@ -1,21 +1,26 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file 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 + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * 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 + * 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 CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * 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 CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.apache.sling.starter.access.models; +import javax.jcr.RepositoryException; +import javax.jcr.Session; + import java.security.Principal; import java.util.ArrayList; import java.util.Collection; @@ -23,11 +28,8 @@ import java.util.List; import java.util.Map.Entry; import java.util.concurrent.atomic.AtomicBoolean; -import javax.jcr.RepositoryException; -import javax.jcr.Session; import jakarta.json.JsonObject; import jakarta.json.JsonValue; - import org.apache.jackrabbit.api.JackrabbitSession; import org.apache.jackrabbit.api.security.principal.PrincipalManager; import org.apache.sling.api.SlingHttpServletRequest; @@ -35,7 +37,7 @@ import org.apache.sling.jcr.jackrabbit.accessmanager.GetAcl; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.OSGiService; -@Model(adaptables=SlingHttpServletRequest.class) +@Model(adaptables = SlingHttpServletRequest.class) public class Acl extends AccessFormPage { private List<PrincipalPrivilege> principalPrivilegeList; @@ -47,7 +49,7 @@ public class Acl extends AccessFormPage { principalPrivilegeList = new ArrayList<>(); Session jcrSession = request.getResourceResolver().adaptTo(Session.class); - PrincipalManager principalManager = ((JackrabbitSession)jcrSession).getPrincipalManager(); + PrincipalManager principalManager = ((JackrabbitSession) jcrSession).getPrincipalManager(); JsonObject acl = getAcl.getAcl(jcrSession, resource.getPath()); for (Entry<String, JsonValue> entry : acl.entrySet()) { String uid = entry.getKey(); @@ -56,12 +58,11 @@ public class Acl extends AccessFormPage { PrincipalPrivilege pi = new PrincipalPrivilege(principal); AtomicBoolean allow = new AtomicBoolean(false); AtomicBoolean deny = new AtomicBoolean(false); - JsonObject privilegesObj = ((JsonObject)entry.getValue()).getJsonObject("privileges"); - privilegesObj.values().stream() - .forEach(item -> { - allow.set(allow.get() || ((JsonObject)item).containsKey("allow")); - deny.set(deny.get() || ((JsonObject)item).containsKey("deny")); - }); + JsonObject privilegesObj = ((JsonObject) entry.getValue()).getJsonObject("privileges"); + privilegesObj.values().stream().forEach(item -> { + allow.set(allow.get() || ((JsonObject) item).containsKey("allow")); + deny.set(deny.get() || ((JsonObject) item).containsKey("deny")); + }); if (allow.get()) { pi.setAllow(true); } @@ -75,5 +76,4 @@ public class Acl extends AccessFormPage { return principalPrivilegeList; } - } diff --git a/src/main/java/org/apache/sling/starter/access/models/PrincipalPrivilege.java b/src/main/java/org/apache/sling/starter/access/models/PrincipalPrivilege.java index 8fe31e1..994e07c 100644 --- a/src/main/java/org/apache/sling/starter/access/models/PrincipalPrivilege.java +++ b/src/main/java/org/apache/sling/starter/access/models/PrincipalPrivilege.java @@ -1,18 +1,20 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file 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 + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * 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 + * 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 CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * 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 CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.apache.sling.starter.access.models; @@ -70,5 +72,4 @@ public class PrincipalPrivilege { public boolean getIsGroup() { return isGroup; } - } diff --git a/src/main/java/org/apache/sling/starter/access/models/PrivilegeItem.java b/src/main/java/org/apache/sling/starter/access/models/PrivilegeItem.java index 50a872b..e8cf046 100644 --- a/src/main/java/org/apache/sling/starter/access/models/PrivilegeItem.java +++ b/src/main/java/org/apache/sling/starter/access/models/PrivilegeItem.java @@ -1,18 +1,20 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file 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 + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * 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 + * 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 CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * 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 CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.apache.sling.starter.access.models; @@ -39,8 +41,14 @@ public class PrivilegeItem { public PrivilegeItem(String name, boolean granted, boolean denied, String longestPath) { this(name, granted, denied, longestPath, Collections.emptyList(), Collections.emptyList()); } - public PrivilegeItem(String name, boolean granted, boolean denied, String longestPath, - List<RestrictionItem> allowRestrictions, List<RestrictionItem> denyRestrictions) { + + public PrivilegeItem( + String name, + boolean granted, + boolean denied, + String longestPath, + List<RestrictionItem> allowRestrictions, + List<RestrictionItem> denyRestrictions) { super(); this.name = name; this.granted = granted; @@ -97,7 +105,7 @@ public class PrivilegeItem { public void addExtraCssClass(String addClass) { if (this.extraCssClasses == null) { - this.extraCssClasses = addClass; + this.extraCssClasses = addClass; } else { this.extraCssClasses = String.format("%s %s", this.extraCssClasses, addClass); } @@ -128,6 +136,7 @@ public class PrivilegeItem { public void setAllowRestrictions(List<RestrictionItem> restrictions) { this.allowRestrictions = restrictions; } + public void setDenyRestrictions(List<RestrictionItem> restrictions) { this.denyRestrictions = restrictions; } @@ -182,5 +191,4 @@ public class PrivilegeItem { } return Collections.emptySet(); } - } diff --git a/src/main/java/org/apache/sling/starter/access/models/RestrictionDefinitionInfo.java b/src/main/java/org/apache/sling/starter/access/models/RestrictionDefinitionInfo.java index 35896ae..ca55113 100644 --- a/src/main/java/org/apache/sling/starter/access/models/RestrictionDefinitionInfo.java +++ b/src/main/java/org/apache/sling/starter/access/models/RestrictionDefinitionInfo.java @@ -1,24 +1,26 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file 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 + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * 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 + * 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 CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * 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 CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.apache.sling.starter.access.models; +import org.apache.jackrabbit.oak.api.Type; import org.apache.jackrabbit.oak.spi.security.authorization.restriction.RestrictionDefinition; import org.jetbrains.annotations.NotNull; -import org.apache.jackrabbit.oak.api.Type; public class RestrictionDefinitionInfo implements RestrictionDefinition { private String displayName; @@ -47,5 +49,4 @@ public class RestrictionDefinitionInfo implements RestrictionDefinition { public boolean isMandatory() { return rd.isMandatory(); } - } diff --git a/src/main/java/org/apache/sling/starter/access/models/RestrictionItem.java b/src/main/java/org/apache/sling/starter/access/models/RestrictionItem.java index 7c35675..f57db29 100644 --- a/src/main/java/org/apache/sling/starter/access/models/RestrictionItem.java +++ b/src/main/java/org/apache/sling/starter/access/models/RestrictionItem.java @@ -1,28 +1,30 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file 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 + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * 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 + * 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 CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * 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 CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.apache.sling.starter.access.models; +import javax.jcr.RepositoryException; +import javax.jcr.Value; + import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import javax.jcr.RepositoryException; -import javax.jcr.Value; - import org.apache.jackrabbit.oak.spi.security.authorization.restriction.RestrictionDefinition; public class RestrictionItem { @@ -48,23 +50,23 @@ public class RestrictionItem { public String getValue() throws RepositoryException { String v = null; if (value instanceof Value) { - v = ((Value)value).getString(); + v = ((Value) value).getString(); } else if (value instanceof Value[]) { - Value[] va = (Value[])value; + Value[] va = (Value[]) value; if (va.length > 0) { v = va[0].getString(); } } else if (value instanceof String) { - v = (String)value; + v = (String) value; } else if (value instanceof String[]) { - String[] values = (String[])value; + String[] values = (String[]) value; if (values.length > 0) { v = values[0]; } } if (v == null) { - //empty string if no values yet + // empty string if no values yet v = ""; } return v; @@ -73,19 +75,19 @@ public class RestrictionItem { public List<String> getValues() throws RepositoryException { List<String> values = new ArrayList<>(); if (value instanceof Value) { - values.add(((Value)value).getString()); + values.add(((Value) value).getString()); } else if (value instanceof Value[]) { - Value[] va = (Value[])value; + Value[] va = (Value[]) value; for (Value v : va) { values.add(v.getString()); } } else if (value instanceof String) { - values.add((String)value); + values.add((String) value); } else if (value instanceof String[]) { - values.addAll(Arrays.asList((String[])value)); + values.addAll(Arrays.asList((String[]) value)); } if (values.isEmpty()) { - //empty string if no values yet + // empty string if no values yet values.add(""); } return values; @@ -119,5 +121,4 @@ public class RestrictionItem { builder.append("]"); return builder.toString(); } - } diff --git a/src/test/java/org/apache/sling/starter/access/models/AccessFormPageTest.java b/src/test/java/org/apache/sling/starter/access/models/AccessFormPageTest.java index 8adbaa9..c0f91f9 100644 --- a/src/test/java/org/apache/sling/starter/access/models/AccessFormPageTest.java +++ b/src/test/java/org/apache/sling/starter/access/models/AccessFormPageTest.java @@ -1,26 +1,23 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file 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 + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * 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 + * 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 CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * 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 CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.apache.sling.starter.access.models; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - import javax.jcr.Node; import javax.jcr.RepositoryException; @@ -34,10 +31,16 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + abstract class AccessFormPageTest { protected AccessFormPage page; protected Node currentNode; protected ResourceResolver rr; + protected abstract AccessFormPage createPageModel(); @BeforeEach @@ -102,5 +105,4 @@ abstract class AccessFormPageTest { Mockito.when(rr.map(page.resource.getPath())).thenReturn("/test1"); assertEquals("/test1", page.getLocation()); } - } diff --git a/src/test/java/org/apache/sling/starter/access/models/AceTest.java b/src/test/java/org/apache/sling/starter/access/models/AceTest.java index f5311c5..afe72f5 100644 --- a/src/test/java/org/apache/sling/starter/access/models/AceTest.java +++ b/src/test/java/org/apache/sling/starter/access/models/AceTest.java @@ -1,28 +1,28 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file 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 + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * 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 + * 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 CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * 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 CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.apache.sling.starter.access.models; -import static org.apache.sling.starter.access.models.AceUtilsTest.createMockPrivilege; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import javax.jcr.RepositoryException; +import javax.jcr.Session; +import javax.jcr.Workspace; +import javax.jcr.security.AccessControlManager; +import javax.jcr.security.Privilege; import java.io.StringReader; import java.util.ArrayList; @@ -36,17 +36,11 @@ import java.util.Map; import java.util.Set; import java.util.stream.Stream; -import javax.jcr.RepositoryException; -import javax.jcr.Session; -import javax.jcr.Workspace; -import javax.jcr.security.AccessControlManager; -import javax.jcr.security.Privilege; import jakarta.json.Json; import jakarta.json.JsonArray; import jakarta.json.JsonObject; import jakarta.json.JsonReader; import jakarta.json.JsonValue; - import org.apache.jackrabbit.api.JackrabbitSession; import org.apache.jackrabbit.api.JackrabbitWorkspace; import org.apache.jackrabbit.api.security.authorization.PrivilegeManager; @@ -71,6 +65,14 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; +import static org.apache.sling.starter.access.models.AceUtilsTest.createMockPrivilege; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + class AceTest extends AccessFormPageTest { private static final String ACE_JSON = "{\n" + " \"principal\":\"testUser1\",\n" @@ -139,7 +141,8 @@ class AceTest extends AccessFormPageTest { acePage.getAce = Mockito.mock(GetAce.class); jcrSession = Mockito.mock(JackrabbitSession.class); - Mockito.when(acePage.request.getResourceResolver().adaptTo(Session.class)).thenReturn(jcrSession); + Mockito.when(acePage.request.getResourceResolver().adaptTo(Session.class)) + .thenReturn(jcrSession); mockPrivilegeLookup(); mockAuthorizableLookup(); @@ -150,7 +153,7 @@ class AceTest extends AccessFormPageTest { protected void mockPrincipalManager() throws RepositoryException { // mock principalMgr PrincipalManager principalMgr = Mockito.mock(PrincipalManager.class); - Mockito.when(((JackrabbitSession)jcrSession).getPrincipalManager()).thenReturn(principalMgr); + Mockito.when(((JackrabbitSession) jcrSession).getPrincipalManager()).thenReturn(principalMgr); Mockito.when(principalMgr.getPrincipal("testUser1")).thenReturn(() -> "testUser1"); Mockito.when(principalMgr.getPrincipal("testGroup1")).thenReturn(new TestGroupPrincipal("testGroup1")); } @@ -162,14 +165,15 @@ class AceTest extends AccessFormPageTest { repGlobDef = new RestrictionDefinitionImpl(AccessControlConstants.REP_GLOB, Type.STRING, isManditory); repNtNamesDef = new RestrictionDefinitionImpl(AccessControlConstants.REP_NT_NAMES, Type.STRINGS, isManditory); Mockito.when(restrictionProvider.getSupportedRestrictions(acePage.resource.getPath())) - .thenReturn(new HashSet<>(Arrays.asList(repGlobDef, repNtNamesDef))); + .thenReturn(new HashSet<>(Arrays.asList(repGlobDef, repNtNamesDef))); } protected void mockAuthorizableLookup() throws RepositoryException { // mock the authorizable lookup path UserManager userMgr = Mockito.mock(UserManager.class); - Mockito.when(acePage.request.getResourceResolver().adaptTo(Session.class)).thenReturn(jcrSession); - Mockito.when(((JackrabbitSession)jcrSession).getUserManager()).thenReturn(userMgr); + Mockito.when(acePage.request.getResourceResolver().adaptTo(Session.class)) + .thenReturn(jcrSession); + Mockito.when(((JackrabbitSession) jcrSession).getUserManager()).thenReturn(userMgr); User userAuthorizable = Mockito.mock(User.class); Mockito.when(userMgr.getAuthorizable("testUser1")).thenReturn(userAuthorizable); } @@ -181,29 +185,19 @@ class AceTest extends AccessFormPageTest { Privilege repReadNodes = createMockPrivilege(PrivilegeConstants.REP_READ_NODES, new Privilege[0]); Privilege repReadProperties = createMockPrivilege(PrivilegeConstants.REP_READ_PROPERTIES, new Privilege[0]); - jcrRead = createMockPrivilege(PrivilegeConstants.JCR_READ, new Privilege[] { - repReadNodes, - repReadProperties - }); + jcrRead = createMockPrivilege(PrivilegeConstants.JCR_READ, new Privilege[] {repReadNodes, repReadProperties}); jcrWrite = createMockPrivilege(PrivilegeConstants.JCR_WRITE, new Privilege[0]); - Privilege all = createMockPrivilege(PrivilegeConstants.JCR_ALL, new Privilege[] { - jcrRead, - jcrWrite - }); + Privilege all = createMockPrivilege(PrivilegeConstants.JCR_ALL, new Privilege[] {jcrRead, jcrWrite}); Mockito.when(acm.privilegeFromName(PrivilegeConstants.JCR_ALL)).thenReturn(all); Mockito.when(acm.privilegeFromName(PrivilegeConstants.REP_READ_NODES)).thenReturn(repReadNodes); - Mockito.when(acm.privilegeFromName(PrivilegeConstants.REP_READ_PROPERTIES)).thenReturn(repReadProperties); + Mockito.when(acm.privilegeFromName(PrivilegeConstants.REP_READ_PROPERTIES)) + .thenReturn(repReadProperties); Mockito.when(acm.privilegeFromName(PrivilegeConstants.JCR_READ)).thenReturn(jcrRead); Mockito.when(acm.privilegeFromName(PrivilegeConstants.JCR_WRITE)).thenReturn(jcrWrite); - Mockito.when(acm.getSupportedPrivileges(acePage.getAcePath())).thenReturn(new Privilege[] { - all, - jcrRead, - repReadNodes, - repReadNodes, - jcrWrite - }); + Mockito.when(acm.getSupportedPrivileges(acePage.getAcePath())) + .thenReturn(new Privilege[] {all, jcrRead, repReadNodes, repReadNodes, jcrWrite}); } /** @@ -235,7 +229,7 @@ class AceTest extends AccessFormPageTest { /** * Test method for {@link org.apache.sling.starter.access.models.Ace#getIsInvalidPrincipal()}. - * @throws RepositoryException + * @throws RepositoryException */ @Test void testGetIsInvalidPrincipal() throws RepositoryException { @@ -254,7 +248,7 @@ class AceTest extends AccessFormPageTest { /** * Test method for {@link org.apache.sling.starter.access.models.Ace#isExists()}. - * @throws RepositoryException + * @throws RepositoryException */ @Test void testIsExists() throws RepositoryException { @@ -268,7 +262,7 @@ class AceTest extends AccessFormPageTest { /** * Test method for {@link org.apache.sling.starter.access.models.Ace#getPrivileges()}. - * @throws RepositoryException + * @throws RepositoryException */ @Test void testGetPrivileges() throws RepositoryException { @@ -290,7 +284,7 @@ class AceTest extends AccessFormPageTest { aceJson = jsonReader.readObject(); } Mockito.when(acePage.getAce.getAce(jcrSession, acePage.resource.getPath(), "testUser1")) - .thenReturn(aceJson); + .thenReturn(aceJson); Map<String, String[]> rawReqParams = new HashMap<>(); RequestParameterMap requestParameterMap = new RequestParameterMapImpl(rawReqParams); @@ -313,14 +307,17 @@ class AceTest extends AccessFormPageTest { RequestParameterMap requestParameterMap = new RequestParameterMapImpl(rawReqParams); Mockito.when(acePage.request.getRequestParameterMap()).thenReturn(requestParameterMap); - Map<String, List<RestrictionItem>> postedAllowRestrictionsMap = new HashMap<>(); - Map<String, List<RestrictionItem>> postedDenyRestrictionsMap = new HashMap<>(); - Map<String, String[]> entriesMap = acePage.populateEntriesFromPreviousFailedPost(postedAllowRestrictionsMap, - postedDenyRestrictionsMap, acePage.toSrMap(acePage.getSupportedRestrictions())); + Map<String, List<RestrictionItem>> postedAllowRestrictionsMap = new HashMap<>(); + Map<String, List<RestrictionItem>> postedDenyRestrictionsMap = new HashMap<>(); + Map<String, String[]> entriesMap = acePage.populateEntriesFromPreviousFailedPost( + postedAllowRestrictionsMap, + postedDenyRestrictionsMap, + acePage.toSrMap(acePage.getSupportedRestrictions())); assertNotNull(entriesMap); assertEquals(2, entriesMap.size()); assertArrayEquals(new String[] {"glob1"}, entriesMap.get("restriction@jcr:read@rep:glob@Allow")); - assertArrayEquals(new String[] {"ntNames1", "ntNames2"}, entriesMap.get("restriction@jcr:write@rep:ntNames@Deny")); + assertArrayEquals( + new String[] {"ntNames1", "ntNames2"}, entriesMap.get("restriction@jcr:write@rep:ntNames@Deny")); } /** @@ -355,14 +352,15 @@ class AceTest extends AccessFormPageTest { /** * Test method for {@link org.apache.sling.starter.access.models.Ace#getSupportedOrRegisteredPrivileges(javax.jcr.Session, java.lang.String)}. - * @throws RepositoryException + * @throws RepositoryException */ @Test void testGetSupportedOrRegisteredPrivileges() throws RepositoryException { // mock that the resource does exist Mockito.when(jcrSession.nodeExists(acePage.getAcePath())).thenReturn(true); @NotNull - Privilege[] supportedOrRegisteredPrivileges = acePage.getSupportedOrRegisteredPrivileges(jcrSession, acePage.getAcePath()); + Privilege[] supportedOrRegisteredPrivileges = + acePage.getSupportedOrRegisteredPrivileges(jcrSession, acePage.getAcePath()); assertNotNull(supportedOrRegisteredPrivileges); assertEquals(5, supportedOrRegisteredPrivileges.length); assertTrue(Stream.of(supportedOrRegisteredPrivileges).anyMatch(p -> jcrRead.equals(p))); @@ -373,11 +371,8 @@ class AceTest extends AccessFormPageTest { Workspace workspace = Mockito.mock(JackrabbitWorkspace.class); Mockito.when(jcrSession.getWorkspace()).thenReturn(workspace); PrivilegeManager privilegeManager = Mockito.mock(PrivilegeManager.class); - Mockito.when(((JackrabbitWorkspace)workspace).getPrivilegeManager()).thenReturn(privilegeManager); - Mockito.when(privilegeManager.getRegisteredPrivileges()).thenReturn(new Privilege[] { - jcrRead, - jcrWrite - }); + Mockito.when(((JackrabbitWorkspace) workspace).getPrivilegeManager()).thenReturn(privilegeManager); + Mockito.when(privilegeManager.getRegisteredPrivileges()).thenReturn(new Privilege[] {jcrRead, jcrWrite}); supportedOrRegisteredPrivileges = acePage.getSupportedOrRegisteredPrivileges(jcrSession, Ace.PATH_REPOSITORY); assertNotNull(supportedOrRegisteredPrivileges); assertEquals(2, supportedOrRegisteredPrivileges.length); @@ -387,14 +382,15 @@ class AceTest extends AccessFormPageTest { /** * Test method for {@link org.apache.sling.starter.access.models.Ace#initialPrivilegesMap(java.util.Map, java.lang.String)}. - * @throws RepositoryException + * @throws RepositoryException */ @Test void testInitialPrivilegesMap() throws RepositoryException { Map<Privilege, String> privilegeLongestPathMap = AceUtils.getPrivilegeLongestPathMap(jcrSession); // mock that the resource does exist Mockito.when(jcrSession.nodeExists(acePage.getAcePath())).thenReturn(true); - Map<Privilege, PrivilegeItem> initialPrivilegesMap = acePage.initialPrivilegesMap(privilegeLongestPathMap, acePage.getAcePath()); + Map<Privilege, PrivilegeItem> initialPrivilegesMap = + acePage.initialPrivilegesMap(privilegeLongestPathMap, acePage.getAcePath()); assertNotNull(initialPrivilegesMap); assertEquals(4, initialPrivilegesMap.size()); assertTrue(initialPrivilegesMap.containsKey(jcrRead)); @@ -403,7 +399,7 @@ class AceTest extends AccessFormPageTest { /** * Test method for {@link org.apache.sling.starter.access.models.Ace#getPersistedPrivilegesMap()}. - * @throws RepositoryException + * @throws RepositoryException */ @Test void testGetPersistedPrivilegesMap() throws RepositoryException { @@ -415,7 +411,7 @@ class AceTest extends AccessFormPageTest { aceJson = jsonReader.readObject(); } Mockito.when(acePage.getAce.getAce(jcrSession, acePage.resource.getPath(), "testUser1")) - .thenReturn(aceJson); + .thenReturn(aceJson); Map<Privilege, PrivilegeItem> persistedPrivilegesMap = acePage.getPersistedPrivilegesMap(); assertNotNull(persistedPrivilegesMap); @@ -434,11 +430,14 @@ class AceTest extends AccessFormPageTest { JsonReader jsonReader = Json.createReader(strReader)) { restrictionsJson = jsonReader.readObject(); } - List<RestrictionItem> jsonToRestrictionItems = acePage.jsonToRestrictionItems(acePage.toSrMap(acePage.getSupportedRestrictions()), restrictionsJson); + List<RestrictionItem> jsonToRestrictionItems = + acePage.jsonToRestrictionItems(acePage.toSrMap(acePage.getSupportedRestrictions()), restrictionsJson); assertNotNull(jsonToRestrictionItems); assertEquals(2, jsonToRestrictionItems.size()); - assertTrue(jsonToRestrictionItems.stream().anyMatch(p -> repGlobDef.getName().equals(p.getName()))); - assertTrue(jsonToRestrictionItems.stream().anyMatch(p -> repNtNamesDef.getName().equals(p.getName()))); + assertTrue(jsonToRestrictionItems.stream() + .anyMatch(p -> repGlobDef.getName().equals(p.getName()))); + assertTrue(jsonToRestrictionItems.stream() + .anyMatch(p -> repNtNamesDef.getName().equals(p.getName()))); } /** @@ -467,7 +466,7 @@ class AceTest extends AccessFormPageTest { /** * Test method for {@link org.apache.sling.starter.access.models.Ace#getPrivilegeAggregationsAsJSON()}. - * @throws RepositoryException + * @throws RepositoryException */ @Test void testGetPrivilegeAggregationsAsJSON() throws RepositoryException { @@ -487,7 +486,7 @@ class AceTest extends AccessFormPageTest { /** * Test method for {@link org.apache.sling.starter.access.models.Ace#getExistingRestrictionNamesAsJSON()}. - * @throws RepositoryException + * @throws RepositoryException */ @Test void testGetExistingRestrictionNamesAsJSON() throws RepositoryException { @@ -503,10 +502,10 @@ class AceTest extends AccessFormPageTest { assertNotNull(restrictionNamesJson); JsonValue allow = restrictionNamesJson.get("allow"); assertTrue(allow instanceof JsonArray); - assertEquals(0, ((JsonArray)allow).size()); + assertEquals(0, ((JsonArray) allow).size()); JsonValue deny = restrictionNamesJson.get("deny"); assertTrue(deny instanceof JsonArray); - assertEquals(0, ((JsonArray)deny).size()); + assertEquals(0, ((JsonArray) deny).size()); // try again with existing restrictions mockPersistedPrivileges(ACE_JSON_WITH_RESTRICTIONS); @@ -519,12 +518,12 @@ class AceTest extends AccessFormPageTest { assertNotNull(restrictionNamesJson); allow = restrictionNamesJson.get("allow"); assertTrue(allow instanceof JsonArray); - assertEquals(1, ((JsonArray)allow).size()); - assertEquals("jcr:read@rep:ntNames", ((JsonArray)allow).getString(0)); + assertEquals(1, ((JsonArray) allow).size()); + assertEquals("jcr:read@rep:ntNames", ((JsonArray) allow).getString(0)); deny = restrictionNamesJson.get("deny"); assertTrue(deny instanceof JsonArray); - assertEquals(1, ((JsonArray)deny).size()); - assertEquals("jcr:write@rep:glob", ((JsonArray)deny).getString(0)); + assertEquals(1, ((JsonArray) deny).size()); + assertEquals("jcr:write@rep:glob", ((JsonArray) deny).getString(0)); } /** @@ -539,7 +538,7 @@ class AceTest extends AccessFormPageTest { /** * Test method for {@link org.apache.sling.starter.access.models.Ace#getOrderList()}. - * @throws RepositoryException + * @throws RepositoryException */ @Test void testGetOrderList() throws RepositoryException { @@ -549,7 +548,7 @@ class AceTest extends AccessFormPageTest { aclJson = jsonReader.readObject(); } Mockito.when(acePage.getAcl.getAcl(jcrSession, acePage.resource.getPath())) - .thenReturn(aclJson); + .thenReturn(aclJson); Collection<PrincipalPrivilege> orderList = acePage.getOrderList(); assertNotNull(orderList); @@ -564,12 +563,10 @@ class AceTest extends AccessFormPageTest { */ @Test void testFieldValuesFromReqParams() { - RequestParameter [] paramValues = new RequestParameter[] { - new RequestParameterImpl("key1", "value1"), - new RequestParameterImpl("key1", "value2"), + RequestParameter[] paramValues = new RequestParameter[] { + new RequestParameterImpl("key1", "value1"), new RequestParameterImpl("key1", "value2"), }; - assertArrayEquals(new String[] {"value1", "value2"}, - acePage.fieldValuesFromReqParams(paramValues)); + assertArrayEquals(new String[] {"value1", "value2"}, acePage.fieldValuesFromReqParams(paramValues)); } /** @@ -588,7 +585,8 @@ class AceTest extends AccessFormPageTest { assertNotNull(fieldValuesForPattern); assertEquals(2, fieldValuesForPattern.size()); assertArrayEquals(new String[] {"glob1"}, fieldValuesForPattern.get("restriction@jcr:read@rep:glob@Allow")); - assertArrayEquals(new String[] {"ntNames1", "ntNames2"}, fieldValuesForPattern.get("restriction@jcr:write@rep:ntNames@Deny")); + assertArrayEquals( + new String[] {"ntNames1", "ntNames2"}, + fieldValuesForPattern.get("restriction@jcr:write@rep:ntNames@Deny")); } - } diff --git a/src/test/java/org/apache/sling/starter/access/models/AceUtilsTest.java b/src/test/java/org/apache/sling/starter/access/models/AceUtilsTest.java index 64712d9..59cd110 100644 --- a/src/test/java/org/apache/sling/starter/access/models/AceUtilsTest.java +++ b/src/test/java/org/apache/sling/starter/access/models/AceUtilsTest.java @@ -1,35 +1,37 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file 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 + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * 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 + * 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 CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * 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 CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.apache.sling.starter.access.models; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import java.util.Map; - import javax.jcr.RepositoryException; import javax.jcr.Session; import javax.jcr.security.AccessControlManager; import javax.jcr.security.Privilege; +import java.util.Map; + import org.apache.jackrabbit.oak.spi.security.privilege.PrivilegeConstants; import org.junit.jupiter.api.Test; import org.mockito.Mockito; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + class AceUtilsTest { /** @@ -43,28 +45,35 @@ class AceUtilsTest { Privilege repReadNodes = createMockPrivilege(PrivilegeConstants.REP_READ_NODES, new Privilege[0]); Privilege repReadProperties = createMockPrivilege(PrivilegeConstants.REP_READ_PROPERTIES, new Privilege[0]); - Privilege jcrRead = createMockPrivilege(PrivilegeConstants.JCR_READ, new Privilege[] { - repReadNodes, - repReadProperties - }); - Privilege jcrReadAccessControl = createMockPrivilege(PrivilegeConstants.JCR_READ_ACCESS_CONTROL, new Privilege[0]); + Privilege jcrRead = + createMockPrivilege(PrivilegeConstants.JCR_READ, new Privilege[] {repReadNodes, repReadProperties}); + Privilege jcrReadAccessControl = + createMockPrivilege(PrivilegeConstants.JCR_READ_ACCESS_CONTROL, new Privilege[0]); - Privilege all = createMockPrivilege(PrivilegeConstants.JCR_ALL, new Privilege[] { - jcrRead, - jcrReadAccessControl - }); + Privilege all = + createMockPrivilege(PrivilegeConstants.JCR_ALL, new Privilege[] {jcrRead, jcrReadAccessControl}); Mockito.when(acm.privilegeFromName(PrivilegeConstants.JCR_ALL)).thenReturn(all); Map<Privilege, String> privilegeLongestPathMap = AceUtils.getPrivilegeLongestPathMap(session); assertNotNull(privilegeLongestPathMap); assertEquals(PrivilegeConstants.JCR_ALL, privilegeLongestPathMap.get(all)); - assertEquals(String.format("%s/%s", PrivilegeConstants.JCR_ALL, PrivilegeConstants.JCR_READ), + assertEquals( + String.format("%s/%s", PrivilegeConstants.JCR_ALL, PrivilegeConstants.JCR_READ), privilegeLongestPathMap.get(jcrRead)); - assertEquals(String.format("%s/%s/%s", PrivilegeConstants.JCR_ALL, PrivilegeConstants.JCR_READ, PrivilegeConstants.REP_READ_NODES), + assertEquals( + String.format( + "%s/%s/%s", + PrivilegeConstants.JCR_ALL, PrivilegeConstants.JCR_READ, PrivilegeConstants.REP_READ_NODES), privilegeLongestPathMap.get(repReadNodes)); - assertEquals(String.format("%s/%s/%s", PrivilegeConstants.JCR_ALL, PrivilegeConstants.JCR_READ, PrivilegeConstants.REP_READ_PROPERTIES), + assertEquals( + String.format( + "%s/%s/%s", + PrivilegeConstants.JCR_ALL, + PrivilegeConstants.JCR_READ, + PrivilegeConstants.REP_READ_PROPERTIES), privilegeLongestPathMap.get(repReadProperties)); - assertEquals(String.format("%s/%s", PrivilegeConstants.JCR_ALL, PrivilegeConstants.JCR_READ_ACCESS_CONTROL), + assertEquals( + String.format("%s/%s", PrivilegeConstants.JCR_ALL, PrivilegeConstants.JCR_READ_ACCESS_CONTROL), privilegeLongestPathMap.get(jcrReadAccessControl)); } @@ -78,5 +87,4 @@ class AceUtilsTest { } return p; } - } diff --git a/src/test/java/org/apache/sling/starter/access/models/AclTest.java b/src/test/java/org/apache/sling/starter/access/models/AclTest.java index 891e5ef..77cef6d 100644 --- a/src/test/java/org/apache/sling/starter/access/models/AclTest.java +++ b/src/test/java/org/apache/sling/starter/access/models/AclTest.java @@ -1,37 +1,34 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file 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 + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * 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 + * 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 CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * 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 CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.apache.sling.starter.access.models; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import javax.jcr.RepositoryException; +import javax.jcr.Session; import java.io.StringReader; import java.util.Collection; import java.util.HashMap; import java.util.Map; -import javax.jcr.RepositoryException; -import javax.jcr.Session; import jakarta.json.Json; import jakarta.json.JsonObject; import jakarta.json.JsonReader; - import org.apache.jackrabbit.api.JackrabbitSession; import org.apache.jackrabbit.api.security.principal.PrincipalManager; import org.apache.sling.jcr.jackrabbit.accessmanager.GetAcl; @@ -39,6 +36,11 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + class AclTest extends AccessFormPageTest { static final String ACL_JSON = "{\n" + " \"testUser1\":{\n" @@ -84,7 +86,8 @@ class AclTest extends AccessFormPageTest { aclPage.getAcl = Mockito.mock(GetAcl.class); jcrSession = Mockito.mock(JackrabbitSession.class); - Mockito.when(aclPage.request.getResourceResolver().adaptTo(Session.class)).thenReturn(jcrSession); + Mockito.when(aclPage.request.getResourceResolver().adaptTo(Session.class)) + .thenReturn(jcrSession); } /** @@ -93,7 +96,7 @@ class AclTest extends AccessFormPageTest { @Test void testGetPrincipals() throws RepositoryException { PrincipalManager principalMgr = Mockito.mock(PrincipalManager.class); - Mockito.when(((JackrabbitSession)jcrSession).getPrincipalManager()).thenReturn(principalMgr); + Mockito.when(((JackrabbitSession) jcrSession).getPrincipalManager()).thenReturn(principalMgr); Mockito.when(principalMgr.getPrincipal("testUser1")).thenReturn(() -> "testUser1"); Mockito.when(principalMgr.getPrincipal("testGroup1")).thenReturn(new TestGroupPrincipal("testGroup1")); @@ -103,7 +106,7 @@ class AclTest extends AccessFormPageTest { aclJson = jsonReader.readObject(); } Mockito.when(aclPage.getAcl.getAcl(jcrSession, aclPage.resource.getPath())) - .thenReturn(aclJson); + .thenReturn(aclJson); Collection<PrincipalPrivilege> principals = aclPage.getPrincipals(); assertNotNull(principals); @@ -125,5 +128,4 @@ class AclTest extends AccessFormPageTest { assertTrue(candidateGroup.isDeny()); assertTrue(candidateGroup.getIsGroup()); } - } diff --git a/src/test/java/org/apache/sling/starter/access/models/PrincipalPrivilegeTest.java b/src/test/java/org/apache/sling/starter/access/models/PrincipalPrivilegeTest.java index 5680556..d713cf6 100644 --- a/src/test/java/org/apache/sling/starter/access/models/PrincipalPrivilegeTest.java +++ b/src/test/java/org/apache/sling/starter/access/models/PrincipalPrivilegeTest.java @@ -1,28 +1,30 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file 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 + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * 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 + * 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 CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * 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 CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.apache.sling.starter.access.models; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - class PrincipalPrivilegeTest { private PrincipalPrivilege pp; @@ -106,5 +108,4 @@ class PrincipalPrivilegeTest { PrincipalPrivilege gp = new PrincipalPrivilege(new TestGroupPrincipal("testGroup1")); assertTrue(gp.getIsGroup()); } - } diff --git a/src/test/java/org/apache/sling/starter/access/models/PrivilegeItemTest.java b/src/test/java/org/apache/sling/starter/access/models/PrivilegeItemTest.java index 83974e3..194b9eb 100644 --- a/src/test/java/org/apache/sling/starter/access/models/PrivilegeItemTest.java +++ b/src/test/java/org/apache/sling/starter/access/models/PrivilegeItemTest.java @@ -1,26 +1,23 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file 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 + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * 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 + * 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 CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * 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 CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.apache.sling.starter.access.models; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - import java.util.Collections; import java.util.List; @@ -31,6 +28,11 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + class PrivilegeItemTest { private PrivilegeItem jcrReadPi; private PrivilegeItem jcrReadNodesPiWithRestrictions; @@ -43,16 +45,25 @@ class PrivilegeItemTest { @BeforeEach void beforeEach() { - jcrReadPi = new PrivilegeItem(PrivilegeConstants.JCR_READ, false, false, + jcrReadPi = new PrivilegeItem( + PrivilegeConstants.JCR_READ, + false, + false, String.format("%s/%s", PrivilegeConstants.JCR_ALL, PrivilegeConstants.JCR_READ)); - jcrReadNodesPiWithRestrictions = new PrivilegeItem(PrivilegeConstants.REP_READ_NODES, false, false, - String.format("%s/%s/%s", PrivilegeConstants.JCR_ALL, PrivilegeConstants.JCR_READ, PrivilegeConstants.REP_READ_NODES), + jcrReadNodesPiWithRestrictions = new PrivilegeItem( + PrivilegeConstants.REP_READ_NODES, + false, + false, + String.format( + "%s/%s/%s", + PrivilegeConstants.JCR_ALL, PrivilegeConstants.JCR_READ, PrivilegeConstants.REP_READ_NODES), // allowRestrictions - Collections.singletonList(createSingleValueRestrictionItem(AccessControlConstants.REP_GLOB, "glob1", true)), + Collections.singletonList( + createSingleValueRestrictionItem(AccessControlConstants.REP_GLOB, "glob1", true)), // denyRestrictions - Collections.singletonList(createSingleValueRestrictionItem(AccessControlConstants.REP_ITEM_NAMES, - new String[] {"name1", "name2"}, true))); + Collections.singletonList(createSingleValueRestrictionItem( + AccessControlConstants.REP_ITEM_NAMES, new String[] {"name1", "name2"}, true))); } /** @@ -162,9 +173,13 @@ class PrivilegeItemTest { */ @Test void testGetLongestPath() { - assertEquals(String.format("%s/%s", PrivilegeConstants.JCR_ALL, PrivilegeConstants.JCR_READ), + assertEquals( + String.format("%s/%s", PrivilegeConstants.JCR_ALL, PrivilegeConstants.JCR_READ), jcrReadPi.getLongestPath()); - assertEquals(String.format("%s/%s/%s", PrivilegeConstants.JCR_ALL, PrivilegeConstants.JCR_READ, PrivilegeConstants.REP_READ_NODES), + assertEquals( + String.format( + "%s/%s/%s", + PrivilegeConstants.JCR_ALL, PrivilegeConstants.JCR_READ, PrivilegeConstants.REP_READ_NODES), jcrReadNodesPiWithRestrictions.getLongestPath()); } @@ -217,7 +232,8 @@ class PrivilegeItemTest { List<RestrictionItem> denyRestrictions = jcrReadNodesPiWithRestrictions.getDenyRestrictions(); assertEquals(1, denyRestrictions.size()); - assertEquals(AccessControlConstants.REP_ITEM_NAMES, denyRestrictions.get(0).getName()); + assertEquals( + AccessControlConstants.REP_ITEM_NAMES, denyRestrictions.get(0).getName()); } /** @@ -226,7 +242,8 @@ class PrivilegeItemTest { @Test void testSetAllowRestrictions() { assertEquals(Collections.emptyList(), jcrReadPi.getAllowRestrictions()); - List<RestrictionItem> newRestrictions = Collections.singletonList(createSingleValueRestrictionItem(AccessControlConstants.REP_GLOB, "glob1", false)); + List<RestrictionItem> newRestrictions = Collections.singletonList( + createSingleValueRestrictionItem(AccessControlConstants.REP_GLOB, "glob1", false)); jcrReadPi.setAllowRestrictions(newRestrictions); assertEquals(newRestrictions, jcrReadPi.getAllowRestrictions()); } @@ -237,7 +254,8 @@ class PrivilegeItemTest { @Test void testSetDenyRestrictions() { assertEquals(Collections.emptyList(), jcrReadPi.getDenyRestrictions()); - List<RestrictionItem> newRestrictions = Collections.singletonList(createSingleValueRestrictionItem(AccessControlConstants.REP_GLOB, "glob1", false)); + List<RestrictionItem> newRestrictions = Collections.singletonList( + createSingleValueRestrictionItem(AccessControlConstants.REP_GLOB, "glob1", false)); jcrReadPi.setDenyRestrictions(newRestrictions); assertEquals(newRestrictions, jcrReadPi.getDenyRestrictions()); } @@ -257,7 +275,8 @@ class PrivilegeItemTest { @Test void testAddAllowRestrictionToDelete() { jcrReadPi.addAllowRestrictionToDelete(AccessControlConstants.REP_ITEM_NAMES); - assertEquals(Collections.singleton(AccessControlConstants.REP_ITEM_NAMES), jcrReadPi.getAllowRestrictionsToDelete()); + assertEquals( + Collections.singleton(AccessControlConstants.REP_ITEM_NAMES), jcrReadPi.getAllowRestrictionsToDelete()); } /** @@ -276,7 +295,8 @@ class PrivilegeItemTest { @Test void testAddDenyRestrictionToDelete() { jcrReadPi.addDenyRestrictionToDelete(AccessControlConstants.REP_NT_NAMES); - assertEquals(Collections.singleton(AccessControlConstants.REP_NT_NAMES), jcrReadPi.getDenyRestrictionsToDelete()); + assertEquals( + Collections.singleton(AccessControlConstants.REP_NT_NAMES), jcrReadPi.getDenyRestrictionsToDelete()); } /** @@ -286,7 +306,7 @@ class PrivilegeItemTest { void testGetDenyRestrictionsToDelete() { assertEquals(Collections.emptySet(), jcrReadPi.getDenyRestrictionsToDelete()); jcrReadPi.addDenyRestrictionToDelete(AccessControlConstants.REP_PREFIXES); - assertEquals(Collections.singleton(AccessControlConstants.REP_PREFIXES), jcrReadPi.getDenyRestrictionsToDelete()); + assertEquals( + Collections.singleton(AccessControlConstants.REP_PREFIXES), jcrReadPi.getDenyRestrictionsToDelete()); } - } diff --git a/src/test/java/org/apache/sling/starter/access/models/RestrictionDefinitionInfoTest.java b/src/test/java/org/apache/sling/starter/access/models/RestrictionDefinitionInfoTest.java index 457c289..8ac9cfb 100644 --- a/src/test/java/org/apache/sling/starter/access/models/RestrictionDefinitionInfoTest.java +++ b/src/test/java/org/apache/sling/starter/access/models/RestrictionDefinitionInfoTest.java @@ -1,23 +1,23 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file 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 + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * 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 + * 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 CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * 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 CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.apache.sling.starter.access.models; -import static org.junit.jupiter.api.Assertions.*; - import org.apache.jackrabbit.oak.api.Type; import org.apache.jackrabbit.oak.spi.security.authorization.accesscontrol.AccessControlConstants; import org.apache.jackrabbit.oak.spi.security.authorization.restriction.RestrictionDefinition; @@ -27,6 +27,8 @@ import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; +import static org.junit.jupiter.api.Assertions.*; + class RestrictionDefinitionInfoTest { private RestrictionDefinitionInfo rdInfo; @@ -82,8 +84,8 @@ class RestrictionDefinitionInfoTest { // also try a manditory one RestrictionDefinition manditoryRd = createMockRestrictionDefintion(); Mockito.when(manditoryRd.isMandatory()).thenReturn(true); - RestrictionDefinitionInfo manditoryRdInfo = new RestrictionDefinitionInfo(AccessControlConstants.REP_ITEM_NAMES + " DisplayName", manditoryRd); + RestrictionDefinitionInfo manditoryRdInfo = + new RestrictionDefinitionInfo(AccessControlConstants.REP_ITEM_NAMES + " DisplayName", manditoryRd); assertTrue(manditoryRdInfo.isMandatory()); } - } diff --git a/src/test/java/org/apache/sling/starter/access/models/RestrictionItemTest.java b/src/test/java/org/apache/sling/starter/access/models/RestrictionItemTest.java index 4e7b83d..f06c99f 100644 --- a/src/test/java/org/apache/sling/starter/access/models/RestrictionItemTest.java +++ b/src/test/java/org/apache/sling/starter/access/models/RestrictionItemTest.java @@ -1,33 +1,30 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file 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 + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * 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 + * 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 CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * 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 CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.apache.sling.starter.access.models; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.util.Arrays; -import java.util.Collections; - import javax.jcr.RepositoryException; import javax.jcr.Value; import javax.jcr.ValueFactory; +import java.util.Arrays; +import java.util.Collections; + import org.apache.jackrabbit.oak.api.Type; import org.apache.jackrabbit.oak.spi.security.authorization.accesscontrol.AccessControlConstants; import org.apache.jackrabbit.oak.spi.security.authorization.restriction.RestrictionDefinition; @@ -38,6 +35,11 @@ import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + class RestrictionItemTest { private RestrictionItem itemNamesRi; @@ -45,11 +47,11 @@ class RestrictionItemTest { protected RestrictionItem createRestrictionItem(String name, Type<?> type, Object value) { return createRestrictionItem(name, type, value, false); } + protected RestrictionItem createRestrictionItem(String name, Type<?> type, Object value, boolean isManditory) { RestrictionDefinition globRd = Mockito.mock(RestrictionDefinition.class); Mockito.when(globRd.getName()).thenReturn(name); - Mockito.when(globRd.getRequiredType()) - .thenAnswer(new Answer<Type<?>>() { + Mockito.when(globRd.getRequiredType()).thenAnswer(new Answer<Type<?>>() { @Override public Type<?> answer(InvocationOnMock invocation) throws Throwable { return type; @@ -63,7 +65,8 @@ class RestrictionItemTest { @BeforeEach void beforeEach() { - itemNamesRi = createRestrictionItem(AccessControlConstants.REP_ITEM_NAMES, Type.STRINGS, new String[] {"name1", "name2"}); + itemNamesRi = createRestrictionItem( + AccessControlConstants.REP_ITEM_NAMES, Type.STRINGS, new String[] {"name1", "name2"}); } /** @@ -103,8 +106,9 @@ class RestrictionItemTest { assertEquals("glob1", globRi.getValue()); // also a Value[] instead of String[] - itemNamesRi = createRestrictionItem(AccessControlConstants.REP_ITEM_NAMES, Type.STRINGS, - new Value[] {vf.createValue("name1"), vf.createValue("name2")}); + itemNamesRi = createRestrictionItem(AccessControlConstants.REP_ITEM_NAMES, Type.STRINGS, new Value[] { + vf.createValue("name1"), vf.createValue("name2") + }); assertEquals("name1", itemNamesRi.getValue()); } @@ -125,8 +129,9 @@ class RestrictionItemTest { assertEquals(Collections.singletonList("glob1"), globRi.getValues()); // also a Value[] instead of String[] - itemNamesRi = createRestrictionItem(AccessControlConstants.REP_ITEM_NAMES, Type.STRINGS, - new Value[] {vf.createValue("name1"), vf.createValue("name2")}); + itemNamesRi = createRestrictionItem(AccessControlConstants.REP_ITEM_NAMES, Type.STRINGS, new Value[] { + vf.createValue("name1"), vf.createValue("name2") + }); assertEquals(Arrays.asList("name1", "name2"), itemNamesRi.getValues()); } @@ -170,5 +175,4 @@ class RestrictionItemTest { void testToString() { assertNotNull(itemNamesRi.toString()); } - } diff --git a/src/test/java/org/apache/sling/starter/access/models/TestGroupPrincipal.java b/src/test/java/org/apache/sling/starter/access/models/TestGroupPrincipal.java index 7930988..ff47b8f 100644 --- a/src/test/java/org/apache/sling/starter/access/models/TestGroupPrincipal.java +++ b/src/test/java/org/apache/sling/starter/access/models/TestGroupPrincipal.java @@ -1,18 +1,20 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file 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 + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * 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 + * 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 CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * 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 CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.apache.sling.starter.access.models; @@ -43,5 +45,4 @@ class TestGroupPrincipal implements GroupPrincipal { public Enumeration<? extends Principal> members() { return Collections.emptyEnumeration(); } - -} \ No newline at end of file +}
