From 050b8e07332f611bc92ca5d6bcc5d30e1a507d84 Mon Sep 17 00:00:00 2001 From: Mathias Brunkow Moser Date: Sat, 11 May 2024 18:25:13 +0200 Subject: [PATCH] fix(bug): fixed action type bug --- charts/digital-product-pass/values-int.yaml | 4 +- .../models/negotiation/policy/Action.java | 44 ++++++++++++++++--- .../src/main/java/utils/PolicyUtil.java | 15 ++++++- .../src/test/java/utils/PolicyUtilTest.java | 6 +-- 4 files changed, 57 insertions(+), 12 deletions(-) diff --git a/charts/digital-product-pass/values-int.yaml b/charts/digital-product-pass/values-int.yaml index c0b80b59b..3c8b23d22 100644 --- a/charts/digital-product-pass/values-int.yaml +++ b/charts/digital-product-pass/values-int.yaml @@ -23,11 +23,11 @@ ################################################################################# backend: + image: + pullPolicy: Always ingress: enabled: true # className: "nginx" - image: - pullPolicy: Always annotations: ingressClassName: nginx # kubernetes.io/tls-acme: "true" diff --git a/dpp-backend/digitalproductpass/src/main/java/org/eclipse/tractusx/digitalproductpass/models/negotiation/policy/Action.java b/dpp-backend/digitalproductpass/src/main/java/org/eclipse/tractusx/digitalproductpass/models/negotiation/policy/Action.java index 4d4fa5b7e..af19fddf9 100644 --- a/dpp-backend/digitalproductpass/src/main/java/org/eclipse/tractusx/digitalproductpass/models/negotiation/policy/Action.java +++ b/dpp-backend/digitalproductpass/src/main/java/org/eclipse/tractusx/digitalproductpass/models/negotiation/policy/Action.java @@ -45,7 +45,7 @@ public class Action { **/ @JsonProperty("odrl:action") @JsonAlias({"action", "odrl:action"}) - String action; + ActionType action; @JsonProperty("odrl:constraint") @JsonAlias({"constraint", "odrl:constraint"}) LogicalConstraint constraint; @@ -58,7 +58,7 @@ public class Action { public Action() { } - public Action(String action, LogicalConstraint constraint) { + public Action(ActionType action, LogicalConstraint constraint) { this.action = action; this.constraint = constraint; } @@ -75,17 +75,28 @@ public Action(PolicyCheckConfig.ActionConfig actionConfig) { */ public void buildAction(PolicyCheckConfig.ActionConfig actionConfig){ // Create clean list of constraints - this.action = actionConfig.getAction(); + addAction(actionConfig.getAction()); this.constraint = new LogicalConstraint(actionConfig); } /** * GETTERS AND SETTERS **/ - public String getAction() { + public String retrieveAction() { + if(this.action == null){ + return null; + } + return this.action.getType(); + } + public void addAction(String action) { + this.action = new ActionType(); + this.action.setType(action); + } + + public ActionType getAction() { return action; } - public void setAction(String action) { + public void setAction(ActionType action) { this.action = action; } @@ -107,10 +118,31 @@ public void setConstraint(LogicalConstraint constraint) { public Boolean compare(Action action){ try{ if(action == null){return false;} // If action is null not continue - if(!action.getAction().equalsIgnoreCase(this.getAction())){return false;} // If actions strings are not the same + if(!action.retrieveAction().equalsIgnoreCase(this.retrieveAction())){return false;} // If actions strings are not the same return action.getConstraint().compare(this.getConstraint()); //If constraints are the same }catch (Exception e){ throw new ModelException(this.getClass().getName(), e, "It was not possible to compare the actions!"); } } + + static class ActionType{ + @JsonProperty("odrl:type") + @JsonAlias({"type", "odrl:type", "@type"}) + String type; + + public ActionType(String type) { + this.type = type; + } + + public ActionType() { + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + } } \ No newline at end of file diff --git a/dpp-backend/digitalproductpass/src/main/java/utils/PolicyUtil.java b/dpp-backend/digitalproductpass/src/main/java/utils/PolicyUtil.java index 5ad67c1ba..d03c1eff1 100644 --- a/dpp-backend/digitalproductpass/src/main/java/utils/PolicyUtil.java +++ b/dpp-backend/digitalproductpass/src/main/java/utils/PolicyUtil.java @@ -145,7 +145,7 @@ public List getValidPoliciesByConstraints(Object policies, PolicyCheckConfi if (policies instanceof LinkedHashMap) { // Check if policy is valid or not - + LogUtil.printMessage("DEBUG: Single Policy!"); Set policy = this.parsePolicy(policies); LogUtil.printMessage("DEBUG: Policy: " + jsonUtil.toJson(policy, true)); @@ -158,6 +158,7 @@ public List getValidPoliciesByConstraints(Object policies, PolicyCheckConfi // If the policy is not valid return an empty list return new ArrayList<>(); } + LogUtil.printMessage("DEBUG: Multiple Policies!"); List policyList = this.parsePolicies(policies); LogUtil.printMessage("DEBUG: Policies: " + jsonUtil.toJson(policyList, true)); @@ -275,18 +276,28 @@ public Set parsePolicy(Object rawPolicy){ try { // Parse policy to json node JsonNode policy = jsonUtil.toJsonNode(rawPolicy); + LogUtil.printMessage("DEBUG: Json Node Policy: " + jsonUtil.toJson(policy, true)); // Get permission, prohibition and obligation JsonNode permission = policy.get("odrl:permission"); + LogUtil.printMessage("DEBUG: Permission Json Node: " + jsonUtil.toJson(permission, true)); + JsonNode prohibition = policy.get("odrl:prohibition"); + LogUtil.printMessage("DEBUG: prohibition Json Node: " + jsonUtil.toJson(prohibition, true)); + JsonNode obligation = policy.get("odrl:obligation"); + LogUtil.printMessage("DEBUG: prohibition Json Node: " + jsonUtil.toJson(obligation, true)); + // Check if its null if(permission == null || prohibition == null || obligation == null){ throw new UtilException(PolicyUtil.class, "One of the policy action constraints is empty!"); } // Check if all them are array then parse as default if(permission.isArray() && prohibition.isArray() && obligation.isArray()){ + LogUtil.printMessage("DEBUG: All is array "); + return jsonUtil.bind(rawPolicy, new TypeReference<>(){}); } + LogUtil.printMessage("DEBUG: One is not array "); // If not parse the set by action type return new Set(this.parseActions(permission), this.parseActions(prohibition), this.parseActions(obligation)); }catch (Exception e) { @@ -304,8 +315,10 @@ public Set parsePolicy(Object rawPolicy){ public List parseActions(JsonNode node){ // If node is not array parse a single action object if(!node.isArray()){ + LogUtil.printMessage("DEBUG: Node is not array " +jsonUtil.toJson(node, true)); return new ArrayList<>(){{add(jsonUtil.bind(node, new TypeReference<>(){}));}}; } + LogUtil.printMessage("DEBUG: Node is array " +jsonUtil.toJson(node, true)); // If node is array parse the action node as a list return jsonUtil.bind(node, new TypeReference<>(){}); } diff --git a/dpp-backend/digitalproductpass/src/test/java/utils/PolicyUtilTest.java b/dpp-backend/digitalproductpass/src/test/java/utils/PolicyUtilTest.java index b670d58f9..fff3992f9 100644 --- a/dpp-backend/digitalproductpass/src/test/java/utils/PolicyUtilTest.java +++ b/dpp-backend/digitalproductpass/src/test/java/utils/PolicyUtilTest.java @@ -173,13 +173,13 @@ void setUp() { correctLogicalConstraint.put("odrl:and", correctContraints); - action.put("odrl:action", "USE"); + action.put("odrl:action", new LinkedHashMap<>(){{put("odrl:type","USE");}}); action.put("odrl:constraint", logicalConstraint); - correctAction.put("odrl:action", "USE"); + correctAction.put("odrl:action", new LinkedHashMap<>(){{put("odrl:type","USE");}}); correctAction.put("odrl:constraint", correctLogicalConstraint); - actionDtr.put("odrl:action", "USE"); + actionDtr.put("odrl:action", new LinkedHashMap<>(){{put("odrl:type","USE");}}); actionDtr.put("odrl:constraint", logicalConstraintDtr); singlePermission = correctAction;