diff options
Diffstat (limited to 'applications/common/src')
13 files changed, 298 insertions, 223 deletions
diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/OnapPolicyFinderFactory.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/OnapPolicyFinderFactory.java index e66c9943..b000db92 100644 --- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/OnapPolicyFinderFactory.java +++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/OnapPolicyFinderFactory.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. @@ -40,19 +40,12 @@ import com.att.research.xacmlatt.pdp.std.StdPolicyFinder; import com.att.research.xacmlatt.pdp.util.ATTPDPProperties; import com.google.common.base.Splitter; import com.google.common.base.Strings; - import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Properties; import java.util.UUID; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -123,14 +116,6 @@ public class OnapPolicyFinderFactory extends PolicyFinderFactory { } } - propLocation = this.properties.getProperty(policyId + PROP_URL); - if (propLocation != null) { - PolicyDef policy = this.loadPolicyUrlDef(propLocation); - if (policy != null) { - return policy; - } - } - logger.error("No known location for Policy {}", policyId); return null; } @@ -153,41 +138,9 @@ public class OnapPolicyFinderFactory extends PolicyFinderFactory { } return new Policy(StdStatusCode.STATUS_CODE_SYNTAX_ERROR, "DOM Could not load policy"); } catch (DOMStructureException ex) { - logger.error("Error loading policy file {}: {}", fileLocation.getAbsolutePath(), ex); - return new Policy(StdStatusCode.STATUS_CODE_SYNTAX_ERROR, ex.getMessage()); - } - } - - protected PolicyDef loadPolicyUrlDef(String propLocation) { - InputStream is = null; - try { - URL url = new URL(propLocation); - URLConnection urlConnection = url.openConnection(); - OnapPolicyFinderFactory.logger.info("Loading policy file {}", url); - is = urlConnection.getInputStream(); - PolicyDef policyDef = DOMPolicyDef.load(is); - if (policyDef != null) { - return policyDef; - } - } catch (MalformedURLException ex) { - logger.error("Invalid URL " + propLocation + ": " + ex.getMessage(), ex); - } catch (IOException ex) { - logger.error("IOException opening URL {}: {}{}", - propLocation, ex.getMessage(), ex); - } catch (DOMStructureException ex) { - logger.error("Invalid Policy " + propLocation + ": " + ex.getMessage(), ex); + logger.error("Error loading policy file {}", fileLocation.getAbsolutePath(), ex); return new Policy(StdStatusCode.STATUS_CODE_SYNTAX_ERROR, ex.getMessage()); - } finally { - if (is != null) { - try { - is.close(); - } catch (IOException e) { - logger.error("Exception closing InputStream for GET of url {}: {}", - propLocation, e.getMessage() + " (May be memory leak)", e); - } - } } - return null; } /** diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/operationshistory/CountRecentOperationsPip.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/operationshistory/CountRecentOperationsPip.java index 7304dfca..4bf87779 100644 --- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/operationshistory/CountRecentOperationsPip.java +++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/operationshistory/CountRecentOperationsPip.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. @@ -28,17 +28,11 @@ import com.att.research.xacml.api.pip.PIPResponse; import com.att.research.xacml.std.pip.StdMutablePIPResponse; import com.att.research.xacml.std.pip.StdPIPResponse; import com.google.common.base.Strings; - import java.sql.Timestamp; import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.Arrays; -import java.util.Base64; import java.util.Collection; -import java.util.Properties; - -import javax.persistence.Persistence; - import org.onap.policy.pdp.xacml.application.common.ToscaDictionary; import org.onap.policy.pdp.xacml.application.common.std.StdOnapPip; import org.slf4j.Logger; @@ -51,6 +45,7 @@ public class CountRecentOperationsPip extends StdOnapPip { public CountRecentOperationsPip() { super(); + this.issuer = ISSUER_NAME; } @Override @@ -58,38 +53,6 @@ public class CountRecentOperationsPip extends StdOnapPip { return Arrays.asList(PIP_REQUEST_ACTOR, PIP_REQUEST_RECIPE, PIP_REQUEST_TARGET); } - @Override - public void configure(String id, Properties properties) throws PIPException { - super.configure(id, properties); - // - // Create our entity manager - // - em = null; - try { - // - // In case there are any overloaded properties for the JPA - // - Properties emProperties = new Properties(); - emProperties.putAll(properties); - - // - // Need to decode the password before creating the EntityManager - // - String decodedPassword = new String(Base64.getDecoder() - .decode(emProperties.getProperty("javax.persistence.jdbc.password"))); - emProperties.setProperty("javax.persistence.jdbc.password", decodedPassword); - - // - // Create the entity manager factory - // - em = Persistence.createEntityManagerFactory( - properties.getProperty(ISSUER_NAME + ".persistenceunit"), - emProperties).createEntityManager(); - } catch (Exception e) { - logger.error("Persistence failed {} operations history db {}", e.getLocalizedMessage(), e); - } - } - /** * getAttributes. * diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/operationshistory/GetOperationOutcomePip.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/operationshistory/GetOperationOutcomePip.java index 56d68d55..0f970f7e 100644 --- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/operationshistory/GetOperationOutcomePip.java +++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/operationshistory/GetOperationOutcomePip.java @@ -16,7 +16,6 @@ * ============LICENSE_END========================================================= */ - package org.onap.policy.pdp.xacml.application.common.operationshistory; import com.att.research.xacml.api.XACML3; @@ -27,14 +26,9 @@ import com.att.research.xacml.api.pip.PIPResponse; import com.att.research.xacml.std.pip.StdMutablePIPResponse; import com.att.research.xacml.std.pip.StdPIPResponse; import com.google.common.base.Strings; - import java.util.Arrays; -import java.util.Base64; import java.util.Collection; -import java.util.Properties; import javax.persistence.NoResultException; -import javax.persistence.Persistence; - import org.onap.policy.pdp.xacml.application.common.ToscaDictionary; import org.onap.policy.pdp.xacml.application.common.std.StdOnapPip; import org.slf4j.Logger; @@ -47,6 +41,7 @@ public class GetOperationOutcomePip extends StdOnapPip { public GetOperationOutcomePip() { super(); + this.issuer = ISSUER_NAME; } @Override @@ -54,38 +49,6 @@ public class GetOperationOutcomePip extends StdOnapPip { return Arrays.asList(PIP_REQUEST_TARGET); } - @Override - public void configure(String id, Properties properties) throws PIPException { - super.configure(id, properties); - // - // Create our entity manager - // - em = null; - try { - // - // In case there are any overloaded properties for the JPA - // - Properties emProperties = new Properties(); - emProperties.putAll(properties); - - // - // Need to decode the password before creating the EntityManager - // - String decodedPassword = new String(Base64.getDecoder() - .decode(emProperties.getProperty("javax.persistence.jdbc.password"))); - emProperties.setProperty("javax.persistence.jdbc.password", decodedPassword); - - // - // Create the entity manager factory - // - em = Persistence.createEntityManagerFactory( - properties.getProperty(ISSUER_NAME + ".persistenceunit"), - emProperties).createEntityManager(); - } catch (Exception e) { - logger.error("Persistence failed {} operations history db {}", e.getLocalizedMessage(), e); - } - } - /** * getAttributes. * diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdCombinedPolicyResultsTranslator.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdCombinedPolicyResultsTranslator.java index 0a1ace2b..aba5e252 100644 --- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdCombinedPolicyResultsTranslator.java +++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdCombinedPolicyResultsTranslator.java @@ -132,7 +132,7 @@ public class StdCombinedPolicyResultsTranslator extends StdBaseTranslator { try { return RequestParser.parseRequest(StdCombinedPolicyRequest.createInstance(request)); } catch (IllegalArgumentException | IllegalAccessException | DataTypeException e) { - LOGGER.error("Failed to convert DecisionRequest: {}", e); + LOGGER.error("Failed to convert DecisionRequest", e); } // // TODO throw exception diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchablePolicyRequest.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchablePolicyRequest.java index 2cb82942..d0ae524c 100644 --- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchablePolicyRequest.java +++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchablePolicyRequest.java @@ -86,7 +86,7 @@ public class StdMatchablePolicyRequest { } dataTypeFactory = DataTypeFactory.newInstance(); } catch (FactoryException e) { - LOGGER.error("Can't get Data type Factory: {}", e); + LOGGER.error("Can't get Data type Factory", e); } return dataTypeFactory; } diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslator.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslator.java index e84a9e77..d5e2a395 100644 --- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslator.java +++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslator.java @@ -105,7 +105,7 @@ public class StdMatchableTranslator extends StdBaseTranslator { try { return StdMatchablePolicyRequest.createInstance(request); } catch (XacmlApplicationException e) { - LOGGER.error("Failed to convert DecisionRequest: {}", e); + LOGGER.error("Failed to convert DecisionRequest", e); } // // TODO throw exception diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdOnapPip.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdOnapPip.java index 69838599..ca07f22f 100644 --- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdOnapPip.java +++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdOnapPip.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. @@ -36,13 +36,14 @@ import com.att.research.xacml.std.pip.StdPIPRequest; import com.att.research.xacml.std.pip.engines.StdConfigurableEngine; import java.math.BigInteger; +import java.util.Base64; import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.Properties; import javax.persistence.EntityManager; - +import javax.persistence.Persistence; import org.onap.policy.pdp.xacml.application.common.ToscaDictionary; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -68,6 +69,7 @@ public abstract class StdOnapPip extends StdConfigurableEngine { protected Properties properties; protected EntityManager em; + protected String issuer; public StdOnapPip() { super(); @@ -83,6 +85,33 @@ public abstract class StdOnapPip extends StdConfigurableEngine { super.configure(id, properties); logger.info("Configuring historyDb PIP {}", properties); this.properties = properties; + // + // Create our entity manager + // + em = null; + try { + // + // In case there are any overloaded properties for the JPA + // + Properties emProperties = new Properties(); + emProperties.putAll(properties); + + // + // Need to decode the password before creating the EntityManager + // + String decodedPassword = new String(Base64.getDecoder() + .decode(emProperties.getProperty("javax.persistence.jdbc.password"))); + emProperties.setProperty("javax.persistence.jdbc.password", decodedPassword); + + // + // Create the entity manager factory + // + em = Persistence.createEntityManagerFactory( + properties.getProperty(this.issuer + ".persistenceunit"), + emProperties).createEntityManager(); + } catch (Exception e) { + logger.error("Persistence failed {} operations history db", e.getLocalizedMessage(), e); + } } protected String getAttribute(PIPFinder pipFinder, PIPRequest pipRequest) { @@ -114,7 +143,7 @@ public abstract class StdOnapPip extends StdConfigurableEngine { pipResponse = null; } } catch (PIPException ex) { - logger.error("PIPException getting subject-id attribute: " + ex.getMessage(), ex); + logger.error("PIPException getting subject-id attribute", ex); } return pipResponse; } @@ -138,7 +167,7 @@ public abstract class StdOnapPip extends StdConfigurableEngine { try { attributeValue = makeInteger(value); } catch (Exception e) { - logger.error("Failed to convert {} to integer {}", value, e); + logger.error("Failed to convert {} to integer", value, e); } if (attributeValue != null) { stdPipResponse.addAttribute(new StdMutableAttribute(category, attributeId, attributeValue, @@ -152,7 +181,7 @@ public abstract class StdOnapPip extends StdConfigurableEngine { try { attributeValue = makeLong(value); } catch (Exception e) { - logger.error("Failed to convert {} to long {}", value, e); + logger.error("Failed to convert {} to long", value, e); } if (attributeValue != null) { stdPipResponse.addAttribute(new StdMutableAttribute(category, attributeId, attributeValue, diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdXacmlApplicationServiceProvider.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdXacmlApplicationServiceProvider.java index e43e6519..11271651 100644 --- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdXacmlApplicationServiceProvider.java +++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdXacmlApplicationServiceProvider.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. @@ -194,7 +194,7 @@ public abstract class StdXacmlApplicationServiceProvider implements XacmlApplica Files.delete(refPolicy); } catch (IOException e) { LOGGER.error("Failed to delete policy {} from disk {}", toscaPolicy.getMetadata(), - refPolicy.toAbsolutePath().toString(), e); + refPolicy.toAbsolutePath(), e); } // // Write the properties to disk @@ -277,7 +277,7 @@ public abstract class StdXacmlApplicationServiceProvider implements XacmlApplica this.pdpEngine = engine; } } catch (FactoryException e) { - LOGGER.error("Failed to create XACML PDP Engine {}", e); + LOGGER.error("Failed to create XACML PDP Engine", e); } } @@ -299,7 +299,7 @@ public abstract class StdXacmlApplicationServiceProvider implements XacmlApplica try { response = this.pdpEngine.decide(request); } catch (PDPException e) { - LOGGER.error("Xacml PDP Engine failed {}", e); + LOGGER.error("Xacml PDP Engine decide failed", e); } finally { // // Track the end of timing 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 |