diff options
author | bobbymander <bobby.mander@att.com> | 2019-10-31 15:08:36 -0400 |
---|---|---|
committer | bobbymander <bobby.mander@att.com> | 2019-11-05 09:21:05 -0500 |
commit | f0d99cbc1c5969a6c51af5fe6dcfe546d3a6df66 (patch) | |
tree | 148cd98a72baec013c8c62c95dbf48afb75d9e23 | |
parent | 46b5b531eada52e963f9ca196473a6c1bd8cbf37 (diff) |
More JUnit additions for PAP-REST
Issue-ID: POLICY-2130
Change-Id: Ia64bdbb377d26ba209b2d3fc51bc95f5769a7729
Signed-off-by: bobbymander <bobby.mander@att.com>
6 files changed, 215 insertions, 17 deletions
diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/NotifyOtherPaps.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/NotifyOtherPaps.java index c3171de39..cd290c66c 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/NotifyOtherPaps.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/NotifyOtherPaps.java @@ -22,7 +22,6 @@ package org.onap.policy.pap.xacml.rest.components; import com.att.research.xacml.util.XACMLProperties; - import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; @@ -33,7 +32,6 @@ import java.util.Base64; import java.util.LinkedList; import java.util.List; import java.util.UUID; - import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.rest.XacmlRestProperties; @@ -58,7 +56,7 @@ public class NotifyOtherPaps { */ public void notifyOthers(long entityId, String entityType, String newGroupId) { LOGGER.debug("notifyOthers(long entityId, String entityType, long newGroupId) as notifyOthers(" + entityId + "," - + entityType + "," + newGroupId + ") called"); + + entityType + "," + newGroupId + ") called"); failedPaps = new ArrayList<>(); List<?> otherServers = PolicyDbDao.getPolicyDbDaoInstance().getOtherServers(); @@ -70,7 +68,7 @@ public class NotifyOtherPaps { } } - private void startNotifyThreads(List<?> otherServers, long entityId, String entityType, String newGroupId) { + protected void startNotifyThreads(List<?> otherServers, long entityId, String entityType, String newGroupId) { LinkedList<Thread> notifyThreads = new LinkedList<>(); // we're going to run notifications in parallel threads to speed things // up @@ -144,11 +142,11 @@ public class NotifyOtherPaps { return; } if (newGroupId == null) { - url = new URL(otherPap + "?policydbdaourl=" + papUrl + "&entityid=" + entityId + "&entitytype=" - + entityType); + url = new URL( + otherPap + "?policydbdaourl=" + papUrl + "&entityid=" + entityId + "&entitytype=" + entityType); } else { url = new URL(otherPap + "?policydbdaourl=" + papUrl + "&entityid=" + entityId + "&entitytype=" - + entityType + "&extradata=" + newGroupId); + + entityType + "&extradata=" + newGroupId); } } catch (MalformedURLException e) { LOGGER.warn("Caught MalformedURLException on: new URL()", e); @@ -188,7 +186,7 @@ public class NotifyOtherPaps { int readTimeout; try { readTimeout = - Integer.parseInt(XACMLProperties.getProperty(XacmlRestProperties.PROP_PAP_NOTIFY_TIMEOUT)); + Integer.parseInt(XACMLProperties.getProperty(XacmlRestProperties.PROP_PAP_NOTIFY_TIMEOUT)); } catch (Exception e) { LOGGER.error("xacml.rest.pap.notify.timeoutms property not set, using a default.", e); readTimeout = 10000; @@ -217,7 +215,7 @@ public class NotifyOtherPaps { LOGGER.info("PolicyDBDao: NotifyOtherThread received response 200 from pap server on notify"); } else { LOGGER.warn("PolicyDBDao: NotifyOtherThread connection response code not 200, received: " - + connection.getResponseCode()); + + connection.getResponseCode()); failedPaps.add(dbdEntity); } } catch (Exception e) { diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/service/ImportService.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/service/ImportService.java index d4801dfcf..050bcc561 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/service/ImportService.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/service/ImportService.java @@ -30,10 +30,9 @@ import java.io.Writer; import java.util.HashMap; import java.util.Map; import java.util.UUID; - +import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; - import org.onap.policy.common.logging.eelf.PolicyLogger; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; @@ -74,7 +73,8 @@ public class ImportService { StringBuilder builder = new StringBuilder(); int ch; try { - while ((ch = request.getInputStream().read()) != -1) { + ServletInputStream stream = request.getInputStream(); + while ((ch = stream.read()) != -1) { builder.append((char) ch); } } catch (IOException e) { @@ -115,7 +115,7 @@ public class ImportService { } PolicyLogger.info("Request from API to import new Service"); try (Writer writer = new BufferedWriter(new OutputStreamWriter( - new FileOutputStream(extractDir + File.separator + randomID + type), "utf-8"))) { + new FileOutputStream(extractDir + File.separator + randomID + type), "utf-8"))) { writer.write(xmi); } catch (IOException e) { logger.error(e); @@ -125,7 +125,7 @@ public class ImportService { } else { InputStream inputStream = null; try (FileOutputStream outputStream = - new FileOutputStream(extractDir + File.separator + randomID + ".zip")) { + new FileOutputStream(extractDir + File.separator + randomID + ".zip")) { inputStream = request.getInputStream(); byte[] buffer = new byte[4096]; int bytesRead = -1; @@ -178,7 +178,7 @@ public class ImportService { } PolicyLogger.info("Request from API to import new Optimization Service Model"); try (Writer writer = new BufferedWriter(new OutputStreamWriter( - new FileOutputStream(extractDir + File.separator + randomID + type), "utf-8"))) { + new FileOutputStream(extractDir + File.separator + randomID + type), "utf-8"))) { writer.write(yml); } catch (IOException e) { logger.error(e); 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 new file mode 100644 index 000000000..13fb81d17 --- /dev/null +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/HeartbeatTest.java @@ -0,0 +1,41 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-PAP-REST + * ================================================================================ + * Copyright (C) 2019 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; + +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 org.junit.Test; + +public class HeartbeatTest { + @Test + public void testHeartbeat() throws IOException, PAPException { + Heartbeat hb = new Heartbeat(null); + + assertThatThrownBy(hb::run).isInstanceOf(NullPointerException.class); + assertTrue(hb.isHeartBeatRunning()); + hb.terminate(); + assertFalse(hb.isHeartBeatRunning()); + } +} diff --git a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/UpdatePdpThreadTest.java b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/UpdatePdpThreadTest.java new file mode 100644 index 000000000..9034f393b --- /dev/null +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/UpdatePdpThreadTest.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-PAP-REST + * ================================================================================ + * Copyright (C) 2019 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; + +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.junit.Assert.assertNotNull; + +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; +import org.junit.Test; +import org.onap.policy.common.logging.OnapLoggingContext; +import org.onap.policy.xacml.api.pap.OnapPDP; +import org.onap.policy.xacml.std.pap.StdPDP; + +public class UpdatePdpThreadTest { + @Test + public void testConstructor1() { + UpdatePdpThread thread = new UpdatePdpThread(null, null); + assertNotNull(thread); + } + + @Test + public void testConstructor2() { + OnapLoggingContext loggingContext = new OnapLoggingContext(); + loggingContext.setRequestId("id"); + UpdatePdpThread thread = new UpdatePdpThread(null, loggingContext, null); + assertNotNull(thread); + } + + @Test + public void testRun() { + OnapPDP pdp = new StdPDP("http://id", 0); + OnapLoggingContext loggingContext = new OnapLoggingContext(); + loggingContext.setRequestId("id"); + List<Properties> properties = new ArrayList<Properties>(); + UpdatePdpThread thread = new UpdatePdpThread(pdp, loggingContext, properties); + assertThatCode(thread::run).doesNotThrowAnyException(); + } +} diff --git a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/components/NotifyOtherPapsTest.java b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/components/NotifyOtherPapsTest.java new file mode 100644 index 000000000..50c166aed --- /dev/null +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/components/NotifyOtherPapsTest.java @@ -0,0 +1,66 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-PAP-REST + * ================================================================================ + * Copyright (C) 2018-2019 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.assertj.core.api.Assertions.assertThatCode; + +import com.att.research.xacml.util.XACMLProperties; +import java.util.ArrayList; +import java.util.List; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.rest.jpa.PolicyDBDaoEntity; + +public class NotifyOtherPapsTest { + private static final String systemKey = XACMLProperties.XACML_PROPERTIES_NAME; + private static final String oldProperty = System.getProperty(systemKey); + + @Before + public void setup() { + // Set the system property temporarily + System.setProperty(systemKey, "xacml.pap.properties"); + } + + @Test + public void negTestNotify() { + NotifyOtherPaps notify = new NotifyOtherPaps(); + List<PolicyDBDaoEntity> otherServers = new ArrayList<PolicyDBDaoEntity>(); + PolicyDBDaoEntity dbdaoEntity = new PolicyDBDaoEntity(); + dbdaoEntity.setPolicyDBDaoUrl("http://test"); + otherServers.add(dbdaoEntity); + long entityId = 0; + String entityType = "entityType"; + String newGroupId = "newGroupId"; + assertThatCode(() -> notify.startNotifyThreads(otherServers, entityId, entityType, newGroupId)) + .doesNotThrowAnyException(); + } + + @After + public void cleanup() { + // Restore the original system property + if (oldProperty != null) { + System.setProperty(systemKey, oldProperty); + } else { + System.clearProperty(systemKey); + } + } +} 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 72d492620..3b826f694 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 @@ -20,14 +20,14 @@ package org.onap.policy.pap.xacml.rest.service; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import com.mockrunner.mock.web.MockHttpServletRequest; import com.mockrunner.mock.web.MockHttpServletResponse; - import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; - import org.junit.Test; public class ImportServiceTest { @@ -39,4 +39,39 @@ public class ImportServiceTest { service.doImportMicroServicePut(request, response); assertEquals(response.getHeader("error"), "missing"); } + + @Test + public void testImportBRMS() { + ImportService service = new ImportService(); + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setupAddParameter("serviceName", "serviceName"); + request.setupAddParameter("importService", "BRMSPARAM"); + request.setBodyContent("foo"); + HttpServletResponse response = new MockHttpServletResponse(); + assertThatCode(() -> service.doImportMicroServicePut(request, response)).doesNotThrowAnyException(); + } + + @Test + public void testImportMS() { + ImportService service = new ImportService(); + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setupAddParameter("serviceName", "serviceName"); + request.setupAddParameter("importService", "MICROSERVICE"); + request.setupAddParameter("fileName", "fileName"); + request.setBodyContent("foo"); + HttpServletResponse response = new MockHttpServletResponse(); + assertThatThrownBy(() -> service.doImportMicroServicePut(request, response)).isInstanceOf(Exception.class); + } + + @Test + public void testImportOpt() { + ImportService service = new ImportService(); + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setupAddParameter("serviceName", "serviceName"); + request.setupAddParameter("importService", "OPTIMIZATION"); + request.setupAddParameter("fileName", "fileName"); + request.setBodyContent("foo"); + HttpServletResponse response = new MockHttpServletResponse(); + assertThatThrownBy(() -> service.doImportMicroServicePut(request, response)).isInstanceOf(Exception.class); + } } |