diff options
author | Pamela Dragosh <pdragosh@research.att.com> | 2020-03-10 16:17:36 -0400 |
---|---|---|
committer | Pamela Dragosh <pdragosh@research.att.com> | 2020-03-10 16:17:41 -0400 |
commit | c48d65afeecfe1fd5cd735708107aa7c3bc87d17 (patch) | |
tree | e88bceda045169fea04ce1c144009ec1cf008640 /applications/common/src/test | |
parent | ef1c07eb976ff4d8145f40ce4db092591bb78047 (diff) |
Misc XACML code coverage
Removed duplicate code from operations history classes.
added code coverage to other classes.
cleared sonar in main for args
Issue-ID: POLICY-2242
Change-Id: Ief548c96a180c132a3f17681d9cbc018c62e42ac
Signed-off-by: Pamela Dragosh <pdragosh@research.att.com>
Diffstat (limited to 'applications/common/src/test')
5 files changed, 252 insertions, 85 deletions
diff --git a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/OnapPolicyFinderFactoryTest.java b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/OnapPolicyFinderFactoryTest.java index 5cd1cdc7..b5b8f7b1 100644 --- a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/OnapPolicyFinderFactoryTest.java +++ b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/OnapPolicyFinderFactoryTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-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. @@ -22,17 +22,54 @@ package org.onap.policy.pdp.xacml.application.common; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import java.io.FileInputStream; +import java.util.Properties; import org.junit.Test; public class OnapPolicyFinderFactoryTest { @Test - public void test() throws NoSuchMethodException, SecurityException { + public void testNoUseConstructor() throws Exception { assertThatExceptionOfType(OnapPolicyFinderFactoryException.class).isThrownBy(() -> { new OnapPolicyFinderFactory(); }).withMessageContaining("Please use the constructor with Properties object."); } + @Test + public void testFinder() throws Exception { + // + // Load our test properties to use + // + Properties properties = new Properties(); + try (FileInputStream is = new FileInputStream("src/test/resources/finder.test.properties")) { + properties.load(is); + } + OnapPolicyFinderFactory finder = new OnapPolicyFinderFactory(properties); + assertThat(finder).isNotNull(); + + assertThat(finder.getPolicyFinder()).isNotNull(); + assertThat(finder.getPolicyFinder(properties)).isNotNull(); + } + + @Test + public void testFinderWithCombiningAlgorithm() throws Exception { + // + // Load our test properties to use + // + Properties properties = new Properties(); + try (FileInputStream is = new FileInputStream("src/test/resources/finder.test.properties")) { + properties.load(is); + } + // + // Set a combining algorithm + // + properties.put("xacml.att.policyFinderFactory.combineRootPolicies", + "urn:com:att:xacml:3.0:policy-combining-algorithm:combined-permit-overrides"); + OnapPolicyFinderFactory finder = new OnapPolicyFinderFactory(properties); + assertThat(finder).isNotNull(); + } + } diff --git a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/operationshistory/CountRecentOperationsPipTest.java b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/operationshistory/CountRecentOperationsPipTest.java index ac8a0c29..9a0eb6de 100644 --- a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/operationshistory/CountRecentOperationsPipTest.java +++ b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/operationshistory/CountRecentOperationsPipTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-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. @@ -20,8 +20,8 @@ package org.onap.policy.pdp.xacml.application.common.operationshistory; import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.Assert.assertEquals; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; import com.att.research.xacml.api.Attribute; @@ -115,6 +115,16 @@ public class CountRecentOperationsPipTest { } /** + * Close the entity manager. + */ + @AfterClass + public static void cleanup() { + if (em != null) { + em.close(); + } + } + + /** * Create an instance of our engine. * * @throws Exception if an error occurs @@ -144,22 +154,6 @@ public class CountRecentOperationsPipTest { when(okStatus.isOk()).thenReturn(true); } - private Dbao createEntry(String cl, String target, String outcome) { - // - // Create entry - // - Dbao newEntry = new Dbao(); - newEntry.setClosedLoopName(cl); - newEntry.setTarget(target); - newEntry.setOutcome(outcome); - newEntry.setActor("Controller"); - newEntry.setOperation("operationA"); - newEntry.setStarttime(Date.from(Instant.now().minusMillis(20000))); - newEntry.setEndtime(Date.from(Instant.now())); - newEntry.setRequestId(UUID.randomUUID().toString()); - return newEntry; - } - @Test public void testAttributesRequired() { assertEquals(3, pipEngine.attributesRequired().size()); @@ -236,19 +230,6 @@ public class CountRecentOperationsPipTest { assertEquals(1, getCount(newEntry)); } - private long getCount(Dbao newEntry) throws PIPException { - responses = new LinkedList<>(Arrays.asList(resp1, resp2, resp3)); - attributes = new LinkedList<>( - Arrays.asList(newEntry.getActor(), newEntry.getOperation(), newEntry.getTarget())); - - PIPResponse result = pipEngine.getAttributes(pipRequest, pipFinder); - - Attribute attr = result.getAttributes().iterator().next(); - AttributeValue<?> value = attr.getValues().iterator().next(); - - return ((Number) value.getValue()).longValue(); - } - @Test public void testStringToChronoUnit() throws PIPException { // not configured yet @@ -277,14 +258,33 @@ public class CountRecentOperationsPipTest { assertEquals(-1, getCount(newEntry)); } - /** - * Close the entity manager. - */ - @AfterClass - public static void cleanup() { - if (em != null) { - em.close(); - } + private long getCount(Dbao newEntry) throws PIPException { + responses = new LinkedList<>(Arrays.asList(resp1, resp2, resp3)); + attributes = new LinkedList<>( + Arrays.asList(newEntry.getActor(), newEntry.getOperation(), newEntry.getTarget())); + + PIPResponse result = pipEngine.getAttributes(pipRequest, pipFinder); + + Attribute attr = result.getAttributes().iterator().next(); + AttributeValue<?> value = attr.getValues().iterator().next(); + + return ((Number) value.getValue()).longValue(); + } + + private Dbao createEntry(String cl, String target, String outcome) { + // + // Create entry + // + Dbao newEntry = new Dbao(); + newEntry.setClosedLoopName(cl); + newEntry.setTarget(target); + newEntry.setOutcome(outcome); + newEntry.setActor("Controller"); + newEntry.setOperation("operationA"); + newEntry.setStarttime(Date.from(Instant.now().minusMillis(20000))); + newEntry.setEndtime(Date.from(Instant.now())); + newEntry.setRequestId(UUID.randomUUID().toString()); + return newEntry; } private class MyPip extends CountRecentOperationsPip { diff --git a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/operationshistory/GetOperationOutcomePipTest.java b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/operationshistory/GetOperationOutcomePipTest.java index 037f49a4..f4ed1a3b 100644 --- a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/operationshistory/GetOperationOutcomePipTest.java +++ b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/operationshistory/GetOperationOutcomePipTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-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. @@ -18,30 +18,46 @@ package org.onap.policy.pdp.xacml.application.common.operationshistory; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; +import com.att.research.xacml.api.pip.PIPException; +import com.att.research.xacml.api.pip.PIPFinder; +import com.att.research.xacml.api.pip.PIPRequest; +import com.att.research.xacml.std.pip.StdPIPResponse; import java.io.FileInputStream; import java.lang.reflect.Method; import java.sql.Date; import java.time.Instant; import java.util.Properties; import java.util.UUID; - import javax.persistence.EntityManager; import javax.persistence.Persistence; - import org.junit.AfterClass; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class GetOperationOutcomePipTest { private static final Logger LOGGER = LoggerFactory.getLogger(GetOperationOutcomePipTest.class); - private static GetOperationOutcomePip pipEngine; + private static final String TEST_PROPERTIES = "src/test/resources/test.properties"; private static EntityManager em; + private Properties properties; + private GetOperationOutcomePip pipEngine; + + @Mock + private PIPRequest pipRequest; + + @Mock + private PIPFinder pipFinder; + /** * Create an instance of our engine and also the persistence * factory. @@ -49,31 +65,21 @@ public class GetOperationOutcomePipTest { * @throws Exception connectivity issues */ @BeforeClass - public static void setup() throws Exception { + public static void setupDatabase() throws Exception { LOGGER.info("Setting up PIP Testing"); // - // Create instance - // - pipEngine = new GetOperationOutcomePip(); - // // Load our test properties to use // - Properties properties = new Properties(); - try (FileInputStream is = new FileInputStream("src/test/resources/test.properties")) { - properties.load(is); + Properties props = new Properties(); + try (FileInputStream is = new FileInputStream(TEST_PROPERTIES)) { + props.load(is); } // - // Configure it using properties - // - pipEngine.configure("issuer", properties); - LOGGER.info("PIP configured now creating our entity manager"); - LOGGER.info("properties {}", properties); - // // Connect to in-mem db // String persistenceUnit = GetOperationOutcomePip.ISSUER_NAME + ".persistenceunit"; LOGGER.info("persistenceunit {}", persistenceUnit); - em = Persistence.createEntityManagerFactory(properties.getProperty(persistenceUnit), properties) + em = Persistence.createEntityManagerFactory(props.getProperty(persistenceUnit), props) .createEntityManager(); // // @@ -81,27 +87,70 @@ public class GetOperationOutcomePipTest { LOGGER.info("Configured own entity manager", em.toString()); } - private void insertEntry(String cl, String target, String outcome) { + /** + * Close the entity manager. + */ + @AfterClass + public static void cleanup() { + if (em != null) { + em.close(); + } + } + + /** + * Create an instance of our engine. + * + * @throws Exception if an error occurs + */ + @Before + public void setupEngine() throws Exception { + MockitoAnnotations.initMocks(this); + + when(pipRequest.getIssuer()).thenReturn("urn:org:onap:xacml:guard:tw:1:hour"); // - // Create entry + // Create instance // - Dbao newEntry = new Dbao(); - newEntry.setClosedLoopName(cl); - newEntry.setTarget(target); - newEntry.setOutcome(outcome); - newEntry.setActor("Controller"); - newEntry.setOperation("operationA"); - newEntry.setStarttime(Date.from(Instant.now().minusMillis(20000))); - newEntry.setEndtime(Date.from(Instant.now())); - newEntry.setRequestId(UUID.randomUUID().toString()); + pipEngine = new GetOperationOutcomePip(); // - // Add entry + // Load the properties // - em.getTransaction().begin(); - em.persist(newEntry); - em.getTransaction().commit(); + properties = new Properties(); + try (FileInputStream is = new FileInputStream(TEST_PROPERTIES)) { + properties.load(is); + } + // + // Configure it using properties + // + pipEngine.configure("issuer", properties); + LOGGER.info("PIP configured now creating our entity manager"); + LOGGER.info("properties {}", properties); + + } + + @Test + public void testAttributesRequired() { + assertEquals(1, pipEngine.attributesRequired().size()); + } + + @Test + public void testConfigure_DbException() throws Exception { + properties.put("javax.persistence.jdbc.url", "invalid"); + assertThatCode(() -> + pipEngine.configure("issuer", properties) + ).doesNotThrowAnyException(); + } + + @Test + public void testGetAttributes_NullIssuer() throws PIPException { + when(pipRequest.getIssuer()).thenReturn(null); + assertEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pipEngine.getAttributes(pipRequest, pipFinder)); } + @Test + public void testGetAttributes_WrongIssuer() throws PIPException { + when(pipRequest.getIssuer()).thenReturn("wrong-issuer"); + assertEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pipEngine.getAttributes(pipRequest, pipFinder)); + } @Test public void testGetOutcomeFromDb() throws Exception { @@ -143,14 +192,24 @@ public class GetOperationOutcomePipTest { assertEquals("4", outcome); } - /** - * Close the entity manager. - */ - @AfterClass - public static void cleanup() { - if (em != null) { - em.close(); - } + private void insertEntry(String cl, String target, String outcome) { + // + // Create entry + // + Dbao newEntry = new Dbao(); + newEntry.setClosedLoopName(cl); + newEntry.setTarget(target); + newEntry.setOutcome(outcome); + newEntry.setActor("Controller"); + newEntry.setOperation("operationA"); + newEntry.setStarttime(Date.from(Instant.now().minusMillis(20000))); + newEntry.setEndtime(Date.from(Instant.now())); + newEntry.setRequestId(UUID.randomUUID().toString()); + // + // Add entry + // + em.getTransaction().begin(); + em.persist(newEntry); + em.getTransaction().commit(); } - } diff --git a/applications/common/src/test/resources/finder.test.properties b/applications/common/src/test/resources/finder.test.properties new file mode 100644 index 00000000..73201fdd --- /dev/null +++ b/applications/common/src/test/resources/finder.test.properties @@ -0,0 +1,32 @@ +# +# Properties that the embedded PDP engine uses to configure and load +# +# Standard API Factories +# +xacml.dataTypeFactory=com.att.research.xacml.std.StdDataTypeFactory +xacml.pdpEngineFactory=com.att.research.xacmlatt.pdp.ATTPDPEngineFactory +xacml.pepEngineFactory=com.att.research.xacml.std.pep.StdEngineFactory +xacml.pipFinderFactory=com.att.research.xacml.std.pip.StdPIPFinderFactory +xacml.traceEngineFactory=com.att.research.xacml.std.trace.LoggingTraceEngineFactory +# +# AT&T PDP Implementation Factories +# +xacml.att.evaluationContextFactory=com.att.research.xacmlatt.pdp.std.StdEvaluationContextFactory +xacml.att.combiningAlgorithmFactory=com.att.research.xacmlatt.pdp.std.StdCombiningAlgorithmFactory +xacml.att.functionDefinitionFactory=com.att.research.xacmlatt.pdp.std.StdFunctionDefinitionFactory +# +# ONAP PDP Implementation Factories +# +xacml.att.policyFinderFactory=org.onap.policy.pdp.xacml.application.common.OnapPolicyFinderFactory +# +# +# +xacml.rootPolicies=rootstart,noexist +rootstart.file=src/test/resources/finder.xml +noexist.file=src/test/resources/idonotexist.xml + +xacml.referencedPolicies=refstart1,refstart2,refstart3,refstart4 +refstart1.file=src/test/resources/ref1.xml +refstart2.file=src/test/resources/ref2.xml +refstart3.file=src/test/resources/ref3.xml +refstart4.file=src/test/resources/ref4.xml diff --git a/applications/common/src/test/resources/finder.xml b/applications/common/src/test/resources/finder.xml new file mode 100644 index 00000000..dc69d99a --- /dev/null +++ b/applications/common/src/test/resources/finder.xml @@ -0,0 +1,39 @@ +<?xml version="1.0" encoding="UTF-8"?> +<Policy + xmlns="urn:oasis:names:tc:xacml:3.0:core:schema:wd-17" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="urn:oasis:names:tc:xacml:3.0:core:schema:wd-17 + http://docs.oasis-open.org/xacml/3.0/xacml-core-v3-schema-wd-17.xsd" + PolicyId="urn:oasis:names:tc:xacml:3.0:example:SimplePolicy1" + Version="1.0" + RuleCombiningAlgId="identifier:rule-combining-algorithm:deny-overrides"> + <Description> + Medi Corp access control policy + </Description> + <Target/> + <Rule + RuleId= "urn:oasis:names:tc:xacml:3.0:example:SimpleRule1" + Effect="Permit"> + <Description> + Any subject with an e-mail name in the med.example.com domain + can perform any action on any resource. + </Description> + <Target> + <AnyOf> + <AllOf> + <Match + MatchId="urn:oasis:names:tc:xacml:1.0:function:rfc822Name-match"> + <AttributeValue + DataType="http://www.w3.org/2001/XMLSchema#string" + >med.example.com</AttributeValue> + <AttributeDesignator + MustBePresent="false" + Category="urn:oasis:names:tc:xacml:1.0:subject-category:access-subject" + AttributeId="urn:oasis:names:tc:xacml:1.0:subject:subject-id" + DataType="urn:oasis:names:tc:xacml:1.0:data-type:rfc822Name"/> + </Match> + </AllOf> + </AnyOf> + </Target> + </Rule> +</Policy>
\ No newline at end of file |