JUnit additions for PDP,PDP-REST,SDK,XACML 47/40547/4
authorbobbymander <bobby.mander@att.com>
Mon, 2 Apr 2018 18:35:37 +0000 (14:35 -0400)
committerbobbymander <bobby.mander@att.com>
Mon, 2 Apr 2018 19:47:02 +0000 (15:47 -0400)
Issue-ID: POLICY-600
Change-Id: I8052451c3f66116f361f2963fcc2727c022dd4d9
Signed-off-by: bobbymander <bobby.mander@att.com>
ONAP-PDP-REST/src/test/java/org/onap/policy/pdp/rest/api/services/DecisionPolicyServiceTest.java
ONAP-PDP/src/test/java/org/onap/policy/xacml/pdp/std/functions/FunctionDefinitionCustomRegexpMatchTest.java
ONAP-SDK-APP/src/test/java/org/onap/portalapp/scheduler/RegisterTest.java
ONAP-XACML/src/test/java/org/onap/policy/xacml/test/util/XACMLPolicyWriterTest.java

index fe06681..24b8497 100644 (file)
  */
 package org.onap.policy.pdp.rest.api.services;
 
-import static org.junit.Assert.*;
-
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 import java.io.FileInputStream;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -29,7 +31,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.UUID;
-
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -40,82 +41,107 @@ import org.onap.policy.api.PolicyParameters;
 
 public class DecisionPolicyServiceTest {
 
-    DecisionPolicyService service = null;
-
-       @Before
-       public void setUp() throws Exception {
-               Properties prop = new Properties();
-               prop.load(new FileInputStream("src/test/resources/pass.xacml.pdp.properties"));
-               String succeeded = prop.getProperty("xacml.rest.pap.url");
-               List<String> paps = Arrays.asList(succeeded.split(","));
-               PAPServices.setPaps(paps);
-               PAPServices.setJunit(true);
-               
-               PolicyParameters policyParameters = new PolicyParameters();
-               policyParameters.setPolicyClass(PolicyClass.Decision);
-        policyParameters.setPolicyName("Test.testDecisionPolicy");
-        policyParameters.setOnapName("MSO");
-        policyParameters.setPolicyDescription("This is a sample Decision policy UPDATE example with Settings");
-        
-        Map<String, String> configAttributes = new HashMap<>(); 
-        configAttributes.put("Template", "UpdateTemplate");
-        configAttributes.put("controller", "default"); 
-        configAttributes.put("SamPoll", "30");
-        configAttributes.put("value", "abcd"); 
-        Map<AttributeType, Map<String,String>> attributes = new HashMap<>();
-        attributes.put(AttributeType.MATCHING, configAttributes);
-        Map<String, String> settingsMap = new HashMap<>();
-        settingsMap.put("server", "5");
-        attributes.put(AttributeType.SETTINGS, settingsMap);
-        policyParameters.setAttributes(attributes);
-        
-               List<String> dynamicRuleAlgorithmLabels = new LinkedList<>();
-               List<String> dynamicRuleAlgorithmFunctions = new LinkedList<>();
-               List<String> dynamicRuleAlgorithmField1 = new LinkedList<>();
-               List<String> dynamicRuleAlgorithmField2 = new LinkedList<>();
-               dynamicRuleAlgorithmLabels = Arrays.asList("A1","A2","A3","A4","A5","A6","A7");
-               dynamicRuleAlgorithmField1 = Arrays.asList("S_server","cap","cobal","A2","Config","A4","A1");
-               dynamicRuleAlgorithmFunctions = Arrays.asList("integer-equal","string-contains","integer-equal","and","integer-greater-than","or","and");
-               dynamicRuleAlgorithmField2 = Arrays.asList("90","ca","90","A3","45","A5","A6");      
-               policyParameters.setDynamicRuleAlgorithmLabels(dynamicRuleAlgorithmLabels);
-               policyParameters.setDynamicRuleAlgorithmField1(dynamicRuleAlgorithmField1);
-               policyParameters.setDynamicRuleAlgorithmFunctions(dynamicRuleAlgorithmFunctions);
-               policyParameters.setDynamicRuleAlgorithmField2(dynamicRuleAlgorithmField2);
-               
-        policyParameters.setRequestID(UUID.randomUUID());
-               policyParameters.setGuard(true);
-               policyParameters.setRiskLevel("5");
-               policyParameters.setRiskType("TEST");
-               String policyName = "testDecisionPolicy";
-               String policyScope = "Test";
-               service = new DecisionPolicyService(policyName, policyScope, policyParameters);
-       }
-
-       @After
-       public void tearDown() throws Exception {
-               PAPServices.setPaps(null);
-               PAPServices.setJunit(false);
-       }
-
-       @Test
-       public final void testDecisionPolicyService() {
-               assertNotNull(service);
-       }
-
-       @Test
-       public final void testGetValidation() {
-               assertTrue(service.getValidation());
-       }
-
-       @Test
-       public final void testGetMessage() {
-               String message = service.getMessage();
-               assertNull(message);    }
-
-       @Test
-       public final void testGetResult() throws PolicyException {
-               service.getValidation();
-               String result = service.getResult(false);
-               assertEquals("success",result); }
+  DecisionPolicyService service = null;
+
+  @Before
+  public void setUp() throws Exception {
+    Properties prop = new Properties();
+    prop.load(new FileInputStream("src/test/resources/pass.xacml.pdp.properties"));
+    String succeeded = prop.getProperty("xacml.rest.pap.url");
+    List<String> paps = Arrays.asList(succeeded.split(","));
+    PAPServices.setPaps(paps);
+    PAPServices.setJunit(true);
+
+    PolicyParameters policyParameters = new PolicyParameters();
+    policyParameters.setPolicyClass(PolicyClass.Decision);
+    policyParameters.setPolicyName("Test.testDecisionPolicy");
+    policyParameters.setOnapName("MSO");
+    policyParameters
+        .setPolicyDescription("This is a sample Decision policy UPDATE example with Settings");
+
+    Map<String, String> configAttributes = new HashMap<>();
+    configAttributes.put("Template", "UpdateTemplate");
+    configAttributes.put("controller", "default");
+    configAttributes.put("SamPoll", "30");
+    configAttributes.put("value", "abcd");
+    Map<AttributeType, Map<String, String>> attributes = new HashMap<>();
+    attributes.put(AttributeType.MATCHING, configAttributes);
+    Map<String, String> settingsMap = new HashMap<>();
+    settingsMap.put("server", "5");
+    attributes.put(AttributeType.SETTINGS, settingsMap);
+    policyParameters.setAttributes(attributes);
+
+    List<String> dynamicRuleAlgorithmLabels = new LinkedList<>();
+    List<String> dynamicRuleAlgorithmFunctions = new LinkedList<>();
+    List<String> dynamicRuleAlgorithmField1 = new LinkedList<>();
+    List<String> dynamicRuleAlgorithmField2 = new LinkedList<>();
+    dynamicRuleAlgorithmLabels = Arrays.asList("A1", "A2", "A3", "A4", "A5", "A6", "A7");
+    dynamicRuleAlgorithmField1 =
+        Arrays.asList("S_server", "cap", "cobal", "A2", "Config", "A4", "A1");
+    dynamicRuleAlgorithmFunctions = Arrays.asList("integer-equal", "string-contains",
+        "integer-equal", "and", "integer-greater-than", "or", "and");
+    dynamicRuleAlgorithmField2 = Arrays.asList("90", "ca", "90", "A3", "45", "A5", "A6");
+    policyParameters.setDynamicRuleAlgorithmLabels(dynamicRuleAlgorithmLabels);
+    policyParameters.setDynamicRuleAlgorithmField1(dynamicRuleAlgorithmField1);
+    policyParameters.setDynamicRuleAlgorithmFunctions(dynamicRuleAlgorithmFunctions);
+    policyParameters.setDynamicRuleAlgorithmField2(dynamicRuleAlgorithmField2);
+
+    policyParameters.setRequestID(UUID.randomUUID());
+    policyParameters.setGuard(true);
+    policyParameters.setRiskLevel("5");
+    policyParameters.setRiskType("TEST");
+    String policyName = "testDecisionPolicy";
+    String policyScope = "Test";
+    service = new DecisionPolicyService(policyName, policyScope, policyParameters);
+  }
+
+  @After
+  public void tearDown() throws Exception {
+    PAPServices.setPaps(null);
+    PAPServices.setJunit(false);
+  }
+
+  @Test
+  public final void testDecisionPolicyService() {
+    assertNotNull(service);
+  }
+
+  @Test
+  public final void testGetValidation() {
+    assertTrue(service.getValidation());
+  }
+
+  @Test
+  public final void testGetMessage() {
+    String message = service.getMessage();
+    assertNull(message);
+  }
+
+  @Test
+  public final void testGetResult() throws PolicyException {
+    service.getValidation();
+    String result = service.getResult(false);
+    assertEquals("success", result);
+  }
+
+  @Test
+  public void testExtraPaths() throws PolicyException {
+    // Set up test data
+    String value = "testVal";
+    PolicyParameters params = new PolicyParameters();
+    Map<AttributeType, Map<String, String>> attributes =
+        new HashMap<AttributeType, Map<String, String>>();
+    // attributes.put(AttributeType.MATCHING, null);
+    attributes.put(AttributeType.SETTINGS, null);
+    params.setAttributes(attributes);
+    DecisionPolicyService service = new DecisionPolicyService(value, value, params);
+
+    // Negative test methods
+    assertEquals(false, service.getValidation());
+    assertEquals("success", service.getResult(true));
+    attributes.remove(AttributeType.SETTINGS);
+    attributes.put(AttributeType.MATCHING, null);
+    assertEquals("success", service.getResult(true));
+  }
 
 }
