diff options
author | bobbymander <bobby.mander@att.com> | 2019-11-13 10:30:56 -0500 |
---|---|---|
committer | bobbymander <bobby.mander@att.com> | 2019-11-13 14:30:07 -0500 |
commit | 577b34e9bdad94d0b1559a9917af3283124aeeda (patch) | |
tree | 8e6defb4c95f2abe4873400b3a8c7698b1bf3b08 /ONAP-PAP-REST/src/test/java | |
parent | 0c20d1c294fe146e1018f14b07a8d861c29fe527 (diff) |
Few JUnit additions for PAP-REST
Issue-ID: POLICY-2130
Change-Id: Ie9972bc0014def93acf04c4a2386585fb4accdef
Signed-off-by: bobbymander <bobby.mander@att.com>
Diffstat (limited to 'ONAP-PAP-REST/src/test/java')
5 files changed, 223 insertions, 28 deletions
diff --git a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/HeartbeatTest.java b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/HeartbeatTest.java index 13fb81d17..33b7f8be6 100644 --- a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/HeartbeatTest.java +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/HeartbeatTest.java @@ -20,13 +20,24 @@ package org.onap.policy.pap.xacml.rest; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import com.att.research.xacml.api.pap.PAPException; import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashSet; +import java.util.Set; import org.junit.Test; +import org.mockito.Mockito; +import org.onap.policy.xacml.api.pap.OnapPDP; +import org.onap.policy.xacml.api.pap.OnapPDPGroup; +import org.onap.policy.xacml.api.pap.PAPPolicyEngine; +import org.onap.policy.xacml.std.pap.StdPDP; +import org.onap.policy.xacml.std.pap.StdPDPGroup; public class HeartbeatTest { @Test @@ -38,4 +49,43 @@ public class HeartbeatTest { hb.terminate(); assertFalse(hb.isHeartBeatRunning()); } + + @Test + public void testGetPdps() throws PAPException, IOException { + Set<OnapPDPGroup> pdpGroups = new HashSet<OnapPDPGroup>(); + StdPDPGroup pdpGroup = new StdPDPGroup(); + OnapPDP pdp = new StdPDP(); + pdpGroup.addPDP(pdp); + pdpGroups.add(pdpGroup); + PAPPolicyEngine pap = Mockito.mock(PAPPolicyEngine.class); + Mockito.when(pap.getOnapPDPGroups()).thenReturn(pdpGroups); + Heartbeat hb = new Heartbeat(pap); + hb.getPdpsFromGroup(); + assertFalse(hb.isHeartBeatRunning()); + + assertThatCode(hb::notifyEachPdp).doesNotThrowAnyException(); + assertThatThrownBy(hb::run).isInstanceOf(Exception.class); + assertThatThrownBy(hb::notifyEachPdp).isInstanceOf(Exception.class); + } + + @Test + public void testOpen() throws MalformedURLException { + Heartbeat hb = new Heartbeat(null); + OnapPDP pdp = new StdPDP(); + + assertThatCode(() -> { + URL url = new URL("http://onap.org"); + hb.openPdpConnection(url, pdp); + }).doesNotThrowAnyException(); + + assertThatCode(() -> { + URL url = new URL("http://1.2.3.4"); + hb.openPdpConnection(url, pdp); + }).doesNotThrowAnyException(); + + assertThatCode(() -> { + URL url = new URL("http://fakesite.fakenews"); + hb.openPdpConnection(url, pdp); + }).doesNotThrowAnyException(); + } } diff --git a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/components/BRMSPolicyTest.java b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/components/BRMSPolicyTest.java index 333d878ca..5c1d3dd76 100644 --- a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/components/BRMSPolicyTest.java +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/components/BRMSPolicyTest.java @@ -22,15 +22,22 @@ package org.onap.policy.pap.xacml.rest.components; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import com.att.research.xacml.api.pap.PAPException; import java.io.IOException; - +import java.nio.charset.Charset; +import java.util.HashMap; +import java.util.Map; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.mockito.Mockito; +import org.onap.policy.rest.adapter.PolicyRestAdapter; import org.onap.policy.rest.dao.CommonClassDao; public class BRMSPolicyTest { @@ -68,4 +75,42 @@ public class BRMSPolicyTest { String userID = "testID"; assertEquals(1, template.addRule(rule, ruleName, description, userID).size()); } + + @Test + public void testCreateBrmsParamPolicyAdapter() throws PAPException { + Map<String, String> brmsParamBody = new HashMap<String, String>(); + brmsParamBody.put("key", "value"); + + PolicyRestAdapter adapter = new PolicyRestAdapter(); + adapter.setHighestVersion(1); + adapter.setPolicyType("Config"); + adapter.setBrmsParamBody(brmsParamBody); + adapter.setNewFileName("policyName.1.xml"); + Map<String, String> dynamicFieldConfigAttributes = new HashMap<String, String>(); + dynamicFieldConfigAttributes.put("key", "value"); + adapter.setDynamicFieldConfigAttributes(dynamicFieldConfigAttributes); + CreateBrmsParamPolicy policy = new CreateBrmsParamPolicy(adapter); + String ruleContents = "contents"; + + assertThatCode(() -> policy.saveConfigurations("name.xml", "rules")).doesNotThrowAnyException(); + try { + policy.prepareToSave(); + policy.savePolicies(); + } catch (Exception ex) { + // Ignore + } + + assertThatThrownBy(() -> policy.expandConfigBody(ruleContents, brmsParamBody)) + .isInstanceOf(NullPointerException.class); + assertTrue(policy.validateConfigForm()); + policy.getAdviceExpressions(1, "name.1.xml"); + assertNotNull(policy.getCorrectPolicyDataObject()); + } + + @Test + public void testRead() { + Charset encoding = Charset.defaultCharset(); + assertThatCode(() -> CreateBrmsParamPolicy.readFile("xacml.pap.properties", encoding)) + .doesNotThrowAnyException(); + } } diff --git a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/controller/OptimizationDictionaryControllerTest.java b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/controller/OptimizationDictionaryControllerTest.java index 451989c1f..d990b9002 100644 --- a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/controller/OptimizationDictionaryControllerTest.java +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/controller/OptimizationDictionaryControllerTest.java @@ -22,16 +22,20 @@ package org.onap.policy.pap.xacml.rest.controller; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import com.mockrunner.mock.web.MockHttpServletRequest; import java.io.BufferedReader; import java.io.StringReader; import javax.servlet.http.HttpServletRequest; - +import javax.ws.rs.core.Response; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; @@ -72,16 +76,16 @@ public class OptimizationDictionaryControllerTest { HttpServletRequest request = Mockito.mock(HttpServletRequest.class); jsonString = "{\"optimizationModelsDictionaryData\": {\"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\"," - + "\"onapName\": \"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\"}}"; + + " {\"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\"," + + "\"onapName\": \"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\"}}"; br = new BufferedReader(new StringReader(jsonString)); // --- mock the getReader() call @@ -108,7 +112,7 @@ public class OptimizationDictionaryControllerTest { controller.getOptimizationModelsDictionaryEntityData(response); logger.info("response.getContentAsString(): " + response.getContentAsString()); assertTrue(response.getContentAsString() != null - && response.getContentAsString().contains("optimizationModelsDictionaryDatas")); + && response.getContentAsString().contains("optimizationModelsDictionaryDatas")); } catch (Exception e) { fail("Exception: " + e); @@ -130,7 +134,7 @@ public class OptimizationDictionaryControllerTest { controller.saveOptimizationModelsDictionary(request, response); logger.info("response.getContentAsString(): " + response.getContentAsString()); assertTrue(response.getContentAsString() != null - && response.getContentAsString().contains("optimizationModelsDictionaryDatas")); + && response.getContentAsString().contains("optimizationModelsDictionaryDatas")); } catch (Exception e) { fail("Exception: " + e); @@ -149,28 +153,59 @@ public class OptimizationDictionaryControllerTest { 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\"," - + "\"onapName\": \"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\"}}"; + "{\"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\"," + + "\"onapName\": \"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.removeOptimizationModelsDictionary(request, response); logger.info("response.getContentAsString(): " + response.getContentAsString()); assertTrue(response.getContentAsString() != null - && response.getContentAsString().contains("optimizationModelsDictionaryDatas")); + && response.getContentAsString().contains("optimizationModelsDictionaryDatas")); } catch (Exception e) { fail("Exception: " + e); } logger.info("testRemoveOptimizationModelsDictionary: exit"); } + + @Test + public void testGet() { + OptimizationDictionaryController controller = new OptimizationDictionaryController(commonClassDao); + MockHttpServletResponse response = new MockHttpServletResponse(); + controller.getOptimizationModelsDictionaryByNameEntityData(response); + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + } + + @Test + public void testSave() { + OptimizationDictionaryController controller = new OptimizationDictionaryController(commonClassDao); + MockHttpServletRequest req = new MockHttpServletRequest(); + MockHttpServletResponse response = new MockHttpServletResponse(); + req.setBodyContent("{\n\"modelType\": \"type.yml\", \"dataOrderInfo\": \"info\", \"userid\": \"id\", " + + "\"optimizationModelsDictionaryData\": {\"description\": \"desc\", \"modelName\": \"name\", \"version\": \"1.0\"}, " + + "\"classMap\": \"{\\\"dep\\\":\\\"{\\\"dependency\\\":\\\"depval\\\"}\\\"}\" }\n"); + // + "\"classMap\": \"{\\\"dep\\\":\\\"dependency\\\"}\" }\n"); + assertThatThrownBy(() -> controller.saveOptimizationModelsDictionary(req, response)) + .isInstanceOf(NullPointerException.class); + + req.setBodyContent("{\n\"modelType\": \"type.xml\", \"dataOrderInfo\": \"info\", \"userid\": \"id\", " + + "\"optimizationModelsDictionaryData\": {\"description\": \"desc\", \"modelName\": \"name\", \"version\": \"1.0\"}, " + + "\"classMap\": \"{\\\"dep\\\": {\\\"dependency\\\":\\\"depval\\\"} }\" }\n"); + assertThatCode(() -> controller.saveOptimizationModelsDictionary(req, response)).doesNotThrowAnyException(); + + req.setupAddParameter("apiflag", "api"); + assertThatThrownBy(() -> controller.saveOptimizationModelsDictionary(req, response)) + .isInstanceOf(NullPointerException.class); + } + } diff --git a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/handler/DeleteHandlerTest.java b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/handler/DeleteHandlerTest.java index a9da00d5a..d0e4416f9 100644 --- a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/handler/DeleteHandlerTest.java +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/handler/DeleteHandlerTest.java @@ -25,11 +25,13 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; import static org.mockito.Mockito.when; import com.mockrunner.mock.web.MockHttpServletRequest; import com.mockrunner.mock.web.MockHttpServletResponse; import java.io.IOException; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.hibernate.Session; @@ -45,6 +47,7 @@ import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl; import org.onap.policy.pap.xacml.rest.elk.client.PolicyElasticSearchController; import org.onap.policy.rest.dao.CommonClassDao; import org.onap.policy.rest.jpa.PolicyEntity; +import org.onap.policy.rest.jpa.PolicyVersion; import org.onap.policy.xacml.api.pap.PAPPolicyEngine; import org.onap.policy.xacml.std.pap.StdEngine; import org.powermock.api.mockito.PowerMockito; @@ -120,12 +123,74 @@ public class DeleteHandlerTest { CommonClassDao dao = Mockito.mock(CommonClassDao.class); DeleteHandler handler = new DeleteHandler(dao); - // Mock request + // Request #1 MockHttpServletRequest request = new MockHttpServletRequest(); request.setBodyContent( "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Config_name.1.xml\", \"deleteCondition\": \"All Versions\"\n}\n"); MockHttpServletResponse response = new MockHttpServletResponse(); + handler.doApiDeleteFromPap(request, response); + assertTrue(response.containsHeader("error")); + + // Request #2 + request.setBodyContent( + "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Action_name.1.xml\", \"deleteCondition\": \"All Versions\"\n}\n"); + handler.doApiDeleteFromPap(request, response); + assertTrue(response.containsHeader("error")); + + // Request #3 + request.setBodyContent( + "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Decision_name.1.xml\", \"deleteCondition\": \"All Versions\"\n}\n"); + handler.doApiDeleteFromPap(request, response); + assertTrue(response.containsHeader("error")); + + // Request #4 + request.setBodyContent( + "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Bar_name.1.xml\", \"deleteCondition\": \"All Versions\"\n}\n"); + handler.doApiDeleteFromPap(request, response); + assertTrue(response.containsHeader("error")); + + // Request #5 + request.setBodyContent( + "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Config_name.1.xml\", \"deleteCondition\": \"Current Version\"\n}\n"); + handler.doApiDeleteFromPap(request, response); + assertTrue(response.containsHeader("error")); + + // Request #6 + request.setBodyContent( + "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Action_name.1.xml\", \"deleteCondition\": \"Current Version\"\n}\n"); + handler.doApiDeleteFromPap(request, response); + assertTrue(response.containsHeader("error")); + + // Request #7 + request.setBodyContent( + "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Decision_name.1.xml\", \"deleteCondition\": \"Current Version\"\n}\n"); + handler.doApiDeleteFromPap(request, response); + assertTrue(response.containsHeader("error")); + + // Mock dao + List<PolicyVersion> pePVs = new ArrayList<PolicyVersion>(); + PolicyVersion pv = new PolicyVersion(); + pePVs.add(pv); + List<Object> peObjs = new ArrayList<Object>(pePVs); + List<PolicyEntity> peEnts = new ArrayList<PolicyEntity>(); + PolicyEntity peEnt = new PolicyEntity(); + peEnts.add(peEnt); + List<Object> peEntObjs = new ArrayList<Object>(peEnts); + Mockito.when(dao.getDataByQuery(eq("Select p from PolicyVersion p where p.policyName=:pname"), any())) + .thenReturn(peObjs); + Mockito.when( + dao.getDataByQuery(eq("SELECT p FROM PolicyEntity p WHERE p.policyName=:pName and p.scope=:pScope"), any())) + .thenReturn(peEntObjs); + + // Request #8 + request.setBodyContent( + "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Decision_name.1.xml\", \"deleteCondition\": \"Current Version\"\n}\n"); + handler.doApiDeleteFromPap(request, response); + assertTrue(response.containsHeader("error")); + // Request #9 + request.setBodyContent( + "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Decision_name.1.xml\", \"deleteCondition\": \"Current Version\"\n, \"deleteCondition\": \"All Versions\"}\n"); handler.doApiDeleteFromPap(request, response); assertTrue(response.containsHeader("error")); } diff --git a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/service/ImportServiceTest.java b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/service/ImportServiceTest.java index 3b826f694..b7d6bacbd 100644 --- a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/service/ImportServiceTest.java +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/service/ImportServiceTest.java @@ -37,7 +37,7 @@ public class ImportServiceTest { HttpServletRequest request = new MockHttpServletRequest(); HttpServletResponse response = new MockHttpServletResponse(); service.doImportMicroServicePut(request, response); - assertEquals(response.getHeader("error"), "missing"); + assertEquals("missing", response.getHeader("error")); } @Test |