aboutsummaryrefslogtreecommitdiffstats
path: root/applications/guard
diff options
context:
space:
mode:
authorPamela Dragosh <pdragosh@research.att.com>2020-08-18 12:24:04 -0400
committerPamela Dragosh <pdragosh@research.att.com>2020-08-18 12:24:08 -0400
commitc663e0061b92542d961db5f88039151acb7f63df (patch)
tree0bb1f2f61d74f6489ea77a872273b57680e25faa /applications/guard
parentad9d827a43211c087fe4bcf575134aea3a5b316e (diff)
Fix sonar issue and add code coverage
Fixing too much complexity for GuardPolicyRequest by breaking it up into methods. Added more code coverage to guard translator. Added missing code coverage for coordination guard, it was below 66% and there were some trivial fixes that were needed. Seems that a test resource was never really used, or perhaps got omitted in a previous review. Issue-ID: POLICY-2590 Change-Id: I1be4ff166e6c43c1c4ea54fdf7e761e22adcaa2d Signed-off-by: Pamela Dragosh <pdragosh@research.att.com>
Diffstat (limited to 'applications/guard')
-rw-r--r--applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/CoordinationGuardTranslator.java11
-rw-r--r--applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/GuardPolicyRequest.java14
-rw-r--r--applications/guard/src/test/java/org/onap/policy/xacml/pdp/application/guard/CoordinationGuardTranslatorTest.java69
-rw-r--r--applications/guard/src/test/java/org/onap/policy/xacml/pdp/application/guard/GuardTranslatorTest.java6
-rw-r--r--applications/guard/src/test/resources/test-directive.yaml2
5 files changed, 92 insertions, 10 deletions
diff --git a/applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/CoordinationGuardTranslator.java b/applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/CoordinationGuardTranslator.java
index d20a25f0..126955e7 100644
--- a/applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/CoordinationGuardTranslator.java
+++ b/applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/CoordinationGuardTranslator.java
@@ -113,6 +113,9 @@ public class CoordinationGuardTranslator implements ToscaPolicyTranslator {
*/
public static CoordinationDirective loadCoordinationDirectiveFromFile(
String directiveFilename) {
+ if (directiveFilename == null) {
+ return null;
+ }
try (InputStream is = new FileInputStream(new File(directiveFilename))) {
String contents = IOUtils.toString(is, StandardCharsets.UTF_8);
//
@@ -142,19 +145,19 @@ public class CoordinationGuardTranslator implements ToscaPolicyTranslator {
*/
String xacmlProtoFilename =
protoDir + File.separator + cd.getCoordinationFunction() + ".xml";
- LOGGER.debug("xacmlProtoFilename={}", xacmlProtoFilename);
+ LOGGER.info("xacmlProtoFilename={}", xacmlProtoFilename);
/*
- * Values to be used for placeholders
+ * Values to be substituted for placeholder's
*/
final String uniqueId = UUID.randomUUID().toString();
final String cLOne = cd.getControlLoop(0);
final String cLTwo = cd.getControlLoop(1);
/*
- * Replace function placeholders with appropriate values
+ * Replace function placeholder's with appropriate values
*/
String policyXml = ResourceUtils.getResourceAsString(xacmlProtoFilename);
if (policyXml == null) {
- throw new ToscaPolicyConversionException("Error while generating XACML policy for coordination directive");
+ throw new ToscaPolicyConversionException("Unable to find prototype " + xacmlProtoFilename);
}
policyXml = policyXml.replace("UNIQUE_ID", uniqueId);
policyXml = policyXml.replace("CONTROL_LOOP_ONE", cLOne);
diff --git a/applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/GuardPolicyRequest.java b/applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/GuardPolicyRequest.java
index 72c8ddd2..87635963 100644
--- a/applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/GuardPolicyRequest.java
+++ b/applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/GuardPolicyRequest.java
@@ -139,6 +139,13 @@ public class GuardPolicyRequest {
//
// Find our fields
//
+ findFields(request, guard);
+ findFilterFields(request, guard);
+ return request;
+ }
+
+ private static GuardPolicyRequest findFields(GuardPolicyRequest request, Map<String, Object> guard)
+ throws ToscaPolicyConversionException {
if (guard.containsKey("actor")) {
request.actorId = guard.get("actor").toString();
}
@@ -158,6 +165,11 @@ public class GuardPolicyRequest {
throw new ToscaPolicyConversionException("Failed to decode vfCount", e);
}
}
+
+ return request;
+ }
+
+ private static GuardPolicyRequest findFilterFields(GuardPolicyRequest request, Map<String, Object> guard) {
if (guard.containsKey("generic-vnf.vnf-name")) {
request.vnfName = guard.get("generic-vnf.vnf-name").toString();
}
@@ -176,8 +188,6 @@ public class GuardPolicyRequest {
if (guard.containsKey("cloud-region.cloud-region-id")) {
request.cloudRegionId = guard.get("cloud-region.cloud-region-id").toString();
}
-
return request;
}
-
}
diff --git a/applications/guard/src/test/java/org/onap/policy/xacml/pdp/application/guard/CoordinationGuardTranslatorTest.java b/applications/guard/src/test/java/org/onap/policy/xacml/pdp/application/guard/CoordinationGuardTranslatorTest.java
new file mode 100644
index 00000000..d0541e2a
--- /dev/null
+++ b/applications/guard/src/test/java/org/onap/policy/xacml/pdp/application/guard/CoordinationGuardTranslatorTest.java
@@ -0,0 +1,69 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.xacml.pdp.application.guard;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+
+import org.junit.Test;
+import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
+
+public class CoordinationGuardTranslatorTest {
+
+ @Test
+ public void testUnsupportedMethods() {
+ CoordinationGuardTranslator translator = new CoordinationGuardTranslator();
+
+ assertThatExceptionOfType(ToscaPolicyConversionException.class)
+ .isThrownBy(() -> translator.convertRequest(null))
+ .withMessageContaining("this convertRequest shouldn't be used");
+
+ assertThat(translator.convertResponse(null)).isNull();
+ }
+
+ @Test
+ public void testLoadingDirectives() {
+ assertThat(CoordinationGuardTranslator.loadCoordinationDirectiveFromFile(null)).isNull();
+
+ assertThat(CoordinationGuardTranslator.loadCoordinationDirectiveFromFile("nonexistent.yaml")).isNull();
+
+ CoordinationDirective directive = CoordinationGuardTranslator
+ .loadCoordinationDirectiveFromFile("src/test/resources/test-directive.yaml");
+ assertThat(directive).isNotNull();
+ assertThat(directive.getCoordinationFunction()).isEqualTo("whatisthisvaluesupposedtobe");
+ assertThat(directive.getControlLoop()).hasSize(2);
+ assertThat(directive.getControlLoop()).contains("cl1", "cl2");
+ }
+
+ @Test
+ public void testGeneratingXacml() {
+ CoordinationDirective directive = CoordinationGuardTranslator
+ .loadCoordinationDirectiveFromFile("src/test/resources/test-directive.yaml");
+
+ assertThatExceptionOfType(ToscaPolicyConversionException.class)
+ .isThrownBy(() -> CoordinationGuardTranslator
+ .generateXacmlFromCoordinationDirective(directive, "idontexist.yaml"))
+ .withMessageContaining("Unable to find prototype ");
+ }
+
+}
diff --git a/applications/guard/src/test/java/org/onap/policy/xacml/pdp/application/guard/GuardTranslatorTest.java b/applications/guard/src/test/java/org/onap/policy/xacml/pdp/application/guard/GuardTranslatorTest.java
index 07e60c61..efe698eb 100644
--- a/applications/guard/src/test/java/org/onap/policy/xacml/pdp/application/guard/GuardTranslatorTest.java
+++ b/applications/guard/src/test/java/org/onap/policy/xacml/pdp/application/guard/GuardTranslatorTest.java
@@ -136,11 +136,11 @@ public class GuardTranslatorTest {
assertThatExceptionOfType(ToscaPolicyConversionException.class).isThrownBy(() ->
translator.convertPolicy(policy)
).withMessageContaining("Missing blacklist");
- } else if ("blacklist-noalgorithm".equals(policy.getName())) {
+ } else if ("filter-noalgorithm".equals(policy.getName())) {
assertThatExceptionOfType(ToscaPolicyConversionException.class).isThrownBy(() ->
translator.convertPolicy(policy)
- ).withMessageContaining("Missing precedence");
- } else if ("blacklist-badalgorithm".equals(policy.getName())) {
+ ).withMessageContaining("Missing algorithm");
+ } else if ("filter-badalgorithm".equals(policy.getName())) {
assertThatExceptionOfType(ToscaPolicyConversionException.class)
.isThrownBy(() -> translator.convertPolicy(policy))
.withMessageContaining(
diff --git a/applications/guard/src/test/resources/test-directive.yaml b/applications/guard/src/test/resources/test-directive.yaml
index 44dfa74b..1f477766 100644
--- a/applications/guard/src/test/resources/test-directive.yaml
+++ b/applications/guard/src/test/resources/test-directive.yaml
@@ -1,4 +1,4 @@
controlLoop:
- cl1
- cl2
-directive: whatisthisvaluesupposedtobe
+coordinationFunction: whatisthisvaluesupposedtobe