diff options
Diffstat (limited to 'applications/common/src/test/java/org')
3 files changed, 181 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(); } - } |