From d9eb582ec7c95aaa4ebae06bb8e97140cc195697 Mon Sep 17 00:00:00 2001 From: Michael Mokry Date: Fri, 8 Sep 2017 11:25:20 -0500 Subject: [PATCH] New commit to add Junits to PAP-REST Updated the previous commit to resolve merge conflicts from another pull request being merged before this one. Change-Id: I488246061e9b29c75ac8fe45c167ed7afa8db3b8 Issue-ID: POLICY-32 Signed-off-by: Michael Mokry --- .../pap/xacml/rest/components/ActionPolicy.java | 43 +--- .../xacml/rest/components/ActionPolicyTest.java | 142 +++++++++++ .../xacml/rest/components/DecisionPolicyTest.java | 174 +++++++++++++ .../rest/components/FirewallConfigPolicyTest.java | 128 ++++++++++ .../DecisionPolicyDictionaryControllerTest.java | 273 +++++++++++++++++++++ 5 files changed, 728 insertions(+), 32 deletions(-) create mode 100644 ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/components/ActionPolicyTest.java create mode 100644 ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/components/DecisionPolicyTest.java create mode 100644 ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/components/FirewallConfigPolicyTest.java create mode 100644 ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/controller/DecisionPolicyDictionaryControllerTest.java diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ActionPolicy.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ActionPolicy.java index ae17ebf67..1efe82794 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ActionPolicy.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ActionPolicy.java @@ -31,9 +31,6 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; -import javax.persistence.EntityManager; -import javax.persistence.Query; - import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType; import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType; import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType; @@ -53,7 +50,6 @@ import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType; import org.onap.policy.pap.xacml.rest.XACMLPapServlet; import org.onap.policy.pap.xacml.rest.util.JPAUtils; import org.onap.policy.rest.adapter.PolicyRestAdapter; -import org.onap.policy.rest.jpa.ActionPolicyDict; import org.onap.policy.rest.jpa.Datatype; import org.onap.policy.rest.jpa.FunctionDefinition; import org.onap.policy.xacml.api.XACMLErrorConstants; @@ -101,9 +97,6 @@ public class ActionPolicy extends Policy { return isAttribute; } - private static synchronized void setAttribute (boolean att) { - isAttribute = att; - } public ActionPolicy() { super(); @@ -168,9 +161,13 @@ public class ActionPolicy extends Policy { if (policyAdapter.getData() != null) { // Action body is optional so checking value provided or not String comboDictValue = policyAdapter.getActionAttribute(); - String actionBody = getActionPolicyDict(comboDictValue).getBody(); + String actionBody = policyAdapter.getActionBody(); + isAttribute = false; + + //if actionBody is null or empty then we know the ActionAttribute in the request does not exist in the dictionary if(!(actionBody==null || "".equals(actionBody))){ saveActionBody(policyName, actionBody); + isAttribute = true; } else { if(!getAttribute()){ LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Could not find " + comboDictValue + " in the ActionPolicyDict table."); @@ -302,7 +299,7 @@ public class ActionPolicy extends Policy { AttributeValueType typeAttributeValue = new AttributeValueType(); typeAttributeValue.setDataType(STRING_DATATYPE); - String actionDictType = getActionPolicyDict(comboDictValue).getType(); + String actionDictType = policyAdapter.getActionDictType(); typeAttributeValue.getContent().add(actionDictType); assignmentType.setExpression(new ObjectFactory().createAttributeValue(typeAttributeValue)); @@ -315,7 +312,7 @@ public class ActionPolicy extends Policy { AttributeValueType actionURLAttributeValue = new AttributeValueType(); actionURLAttributeValue.setDataType(URI_DATATYPE); - String actionDictUrl = getActionPolicyDict(comboDictValue).getUrl(); + String actionDictUrl = policyAdapter.getActionDictUrl(); actionURLAttributeValue.getContent().add(actionDictUrl); assignmentURL.setExpression(new ObjectFactory().createAttributeValue(actionURLAttributeValue)); @@ -328,14 +325,14 @@ public class ActionPolicy extends Policy { AttributeValueType methodAttributeValue = new AttributeValueType(); methodAttributeValue.setDataType(STRING_DATATYPE); - String actionDictMethod = getActionPolicyDict(comboDictValue).getMethod(); + String actionDictMethod = policyAdapter.getActionDictMethod(); methodAttributeValue.getContent().add(actionDictMethod); assignmentMethod.setExpression(new ObjectFactory().createAttributeValue(methodAttributeValue)); obligation.getAttributeAssignmentExpression().add(assignmentMethod); // Add JSON_URL Assignment: - String actionBody = getActionPolicyDict(comboDictValue).getBody(); + String actionBody = policyAdapter.getActionBody(); if (actionBody != null) { AttributeAssignmentExpressionType assignmentJsonURL = new AttributeAssignmentExpressionType(); assignmentJsonURL.setAttributeId(BODY_ATTRIBUTEID); @@ -349,8 +346,8 @@ public class ActionPolicy extends Policy { obligation.getAttributeAssignmentExpression().add(assignmentJsonURL); } - if(getActionPolicyDict(comboDictValue).getHeader() != null){ - String headerVal = getActionPolicyDict(comboDictValue).getHeader(); + String headerVal = policyAdapter.getActionDictHeader(); + if(headerVal != null){ if(headerVal != null && !headerVal.equals("")){ // parse it on : to get number of headers String[] result = headerVal.split(":"); @@ -510,24 +507,6 @@ public class ActionPolicy extends Policy { } return dropDownMap; } - - private ActionPolicyDict getActionPolicyDict(String attributeName){ - ActionPolicyDict retObj = new ActionPolicyDict(); - EntityManager em = XACMLPapServlet.getEmf().createEntityManager(); - Query getActionPolicyDicts = em.createNamedQuery("ActionPolicyDict.findAll"); - List actionPolicyDicts = getActionPolicyDicts.getResultList(); - - for (Object id : actionPolicyDicts) { - ActionPolicyDict actionPolicy = (ActionPolicyDict) id; - if(attributeName.equals(actionPolicy.getAttributeName())){ - setAttribute(true); - retObj = actionPolicy; - break; - } - } - em.close(); - return retObj; - } @Override public Object getCorrectPolicyDataObject() { diff --git a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/components/ActionPolicyTest.java b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/components/ActionPolicyTest.java new file mode 100644 index 000000000..3adb94cd1 --- /dev/null +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/components/ActionPolicyTest.java @@ -0,0 +1,142 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-PAP-REST + * ================================================================================ + * Copyright (C) 2017 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. + * ============LICENSE_END========================================================= + */ +package org.onap.policy.pap.xacml.rest.components; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; +import org.onap.policy.rest.adapter.PolicyRestAdapter; + +import com.att.research.xacml.util.XACMLProperties; + + +public class ActionPolicyTest { + + private static Logger logger = FlexLogger.getLogger(ActionPolicyTest.class); + PolicyRestAdapter policyAdapter = new PolicyRestAdapter(); + List dynamicRuleAlgorithmLabels = new LinkedList<>(); + List dynamicRuleAlgorithmCombo = new LinkedList<>(); + List dynamicRuleAlgorithmField1 = new LinkedList<>(); + List dynamicRuleAlgorithmField2 = new LinkedList<>(); + Map attributeMap = new HashMap<>(); + ActionPolicy component = null; + + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + logger.info("setUp: Entering"); + System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME,"xacml.pap.properties"); + + dynamicRuleAlgorithmLabels.add("test"); + dynamicRuleAlgorithmField1.add("testField1"); + dynamicRuleAlgorithmCombo.add("testCombo"); + dynamicRuleAlgorithmField2.add("testField2"); + + policyAdapter.setPolicyName("Test.Action_junitTest"); + policyAdapter.setPolicyDescription("test"); + policyAdapter.setRuleCombiningAlgId("urn:oasis:names:tc:xacml:3.0:rule-combining-algorithm:permit-overrides"); + policyAdapter.setPolicyType("Action"); + policyAdapter.setEditPolicy(false); + policyAdapter.setDomainDir("src/test/resources/client.properties"); + policyAdapter.setDomain("Test"); + policyAdapter.setNewFileName("Test.Action_junitTest.1.xml"); + policyAdapter.setHighestVersion(1); + policyAdapter.setPolicyID("urn:xacml:policy:id:"+UUID.randomUUID()); + + policyAdapter.setActionDictHeader(""); + policyAdapter.setActionDictType("REST"); + policyAdapter.setActionDictUrl("onap.org"); + policyAdapter.setActionDictMethod("GET"); + policyAdapter.setActionAttribute("test"); + policyAdapter.setActionBody("test"); + + policyAdapter.setDynamicRuleAlgorithmLabels(dynamicRuleAlgorithmLabels); + policyAdapter.setDynamicRuleAlgorithmCombo(dynamicRuleAlgorithmCombo); + policyAdapter.setDynamicRuleAlgorithmField1(dynamicRuleAlgorithmField1); + policyAdapter.setDynamicRuleAlgorithmField2(dynamicRuleAlgorithmField2); + + attributeMap.put("java", "test"); + policyAdapter.setDynamicFieldConfigAttributes(attributeMap); + + component = new ActionPolicy(policyAdapter); + + logger.info("setUp: exit"); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + } + +/* *//** + * Test method for {@link org.openecomp.policy.pap.xacml.rest.components.ActionPolicy#savePolicies()}. + */ + @Test + public void testSavePolicies() { + ActionPolicy mockAction = Mockito.mock(component.getClass()); + + Map successMap = new HashMap<>(); + + successMap.put("success", "success"); + + try { + when(mockAction.savePolicies()).thenReturn(successMap); + successMap = mockAction.savePolicies(); + } catch (Exception e) { + logger.error("Exception Occured"+e); + } + assertEquals(successMap.get("success"),"success"); + } + + /** + * Test method for {@link org.openecomp.policy.pap.xacml.rest.components.ActionPolicy#prepareToSave()}. + */ + @Test + public void testPrepareToSave() { + logger.debug("test PrepareToSave Policy: enter"); + boolean response = true; + + try { + response = component.prepareToSave(); + } catch (Exception e) { + logger.error("Exception Occured"+e); + } + assertTrue(response); + + } + +} \ No newline at end of file diff --git a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/components/DecisionPolicyTest.java b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/components/DecisionPolicyTest.java new file mode 100644 index 000000000..6d1d44605 --- /dev/null +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/components/DecisionPolicyTest.java @@ -0,0 +1,174 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-PAP-REST + * ================================================================================ + * Copyright (C) 2017 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. + * ============LICENSE_END========================================================= + */ +package org.onap.policy.pap.xacml.rest.components; + +import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; +import org.onap.policy.rest.adapter.PolicyRestAdapter; +import org.onap.policy.rest.adapter.RainyDayParams; + +import com.att.research.xacml.util.XACMLProperties; + +public class DecisionPolicyTest { + + private static Logger logger = FlexLogger.getLogger(DecisionPolicyTest.class); + PolicyRestAdapter policyAdapter = new PolicyRestAdapter(); + RainyDayParams rainyday = new RainyDayParams(); + Map attributeMap = new HashMap<>(); + Map treatmentMap = new HashMap<>(); + Map settingsMap = new HashMap<>(); + List errorCodeList = new LinkedList<>(); + List treatmentList = new LinkedList<>(); + List dynamicRuleAlgorithmLabels = new LinkedList<>(); + List dynamicRuleAlgorithmCombo = new LinkedList<>(); + List dynamicRuleAlgorithmField1 = new LinkedList<>(); + List dynamicRuleAlgorithmField2 = new LinkedList<>(); + List dynamicVariableList = new LinkedList<>(); + List dataTypeList = new LinkedList<>(); + DecisionPolicy component = null; + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + logger.info("setUp: Entering"); + System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME,"xacml.pap.properties"); + + policyAdapter.setPolicyName("Test.Decision_junitTest.1.xml"); + policyAdapter.setPolicyDescription("testing"); + policyAdapter.setRuleCombiningAlgId("urn:oasis:names:tc:xacml:3.0:rule-combining-algorithm:permit-overrides"); + policyAdapter.setPolicyType("Decision"); + policyAdapter.setEditPolicy(false); + policyAdapter.setDomainDir("src/test/resources/client.properties"); + policyAdapter.setDomain("Test"); + policyAdapter.setNewFileName("/src/test/resources/Test/client.properties"); + policyAdapter.setHighestVersion(1); + policyAdapter.setPolicyID("urn:xacml:policy:id:"+UUID.randomUUID()); + policyAdapter.setOnapName("MSO"); + + //rainy day attributes + attributeMap.put("ServiceType", "S"); + attributeMap.put("VNFType", "V"); + attributeMap.put("BB_ID", "testBB"); + attributeMap.put("WorkStep", "1"); + + dynamicRuleAlgorithmLabels.add("test1"); + dynamicRuleAlgorithmField1.add("testField1"); + dynamicRuleAlgorithmCombo.add("testruleCombo"); + dynamicRuleAlgorithmField2.add("testField2"); + + policyAdapter.setDynamicRuleAlgorithmLabels(dynamicRuleAlgorithmLabels); + policyAdapter.setDynamicRuleAlgorithmCombo(dynamicRuleAlgorithmCombo); + policyAdapter.setDynamicRuleAlgorithmField1(dynamicRuleAlgorithmField1); + policyAdapter.setDynamicRuleAlgorithmField2(dynamicRuleAlgorithmField2); + policyAdapter.setDynamicVariableList(dynamicVariableList); + policyAdapter.setDynamicSettingsMap(settingsMap); + policyAdapter.setDataTypeList(dataTypeList); + + policyAdapter.setDynamicFieldConfigAttributes(attributeMap); + policyAdapter.setRainydayMap(treatmentMap); + policyAdapter.setRainyday(rainyday); + + component = new DecisionPolicy(policyAdapter); + + logger.info("setUp: exit"); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + } + + /** + * Test method for {@link org.openecomp.policy.pap.xacml.rest.components.DecisionPolicy#savePolicies()}. + */ + @Test + public void testSavePolicies() { + DecisionPolicy mockDecision = Mockito.mock(component.getClass()); + + Map successMap = new HashMap<>(); + successMap.put("success", "success"); + + try { + when(mockDecision.savePolicies()).thenReturn(successMap); + successMap = mockDecision.savePolicies(); + } catch (Exception e) { + e.printStackTrace(); + } + + assertEquals(successMap.get("success"),"success"); + } + + /** + * Test method for {@link org.openecomp.policy.pap.xacml.rest.components.DecisionPolicy#prepareToSave()}. + */ + @Test + public void testPrepareToSaveRainyDay() { + logger.debug("test PrepareToSave Policy: enter"); + policyAdapter.setRuleProvider("Rainy_Day"); + component = new DecisionPolicy(policyAdapter); + boolean response = false; + + try { + response = component.prepareToSave(); + } catch (Exception e) { + logger.error("Exception Occured"+e); + } + assertTrue(response); + } + + /** + * Test method for {@link org.openecomp.policy.pap.xacml.rest.components.DecisionPolicy#prepareToSave()}. + */ + @Test + public void testPrepareToSaveCustom() { + logger.debug("test PrepareToSave Policy: enter"); + policyAdapter.setRuleProvider("Custom"); + component = new DecisionPolicy(policyAdapter); + boolean response = false; + + try { + response = component.prepareToSave(); + } catch (Exception e) { + logger.error("Exception Occured"+e); + } + assertTrue(response); + } + + +} \ No newline at end of file diff --git a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/components/FirewallConfigPolicyTest.java b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/components/FirewallConfigPolicyTest.java new file mode 100644 index 000000000..aed09ce61 --- /dev/null +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/components/FirewallConfigPolicyTest.java @@ -0,0 +1,128 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-PAP-REST + * ================================================================================ + * Copyright (C) 2017 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. + * ============LICENSE_END========================================================= + */ +package org.onap.policy.pap.xacml.rest.components; + +import static org.junit.Assert.*; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + +import java.util.HashMap; + +import java.util.Map; +import java.util.UUID; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; +import org.onap.policy.rest.adapter.PolicyRestAdapter; + +import com.att.research.xacml.util.XACMLProperties; + + +public class FirewallConfigPolicyTest { + + private static Logger logger = FlexLogger.getLogger(FirewallConfigPolicyTest.class); + PolicyRestAdapter policyAdapter = new PolicyRestAdapter(); + Map attributeMap = new HashMap<>(); + FirewallConfigPolicy component = null; + FirewallConfigPolicy mockFWConfig = null; + + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + logger.info("setup: enter"); + System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME,"xacml.pap.properties"); + + policyAdapter.setPolicyName("FWjunitTest"); + policyAdapter.setPolicyDescription("test"); + policyAdapter.setRuleCombiningAlgId("urn:oasis:names:tc:xacml:3.0:rule-combining-algorithm:permit-overrides"); + policyAdapter.setPolicyType("Config"); + policyAdapter.setConfigPolicyType("Firewall Config"); + policyAdapter.setEditPolicy(false); + policyAdapter.setDomainDir("src/test/resources/client.properties"); + policyAdapter.setDomain("Test"); + policyAdapter.setNewFileName("Test.Config_FW_junitTest.1.xml"); + policyAdapter.setHighestVersion(1); + policyAdapter.setVersion(String.valueOf(1)); + policyAdapter.setPolicyID("urn:xacml:policy:id:"+UUID.randomUUID()); + policyAdapter.setRuleID(""); + policyAdapter.setConfigName("testname"); + policyAdapter.setGuard("True"); + policyAdapter.setRiskLevel("3"); + policyAdapter.setRiskType("RiskTest"); + policyAdapter.setSecurityZone("CraigSecurityZone"); + policyAdapter.setUserId("API"); + + attributeMap.put("testJunits", "test"); + policyAdapter.setDynamicFieldConfigAttributes(attributeMap); + + component = new FirewallConfigPolicy(policyAdapter); + mockFWConfig = Mockito.mock(FirewallConfigPolicy.class); + logger.info("setUp: exit"); + + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + } + + /** + * Test method for {@link org.openecomp.policy.pap.xacml.rest.components.FirewallConfigPolicy#savePolicies()}. + */ + @Test + public void testSavePolicies() { + Map successMap = new HashMap<>(); + successMap.put("success", "success"); + try { + when(mockFWConfig.savePolicies()).thenReturn(successMap); + successMap = mockFWConfig.savePolicies(); + } catch (Exception e) { + logger.error("Exception Occured"+e); + } + } + + /** + * Test method for {@link org.openecomp.policy.pap.xacml.rest.components.FirewallConfigPolicy#prepareToSave()}. + */ + @Test + public void testPrepareToSave() { + logger.debug("test prepareToSave Policy: enter"); + + boolean response = false; + try { + when(mockFWConfig.prepareToSave()).thenReturn(true); + response = mockFWConfig.prepareToSave(); + } catch (Exception e) { + logger.error("Exception Occured"+e); + } + assertTrue(response); + + } + +} \ No newline at end of file diff --git a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/controller/DecisionPolicyDictionaryControllerTest.java b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/controller/DecisionPolicyDictionaryControllerTest.java new file mode 100644 index 000000000..529047181 --- /dev/null +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/controller/DecisionPolicyDictionaryControllerTest.java @@ -0,0 +1,273 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-PAP-REST + * ================================================================================ + * Copyright (C) 2017 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. + * ============LICENSE_END========================================================= + */ +package org.onap.policy.pap.xacml.rest.controller; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.BufferedReader; +import java.io.StringReader; +import java.io.UnsupportedEncodingException; + +import javax.servlet.http.HttpServletRequest; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; +import org.onap.policy.rest.dao.CommonClassDao; +import org.springframework.mock.web.MockHttpServletResponse; + +public class DecisionPolicyDictionaryControllerTest { + private static Logger logger = FlexLogger.getLogger(DecisionPolicyDictionaryControllerTest.class); + private static CommonClassDao commonClassDao; + private String jsonString = null; + private HttpServletRequest request = null; + private DecisionPolicyDictionaryController controller = null; + + @Before + public void setUp() throws Exception { + logger.info("setUp: Entering"); + commonClassDao = Mockito.mock(CommonClassDao.class); + HttpServletRequest request = Mockito.mock(HttpServletRequest.class); + + jsonString = "{\"attributeDictionaryDatas\": {\"error\": \"\", \"inprocess\": false,\"model\": {\"name\": \"testingdata\", " + + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", " + + " \"version\": \"\",\"createdBy\": \"someone\", \"modifiedBy\": \"someone\", \"content\": \"\",\"recursive\": false}," + + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\" }," + + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", " + + " \"policyDescription\": \"testing input\", \"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\"," + + " \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, " + + " \"policyJSON\": {\"pmTableName\": \"test\", \"dmdTopic\": \"1\",\"fileId\": \"56\"} }"; + + BufferedReader br = new BufferedReader(new StringReader(jsonString)); + + //--- mock the getReader() call + when(request.getReader()).thenReturn(br); + + controller = new DecisionPolicyDictionaryController(commonClassDao); + + logger.info("setUp: exit"); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetSettingsDictionaryByNameEntityData() { + logger.info("testGetSettingsDictionaryByNameEntityData: Entering"); + + MockHttpServletResponse response = new MockHttpServletResponse(); + + controller.getSettingsDictionaryByNameEntityData(request, response); + + try { + assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("settingsDictionaryDatas")); + logger.info("response.getContentAsString(): " + response.getContentAsString()); + } catch (UnsupportedEncodingException e) { + logger.error("Exception Occured"+e); + fail("Exception: " + e); + } + + logger.info("testGetSettingsDictionaryByNameEntityData: exit"); + + } + + @Test + public void testGetSettingsDictionaryEntityData() { + logger.info("testGetSettingsDictionaryEntityData: Entering"); + + MockHttpServletResponse response = new MockHttpServletResponse(); + + controller.getSettingsDictionaryEntityData(response); + + try { + assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("settingsDictionaryDatas")); + logger.info("response.getContentAsString(): " + response.getContentAsString()); + } catch (UnsupportedEncodingException e) { + logger.error("Exception Occured"+e); + fail("Exception: " + e); + } + + logger.info("testGetSettingsDictionaryEntityData: exit"); + + } + + @Test + public void testSaveSettingsDictionary() { + logger.info("testSaveSettingsDictionary: Entering"); + + MockHttpServletResponse response = new MockHttpServletResponse(); + request = mock(HttpServletRequest.class); + + try { + // mock the getReader() call + jsonString = "{\"settingsDictionaryData\":{\"xacmlId\":\"testMMRestAPI1\",\"datatypeBean\":{\"shortName\":\"string\"},\"description\":\"test\",\"priority\":\"High\"}, \"userid\":\"test\"}"; + + BufferedReader br = new BufferedReader(new StringReader(jsonString)); + when(request.getReader()).thenReturn(br); + controller.saveSettingsDictionary(request, response); + logger.info("response.getContentAsString(): " + response.getContentAsString()); + assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("settingsDictionaryDatas")); + + } catch (Exception e) { + logger.error("Exception Occured"+e); + fail("Exception: " + e); + } + + logger.info("testSaveSettingsDictionary: exit"); + + } + + @Test + public void testRemoveSettingsDictionary() { + logger.info("testRemoveSettingsDictionary: Entering"); + + MockHttpServletResponse response = new MockHttpServletResponse(); + request = mock(HttpServletRequest.class); + + try { + // mock the getReader() call + jsonString = "{\"data\": {\"modelName\": \"test\", \"inprocess\": false,\"model\": {\"name\": \"testingdata\", " + + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", " + + " \"version\": \"\",\"createdBy\": \"someone\", \"modifiedBy\": \"someone\", \"content\": \"\",\"recursive\": false}," + + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\" }," + + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", " + + " \"policyDescription\": \"testing input\", \"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\"," + + " \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, " + + " \"policyJSON\": {\"pmTableName\": \"test\", \"dmdTopic\": \"1\",\"fileId\": \"56\"} }"; + BufferedReader br = new BufferedReader(new StringReader(jsonString)); + when(request.getReader()).thenReturn(br); + controller.removeSettingsDictionary(request, response); + logger.info("response.getContentAsString(): " + response.getContentAsString()); + assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("settingsDictionaryDatas")); + + } catch (Exception e) { + logger.error("Exception Occured"+e); + fail("Exception: " + e); + } + + logger.info("testRemoveSettingsDictionary: exit"); + + } + + @Test + public void testGetRainyDayDictionaryByNameEntityData() { + logger.info("testGetRainyDayDictionaryByNameEntityData: Entering"); + + MockHttpServletResponse response = new MockHttpServletResponse(); + + controller.getRainyDayDictionaryByNameEntityData(request, response); + + try { + assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("rainyDayDictionaryDatas")); + logger.info("response.getContentAsString(): " + response.getContentAsString()); + } catch (UnsupportedEncodingException e) { + logger.error("Exception Occured"+e); + fail("Exception: " + e); + } + + logger.info("testGetRainyDayDictionaryByNameEntityData: exit"); + + } + + @Test + public void testGetRainyDayDictionaryEntityData() { + logger.info("testGetRainyDayDictionaryEntityData: Entering"); + + MockHttpServletResponse response = new MockHttpServletResponse(); + + controller.getRainyDayDictionaryEntityData(request, response); + + try { + assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("rainyDayDictionaryDatas")); + logger.info("response.getContentAsString(): " + response.getContentAsString()); + } catch (UnsupportedEncodingException e) { + logger.error("Exception Occured"+e); + fail("Exception: " + e); + } + + logger.info("testGetRainyDayDictionaryEntityData: exit"); + + } + + @Test + public void testSaveRainyDayDictionary() { + logger.info("testSaveRainyDayDictionary: Entering"); + + MockHttpServletResponse response = new MockHttpServletResponse(); + request = mock(HttpServletRequest.class); + + try { + // mock the getReader() call + jsonString = "{\"rainyDayDictionaryData\":{\"bbid\":\"BB2\",\"workstep\":\"1\",\"userDataTypeValues\":[{\"$$hashKey\":\"object:233\",\"treatment\":\"test1\"},{\"$$hashKey\":\"object:239\",\"treatment\":\"test2\"}]},\"userid\":\"mm117s\"}"; + + BufferedReader br = new BufferedReader(new StringReader(jsonString)); + when(request.getReader()).thenReturn(br); + controller.saveRainyDayDictionary(request, response); + logger.info("response.getContentAsString(): " + response.getContentAsString()); + assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("rainyDayDictionaryDatas")); + + } catch (Exception e) { + logger.error("Exception Occured"+e); + fail("Exception: " + e); + } + + logger.info("testSaveRainyDayDictionary: exit"); + + } + + @Test + public void testRemoveRainyDayDictionary() { + logger.info("testRemoveRainyDayDictionary: Entering"); + + MockHttpServletResponse response = new MockHttpServletResponse(); + request = mock(HttpServletRequest.class); + + try { + // mock the getReader() call + jsonString = "{\"data\": {\"modelName\": \"test\", \"inprocess\": false,\"model\": {\"name\": \"testingdata\", " + + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", " + + " \"version\": \"\",\"createdBy\": \"someone\", \"modifiedBy\": \"someone\", \"content\": \"\",\"recursive\": false}," + + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\" }," + + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", " + + " \"policyDescription\": \"testing input\", \"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\"," + + " \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, " + + " \"policyJSON\": {\"pmTableName\": \"test\", \"dmdTopic\": \"1\",\"fileId\": \"56\"} }"; + BufferedReader br = new BufferedReader(new StringReader(jsonString)); + when(request.getReader()).thenReturn(br); + controller.removeRainyDayDictionary(request, response); + logger.info("response.getContentAsString(): " + response.getContentAsString()); + assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("rainyDayDictionaryDatas")); + + } catch (Exception e) { + logger.error("Exception Occured"+e); + fail("Exception: " + e); + } + + logger.info("testRemoveRainyDayDictionary: exit"); + + } + +} \ No newline at end of file -- 2.16.6