index da1759a..08a01c8 100644 (file)
@@ -26,6 +26,7 @@ import com.att.research.xacml.api.Identifier;
 import com.att.research.xacml.api.XACML;
 import com.att.research.xacml.std.IdentifierImpl;
 import com.att.research.xacml.std.StdAttributeValue;
+import com.att.research.xacml.std.StdStatusCode;
 import com.att.research.xacml.std.datatypes.DataTypes;
 import com.att.research.xacmlatt.pdp.policy.ExpressionResult;
 import com.att.research.xacmlatt.pdp.policy.FunctionArgument;
@@ -40,7 +41,7 @@ public class FunctionDefinitionCustomRegexpMatchTest {
     @Test
     public final void testRegexp() {
         // Setup
-        final String testVal = "testVal";
+        final String testVal = "testVal,testVal2";
         final String testId = "function:testId";
         final IdentifierImpl testFnId = new IdentifierImpl(testId);
         final Identifier identifier = XACML.ID_DATATYPE_STRING;
@@ -49,11 +50,50 @@ public class FunctionDefinitionCustomRegexpMatchTest {
         final List<FunctionArgument> listFa = new ArrayList<FunctionArgument>();
         listFa.add(fArg);
         listFa.add(fArg);
-
-        // Try a match
         final FunctionDefinitionCustomRegexpMatch<String> regexpMatch =
                 new FunctionDefinitionCustomRegexpMatch<String>(testFnId, DataTypes.DT_STRING);
+
+        // Try a match
         final ExpressionResult result = regexpMatch.evaluate(null, listFa);
-        assertEquals(result.getStatus().isOk(), true);
+        assertEquals(true, result.getValue().getValue());
+
+        // Try error case 1
+        assertEquals(StdStatusCode.STATUS_CODE_PROCESSING_ERROR,
+                regexpMatch.evaluate(null, null).getStatus().getStatusCode());
+
+        // Try error case 2
+        final Identifier identifier2 = XACML.ID_DATATYPE_BOOLEAN;
+        final StdAttributeValue<String> attValue2 = new StdAttributeValue<String>(identifier2, testVal);
+        final FunctionArgument fArg2 = new FunctionArgumentAttributeValue(attValue2);
+        final List<FunctionArgument> listFa2 = new ArrayList<FunctionArgument>();
+        listFa2.add(fArg2);
+        listFa2.add(fArg2);
+        assertEquals(StdStatusCode.STATUS_CODE_PROCESSING_ERROR,
+                regexpMatch.evaluate(null, listFa2).getStatus().getStatusCode());
+
+        // Try error case 3
+        final List<FunctionArgument> listFa3 = new ArrayList<FunctionArgument>();
+        listFa3.add(fArg);
+        listFa3.add(fArg2);
+        assertEquals(StdStatusCode.STATUS_CODE_PROCESSING_ERROR,
+                regexpMatch.evaluate(null, listFa3).getStatus().getStatusCode());
+
+        // Try a mismatch
+        final String testVal4 = "testVal3";
+        final StdAttributeValue<String> attValue4 = new StdAttributeValue<String>(identifier, testVal4);
+        final FunctionArgument fArg4 = new FunctionArgumentAttributeValue(attValue4);
+        final List<FunctionArgument> listFa4 = new ArrayList<FunctionArgument>();
+        listFa4.add(fArg);
+        listFa4.add(fArg4);
+        assertEquals(false, regexpMatch.evaluate(null, listFa4).getValue().getValue());
+
+        // Try a comma match
+        final String testVal5 = "testVal2";
+        final StdAttributeValue<String> attValue5 = new StdAttributeValue<String>(identifier, testVal5);
+        final FunctionArgument fArg5 = new FunctionArgumentAttributeValue(attValue5);
+        final List<FunctionArgument> listFa5 = new ArrayList<FunctionArgument>();
+        listFa5.add(fArg);
+        listFa5.add(fArg5);
+        assertEquals(true, regexpMatch.evaluate(null, listFa5).getValue().getValue());
     }
 }
index ec7724c..0f406b1 100644 (file)
@@ -22,36 +22,45 @@ package org.onap.portalapp.scheduler;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
+
 import java.util.ArrayList;
 import java.util.List;
-import org.junit.Rule;
+
 import org.junit.Test;
-import org.junit.rules.ExpectedException;
 import org.quartz.Trigger;
 import org.quartz.TriggerBuilder;
 
 public class RegisterTest {
-       @Rule 
-       public final ExpectedException thrown = ExpectedException.none();
-
-       @Test
-       public void testRegister() {
-               Register register = new Register();
-               TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
-               Trigger trigger = triggerBuilder.build();
-               List<Trigger> triggers = new ArrayList<Trigger>();
-               triggers.add(trigger);
-               
-               register.setScheduleTriggers(triggers);
-               assertEquals(register.getScheduleTriggers(), triggers);
-               assertEquals(register.getTriggers().length, 1);
-       }
-       
-       @Test
-       public void testRegisterNegativeCase() {
-               thrown.expect(NullPointerException.class);
-               Register register = new Register();
-               register.registerTriggers();
-               fail("Expecting an exception.");
-       }
+    @Test
+    public void testRegister() {
+        Register register = new Register();
+        TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
+        Trigger trigger = triggerBuilder.build();
+        List<Trigger> triggers = new ArrayList<Trigger>();
+        triggers.add(trigger);
+
+        register.setScheduleTriggers(triggers);
+        assertEquals(register.getScheduleTriggers(), triggers);
+        assertEquals(register.getTriggers().length, 1);
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void testRegisterNegativeCase() {
+        Register register = new Register();
+        register.registerTriggers();
+        fail("Expecting an exception.");
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void testRegisterNegativeCase2() {
+        // Setup test data
+        String value = "testVal";
+        Register register = new Register();
+        List<Trigger> triggers = new ArrayList<Trigger>();
+        register.setScheduleTriggers(triggers);
+
+        // Test register
+        register.registerTriggers();
+        fail("Expecting an exception.");
+    }
 }
index c948716..0a3a5e0 100644 (file)
  */
 package org.onap.policy.xacml.test.util;
 
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
-
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
+import java.io.OutputStream;
 import java.nio.file.Path;
-
+import java.nio.file.Paths;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.junit.Before;
 import org.junit.Test;
 import org.onap.policy.xacml.util.XACMLPolicyWriter;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
 
 public class XACMLPolicyWriterTest {
-       private static final Log logger                         = LogFactory.getLog(XACMLPolicyWriterTest.class);
-       private static Path configPolicyPathValue;
-       private static Path actionPolicyPathValue;
-       
-       @Before
-       public void setUp() {
-               File templateFile;
-               ClassLoader classLoader = getClass().getClassLoader();
-        try {
-               templateFile = new File(classLoader.getResource("Config_SampleTest1206.1.xml").getFile());
-               configPolicyPathValue = templateFile.toPath();
-               templateFile = new File(classLoader.getResource("Action_TestActionPolicy.1.xml").getFile());
-               actionPolicyPathValue = templateFile.toPath();
-               } catch (Exception e1) {
-                       logger.error("Exception Occured"+e1);
-               }
-       }
-       
-       @SuppressWarnings("static-access")
-       @Test
-       public void xacmlPolicyWriterTest() throws IOException{
-               XACMLPolicyWriter writer = new XACMLPolicyWriter();
-               String configResponseValue = writer.changeFileNameInXmlWhenRenamePolicy(configPolicyPathValue);
-               assertTrue(configResponseValue.equals("txt"));
-               String actionResponseValue = writer.changeFileNameInXmlWhenRenamePolicy(actionPolicyPathValue);
-               assertTrue(actionResponseValue.equals("json"));
-       }
+  private static final Log logger = LogFactory.getLog(XACMLPolicyWriterTest.class);
+  private static Path configPolicyPathValue;
+  private static Path actionPolicyPathValue;
+
+  @Before
+  public void setUp() {
+    File templateFile;
+    ClassLoader classLoader = getClass().getClassLoader();
+    try {
+      templateFile = new File(classLoader.getResource("Config_SampleTest1206.1.xml").getFile());
+      configPolicyPathValue = templateFile.toPath();
+      templateFile = new File(classLoader.getResource("Action_TestActionPolicy.1.xml").getFile());
+      actionPolicyPathValue = templateFile.toPath();
+    } catch (Exception e1) {
+      logger.error("Exception Occured" + e1);
+    }
+  }
+
+  @SuppressWarnings("static-access")
+  @Test
+  public void xacmlPolicyWriterTest() throws IOException {
+    XACMLPolicyWriter writer = new XACMLPolicyWriter();
+    String configResponseValue = writer.changeFileNameInXmlWhenRenamePolicy(configPolicyPathValue);
+    assertTrue(configResponseValue.equals("txt"));
+    String actionResponseValue = writer.changeFileNameInXmlWhenRenamePolicy(actionPolicyPathValue);
+    assertTrue(actionResponseValue.equals("json"));
+  }
+
+  @Test
+  public void testWrites() {
+    // Set up test data
+    PolicyType policyType = new PolicyType();
+    OutputStream os = new ByteArrayOutputStream();
+    Path filename = Paths.get("/tmp/foo");
+    PolicySetType policySet = new PolicySetType();
+
+    // Test write combinations
+    assertNotNull(XACMLPolicyWriter.writePolicyFile(filename, policySet));
+    assertNotNull(XACMLPolicyWriter.writePolicyFile(filename, policyType));
+    assertNotNull(XACMLPolicyWriter.getXmlAsInputStream(policyType));
+    XACMLPolicyWriter.writePolicyFile(os, policySet);
+    assertNotNull(os.toString());
+    XACMLPolicyWriter.writePolicyFile(os, policyType);
+    assertNotNull(os.toString());
+  }
 }