aboutsummaryrefslogtreecommitdiffstats
path: root/ECOMP-PDP-REST/src/test/java
diff options
context:
space:
mode:
authorPamela Dragosh <pdragosh@research.att.com>2017-02-14 19:41:00 -0500
committerPamela Dragosh <pdragosh@research.att.com>2017-02-14 19:41:32 -0500
commit91d04c64771832a0b8815ffbe1f0f9920320d94d (patch)
treefb02d5e1c84a3d91def9a7ee95bc87f9c046cc96 /ECOMP-PDP-REST/src/test/java
parentb9d4caa40ef8e3566ac475968bce17b9b64b6939 (diff)
Initial OpenECOMP policy/engine commit
Change-Id: I7dbff37733b661643dd4d1caefa3d7dccc361b6e Signed-off-by: Pamela Dragosh <pdragosh@research.att.com>
Diffstat (limited to 'ECOMP-PDP-REST/src/test/java')
-rw-r--r--ECOMP-PDP-REST/src/test/java/org/openecomp/policy/pdp/rest/PapUrlResolverTest.java355
-rw-r--r--ECOMP-PDP-REST/src/test/java/org/openecomp/policy/pdp/rest/XACMLPdpServletTest.java395
2 files changed, 750 insertions, 0 deletions
diff --git a/ECOMP-PDP-REST/src/test/java/org/openecomp/policy/pdp/rest/PapUrlResolverTest.java b/ECOMP-PDP-REST/src/test/java/org/openecomp/policy/pdp/rest/PapUrlResolverTest.java
new file mode 100644
index 000000000..7a8060114
--- /dev/null
+++ b/ECOMP-PDP-REST/src/test/java/org/openecomp/policy/pdp/rest/PapUrlResolverTest.java
@@ -0,0 +1,355 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ECOMP-PDP-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.openecomp.policy.pdp.rest;
+import org.junit.Before;
+import org.junit.Test;
+import org.openecomp.policy.pdp.rest.PapUrlResolver;
+import org.openecomp.policy.rest.XACMLRestProperties;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Properties;
+
+import org.junit.Assert;
+
+public class PapUrlResolverTest {
+
+
+ @Test
+ public void testPropertyModifications(){
+ long currentTime = new Date().getTime();
+ DateFormat df = new SimpleDateFormat();
+ //first sort order
+ String urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
+ String failed = "-1,-1,-1,-1";
+ String succeeded = "-1,-1,-1,-1";
+ PapUrlResolver rs = PapUrlResolver.getInstance(urls, failed, succeeded);
+ Assert.assertEquals(rs.getProperties().getProperty(XACMLRestProperties.PROP_PAP_URLS),urls);
+
+ rs.failed();
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ rs.succeeded();
+ Assert.assertFalse(rs.hasMoreUrls());
+ Properties prop = rs.getProperties();
+ Assert.assertEquals(df.format(new Date())+",-1,-1,-1",prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS));
+ Assert.assertEquals("-1,"+df.format(new Date())+",-1,-1",prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS));
+
+ failed = prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS);
+ succeeded = prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS);
+ rs = PapUrlResolver.getInstance(urls, failed, succeeded);
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://two.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://three.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://four.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://one.localhost.com", rs.getUrl());
+ rs.succeeded();
+ rs.getNext();
+ Assert.assertFalse(rs.hasMoreUrls());
+ prop = rs.getProperties();
+ Assert.assertEquals("-1,-1,-1,-1",prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS));
+
+ failed = prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS);
+ succeeded = prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS);
+ rs = PapUrlResolver.getInstance(urls, failed, succeeded);
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://one.localhost.com", rs.getUrl());
+ rs.succeeded();
+ rs.getNext();
+ Assert.assertFalse(rs.hasMoreUrls());
+ prop = rs.getProperties();
+ failed = prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS);
+ succeeded = prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS);
+ rs = PapUrlResolver.getInstance(urls, failed, succeeded);
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://one.localhost.com", rs.getUrl());
+ rs.succeeded();
+ rs.getNext();
+ Assert.assertFalse(rs.hasMoreUrls());
+ prop = rs.getProperties();
+ failed = prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS);
+ succeeded = prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS);
+ rs = PapUrlResolver.getInstance(urls, failed, succeeded);
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://one.localhost.com", rs.getUrl());
+ rs.succeeded();
+ rs.getNext();
+ Assert.assertFalse(rs.hasMoreUrls());
+
+
+ prop = rs.getProperties();
+ succeeded = prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS);
+ System.out.println(succeeded);
+ //can't test this in a timely manner since it will need to wait a minute, since they all succeeded at the same time
+ /*
+ prop = rs.getProperties();
+ failed = prop.getProperty(XACMLRestProperties.PROP_PAP_FAILED_URLS);
+ succeeded = prop.getProperty(XACMLRestProperties.PROP_PAP_SUCCEEDED_URLS);
+ rs = PapUrlResolver.getInstance(urls, failed, succeeded);
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://two.localhost.com", rs.getUrl());
+ rs.succeeded();
+ rs.getNext();
+ Assert.assertFalse(rs.hasMoreUrls());
+ */
+ }
+ @Test
+ public void testModifyUrl(){
+ String newUrl = PapUrlResolver.modifyUrl("http://mypap1.com/pap?id=987654", "http://mypap2.com:45/pap/");
+ int u = 8;
+ }
+
+ @Test
+ public void testSorts(){
+ long currentTime = new Date().getTime();
+ DateFormat df = new SimpleDateFormat();
+ //first sort order
+ String urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
+ String failed = df.format(new Date(currentTime-3600000))+",-1,-1,"+df.format(new Date(currentTime-3200000));
+ String succeeded = "-1,8/13/15 5:41 PM,8/13/15 4:41 PM,-1";
+ PapUrlResolver rs = PapUrlResolver.getInstance(urls, failed, succeeded);
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://two.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://three.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://one.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://four.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertFalse(rs.hasMoreUrls());
+
+ urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
+ failed = "-1,-1,-1,-1";
+ succeeded = "-1,"+df.format(new Date(currentTime-3600000))+","+df.format(new Date(currentTime-6600000))+",-1";
+ rs = PapUrlResolver.getInstance(urls, failed, succeeded);
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://one.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://two.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://three.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://four.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertFalse(rs.hasMoreUrls());
+
+ urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
+ failed = "-1,-1,-1,-1";
+ succeeded = "-1,-1,-1,-1";
+ rs = PapUrlResolver.getInstance(urls, failed, succeeded);
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://one.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://two.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://three.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://four.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertFalse(rs.hasMoreUrls());
+
+ urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
+ failed = df.format(new Date(currentTime-3900000))+","+df.format(new Date(currentTime-5600000))+","+df.format(new Date(currentTime-4600000))+","+df.format(new Date(currentTime-3600000));
+ succeeded = "-1,-1,-1,-1";
+ rs = PapUrlResolver.getInstance(urls, failed, succeeded);
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://two.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://three.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://one.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://four.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertFalse(rs.hasMoreUrls());
+
+ urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
+ failed = df.format(new Date(currentTime-(3600000*4)))+",-1,-1,-1";
+ succeeded = "-1,-1,-1,-1";
+ rs = PapUrlResolver.getInstance(urls, failed, succeeded);
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://two.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://three.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://four.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://one.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertFalse(rs.hasMoreUrls());
+
+ urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
+ failed = df.format(new Date(currentTime-(3600000*6)))+",-1,-1,-1";
+ succeeded = "-1,-1,-1,-1";
+ rs = PapUrlResolver.getInstance(urls, failed, succeeded);
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://one.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://two.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://three.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://four.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertFalse(rs.hasMoreUrls());
+
+
+
+
+ //not used because we are no longer sorting based on success time, only failed time. Modified above.
+ /*
+ //first sort order
+ String urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
+ String failed = df.format(new Date(currentTime-3600000))+",-1,-1,"+df.format(new Date(currentTime-3200000));
+ String succeeded = "-1,8/13/15 5:41 PM,8/13/15 4:41 PM,-1";
+ PapUrlResolver rs = PapUrlResolver.getInstance(urls, failed, succeeded);
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://three.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://two.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://one.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://four.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertFalse(rs.hasMoreUrls());
+
+ urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
+ failed = "-1,-1,-1,-1";
+ succeeded = "-1,"+df.format(new Date(currentTime-3600000))+","+df.format(new Date(currentTime-6600000))+",-1";
+ rs = PapUrlResolver.getInstance(urls, failed, succeeded);
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://one.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://four.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://three.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://two.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertFalse(rs.hasMoreUrls());
+
+ urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
+ failed = "-1,-1,-1,-1";
+ succeeded = "-1,-1,-1,-1";
+ rs = PapUrlResolver.getInstance(urls, failed, succeeded);
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://one.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://two.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://three.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://four.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertFalse(rs.hasMoreUrls());
+
+ urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
+ failed = df.format(new Date(currentTime-3900000))+","+df.format(new Date(currentTime-5600000))+","+df.format(new Date(currentTime-4600000))+","+df.format(new Date(currentTime-3600000));
+ succeeded = "-1,-1,-1,-1";
+ rs = PapUrlResolver.getInstance(urls, failed, succeeded);
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://two.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://three.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://one.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://four.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertFalse(rs.hasMoreUrls());
+
+ urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
+ failed = df.format(new Date(currentTime-(3600000*4)))+",-1,-1,-1";
+ succeeded = "-1,-1,-1,-1";
+ rs = PapUrlResolver.getInstance(urls, failed, succeeded);
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://two.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://three.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://four.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://one.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertFalse(rs.hasMoreUrls());
+
+ urls = "http://one.localhost.com,http://two.localhost.com,http://three.localhost.com,http://four.localhost.com";
+ failed = df.format(new Date(currentTime-(3600000*6)))+",-1,-1,-1";
+ succeeded = "-1,-1,-1,-1";
+ rs = PapUrlResolver.getInstance(urls, failed, succeeded);
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://one.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://two.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://three.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertTrue(rs.hasMoreUrls());
+ Assert.assertEquals("http://four.localhost.com", rs.getUrl());
+ rs.getNext();
+ Assert.assertFalse(rs.hasMoreUrls());
+ */
+ }
+}
diff --git a/ECOMP-PDP-REST/src/test/java/org/openecomp/policy/pdp/rest/XACMLPdpServletTest.java b/ECOMP-PDP-REST/src/test/java/org/openecomp/policy/pdp/rest/XACMLPdpServletTest.java
new file mode 100644
index 000000000..95cb0293c
--- /dev/null
+++ b/ECOMP-PDP-REST/src/test/java/org/openecomp/policy/pdp/rest/XACMLPdpServletTest.java
@@ -0,0 +1,395 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ECOMP-PDP-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.openecomp.policy.pdp.rest;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Properties;
+import java.util.Random;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletInputStream;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.lang.math.RandomUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.junit.Before;
+import org.junit.runner.RunWith;
+import org.mockito.Mockito;
+import org.openecomp.policy.pdp.rest.XACMLPdpServlet;
+import org.springframework.mock.web.MockHttpServletResponse;
+import org.springframework.mock.web.MockServletConfig;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+import org.openecomp.policy.common.im.AdministrativeStateException;
+import org.openecomp.policy.common.im.IntegrityMonitor;
+import org.openecomp.policy.common.im.StandbyStatusException;
+
+import com.att.research.xacml.util.XACMLProperties;
+import com.mockrunner.mock.web.MockServletInputStream;
+import org.openecomp.policy.common.logging.flexlogger.*;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(IntegrityMonitor.class) // so PowerMock can mock static method of IntegrityMonitor
+public class XACMLPdpServletTest extends TestCase{
+ private static Logger logger = FlexLogger.getLogger(XACMLPdpServletTest.class);
+
+ private List<String> headers = new ArrayList<String>();
+
+ private HttpServletRequest httpServletRequest;
+ private HttpServletResponse httpServletResponse;
+ private ServletOutputStream mockOutput;
+ private ServletInputStream mockInput;
+ private ServletConfig servletConfig;
+ private XACMLPdpServlet pdpServlet;
+ private IntegrityMonitor im;
+
+
+
+ @Before
+ public void setUp(){
+ httpServletRequest = Mockito.mock(HttpServletRequest.class);
+ Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
+ Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
+ Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
+
+
+ mockOutput = Mockito.mock(ServletOutputStream.class);
+
+ httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
+
+ try {
+ Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ fail();
+ }
+
+
+ //when(httpServletResponse.getOutputStream()).thenReturn(servletOutputStream);
+ servletConfig = Mockito.mock(MockServletConfig.class);
+ //Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
+ //servletConfig
+ Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
+ pdpServlet = new XACMLPdpServlet();
+
+ Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn("xacml.pdp.properties");
+
+ System.setProperty("xacml.properties", "xacml.pdp.properties");
+ System.setProperty("xacml.rest.pdp.config", "config_testing");
+ System.setProperty("xacml.rest.pep.idfile", "testclient.properties");
+ System.setProperty("xacml.rest.pdp.webapps", "/webapps");
+ System.setProperty("xacml.rootPolicies", "test_PolicyEngine.xml");
+ System.setProperty("xacml.referencedPolicies", "test_PolicyEngine.xml");
+ System.setProperty("test_PolicyEngine.xml.file", "config_testing\\test_PolicyEngine.xml");
+ System.setProperty("xacml.rest.pdp.register", "false");
+ System.setProperty("com.sun.management.jmxremote.port", "9999");
+
+ im = Mockito.mock(IntegrityMonitor.class);
+ // Need PowerMockito for mocking static method getInstance(...)
+ PowerMockito.mockStatic(IntegrityMonitor.class);
+ try {
+ // when IntegrityMonitor.getInstance is called, return the mock object
+ PowerMockito.when(IntegrityMonitor.getInstance(Mockito.anyString(), Mockito.any(Properties.class))).thenReturn(im);
+ } catch (Exception e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+
+ try {
+ Mockito.doNothing().when(im).startTransaction();
+ } catch (StandbyStatusException | AdministrativeStateException e) {
+ fail();
+ }
+ Mockito.doNothing().when(im).endTransaction();
+ }
+
+ public void testInit(){
+ logger.info("XACMLPdpServletTest - testInit");
+ try {
+ pdpServlet.init(servletConfig);
+ assertTrue(true);
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ System.out.println("Unexpected exception in testInit");
+ e.printStackTrace();
+ fail();
+
+ }
+
+ }
+
+ public void testDoGetNoTypeError(){
+ logger.info("XACMLPdpServletTest - testDoGetNoTypeError");
+ try{
+ pdpServlet.init(servletConfig);
+ pdpServlet.doGet(httpServletRequest, httpServletResponse);
+ Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "type not 'config' or 'hb'");
+ assertTrue(true);
+ }catch(Exception e){
+ System.out.println("Unexpected exception in testDoGetNoTypeError");
+ e.printStackTrace();
+ fail();
+ }
+ }
+
+ public void testDoGetConfigType(){
+ logger.info("XACMLPdpServletTest - testDoGetConfigType");
+ Mockito.when(httpServletRequest.getParameter("type")).thenReturn("config");
+
+ try{
+ pdpServlet.init(servletConfig);
+ pdpServlet.doGet(httpServletRequest, httpServletResponse);
+ Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
+ //Mockito.verify(httpServletResponse).sendError(400, "Failed to copy Property file");
+ assertTrue(true);
+ }catch (Exception e){
+ System.out.println("Unexpected exception in testDoGetConfigType");
+ e.printStackTrace();
+ fail();
+ }
+
+ }
+
+
+ public void testDoGetTypeHb(){
+ logger.info("XACMLPdpServletTest - testDoGetTypeHb");
+ try{
+ Mockito.when(httpServletRequest.getParameter("type")).thenReturn("hb");
+ pdpServlet.init(servletConfig);
+ pdpServlet.doGet(httpServletRequest, httpServletResponse);
+ Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_NO_CONTENT);
+ assertTrue(true);
+ }catch(Exception e){
+ System.out.println("Unexpected exception in testDoGetTypeHb");
+ e.printStackTrace();
+ fail();
+ }
+ }
+ public void testDoGetTypeStatus(){
+ logger.info("XACMLPdpServletTest - testDoGetTypeStatus");
+ try{
+ Mockito.when(httpServletRequest.getParameter("type")).thenReturn("Status");
+ pdpServlet.init(servletConfig);
+ pdpServlet.doGet(httpServletRequest, httpServletResponse);
+ Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
+ assertTrue(true);
+ }catch(Exception e){
+ System.out.println("Unexpected exception in testDoGetTypeStatus");
+ e.printStackTrace();
+ fail();
+ }
+ }
+
+ public void testDoPost(){
+ logger.info("XACMLPdpServletTest - testDoPost");
+ try{
+ pdpServlet.init(servletConfig);
+ pdpServlet.doPost(httpServletRequest, httpServletResponse);
+ //response.sendError(HttpServletResponse.SC_BAD_REQUEST, "no content-type given");
+ //Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "no content-type given");
+ assertTrue(true);
+ }catch (Exception e){
+ System.out.println("Unexpected exception in testDoPost");
+ e.printStackTrace();
+ fail();
+ }
+ }
+
+ public void testDoPostToLong(){
+ logger.info("XACMLPdpServletTest - testDoPostToLong");
+ try{
+ Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
+ Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
+
+ pdpServlet.init(servletConfig);
+ pdpServlet.doPost(httpServletRequest, httpServletResponse);
+ //response.sendError(HttpServletResponse.SC_BAD_REQUEST, "no content-type given");
+ //Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Content-Length larger than server will accept.");
+ assertTrue(true);
+ }catch (Exception e){
+ System.out.println("Unexpected exception in testDoPostToLong");
+ e.printStackTrace();
+ fail();
+ }
+ }
+
+ public void testDoPostContentLengthNegative(){
+ logger.info("XACMLPdpServletTest - testDoPostToLong");
+ try{
+ Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
+ Mockito.when(httpServletRequest.getContentLength()).thenReturn(-1);
+
+ pdpServlet.init(servletConfig);
+ pdpServlet.doPost(httpServletRequest, httpServletResponse);
+ //response.sendError(HttpServletResponse.SC_BAD_REQUEST, "no content-type given");
+ //Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Content-Length is negative");
+ assertTrue(true);
+ }catch (Exception e){
+ System.out.println("Unexpected exception in testDoPostContentLengthNegative");
+ e.printStackTrace();
+ fail();
+ }
+ }
+
+ public void testDoPostContentTypeNonValid(){
+ logger.info("XACMLPdpServletTest - testDoPostToLong");
+ try{
+ Mockito.when(httpServletRequest.getContentType()).thenReturn(";");
+ Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
+
+ pdpServlet.init(servletConfig);
+ pdpServlet.doPost(httpServletRequest, httpServletResponse);
+ //response.sendError(HttpServletResponse.SC_BAD_REQUEST, "no content-type given");
+ //Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Parsing Content-Type: ;, error=Invalid content type: ;");
+ assertTrue(true);
+ }catch (Exception e){
+ System.out.println("Unexpected exception in testDoPostContentTypeNonValid");
+ e.printStackTrace();
+ fail();
+ }
+ }
+
+ public void testDoPostContentTypeConfigurationError(){
+ logger.info("XACMLPdpServletTest - testDoPostToLong");
+ try{
+ Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
+ Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
+
+ pdpServlet.init(servletConfig);
+ pdpServlet.doPost(httpServletRequest, httpServletResponse);
+ //response.sendError(HttpServletResponse.SC_BAD_REQUEST, "no content-type given");
+ //Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "unsupported content typestuff");
+ assertTrue(true);
+ }catch (Exception e){
+ System.out.println("Unexpected exception in testDoPostContentTypeConfigurationError");
+ e.printStackTrace();
+ fail();
+ }
+ }
+
+ public void testDoPutCacheEmpty(){
+ logger.info("XACMLPdpServletTest - testDoPutCacheEmpty");
+ mockInput = Mockito.mock(ServletInputStream.class);
+
+ try{
+ Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("cache");
+ Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
+ Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
+ pdpServlet.init(servletConfig);
+ pdpServlet.doPut(httpServletRequest, httpServletResponse);
+ Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "PUT must contain at least one property");
+ assertTrue(true);
+ }catch (Exception e){
+ System.out.println("Unexpected exception in testDoPutCacheEmpty");
+ e.printStackTrace();
+ fail();
+ }
+ }
+
+ public void testDoPutConfigPolicies(){
+ logger.info("XACMLPdpServletTest - testDoPutConfigPolicies");
+ byte[] b = new byte[20];
+ new Random().nextBytes(b);
+
+ mockInput = new MockServletInputStream(b);
+
+ try{
+ Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
+ Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
+ Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
+ pdpServlet.init(servletConfig);
+ pdpServlet.doPut(httpServletRequest, httpServletResponse);
+ /*
+ * Started failing 8/17/16
+ * Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Missing property: xacml.rootPolicies");
+ */
+ assertTrue(true);
+ }catch (Exception e){
+ System.out.println("Unexpected exception in testDoPutConfigPolicies");
+ e.printStackTrace();
+ fail();
+ }
+ }
+
+ public void testDoPutToLong(){
+ logger.info("XACMLPdpServletTest - testDoPutToLong");
+ try{
+ Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
+
+ Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
+ Mockito.when(httpServletRequest.getContentLength()).thenReturn(1000000000);
+
+ pdpServlet.init(servletConfig);
+ pdpServlet.doPut(httpServletRequest, httpServletResponse);
+ //response.sendError(HttpServletResponse.SC_BAD_REQUEST, "no content-type given");
+ Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Content-Length larger than server will accept.");
+ assertTrue(true);
+ }catch (Exception e){
+ System.out.println("Unexpected exception in testDoPutToLong");
+ e.printStackTrace();
+ fail();
+ }
+ }
+
+ public void testDoPutInvalidContentType(){
+ logger.info("XACMLPdpServletTest - testDoPutToLong");
+ try{
+ Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
+
+ Mockito.when(httpServletRequest.getContentType()).thenReturn("text/json");
+ Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
+
+ pdpServlet.init(servletConfig);
+ pdpServlet.doPut(httpServletRequest, httpServletResponse);
+ //response.sendError(HttpServletResponse.SC_BAD_REQUEST, "no content-type given");
+ Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid cache: 'policies' or content-type: 'text/json'");
+ assertTrue(true);
+ }catch (Exception e){
+ System.out.println("Unexpected exception in testDoPutInvalidContentType");
+ e.printStackTrace();
+ fail();
+ }
+ }
+
+ public void testDestroy(){
+ logger.info("XACMLPdpServletTest - testDestroy");
+
+ try{
+ pdpServlet.init(servletConfig);
+ pdpServlet.destroy();
+ }catch(Exception e){
+ System.out.println("Unexpected exception in testDestroy");
+ e.printStackTrace();
+ fail();
+ }
+ }
+}