X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ECOMP-PDP-REST%2Fsrc%2Ftest%2Fjava%2Forg%2Fopenecomp%2Fpolicy%2Fpdp%2Frest%2FXACMLPdpServletTest.java;h=7f302ffa88baafadeef98531072ed0a3753ef428;hb=7e547eaa55920dfbc9691eab33bb728395b50cf2;hp=95cb0293c1b6ef348bb2143e3ba5f7494ee9b012;hpb=91d04c64771832a0b8815ffbe1f0f9920320d94d;p=policy%2Fengine.git 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 index 95cb0293c..7f302ffa8 100644 --- 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 @@ -21,7 +21,6 @@ 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; @@ -34,33 +33,28 @@ 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 org.openecomp.policy.common.logging.flexlogger.FlexLogger; +import org.openecomp.policy.common.logging.flexlogger.Logger; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.mock.web.MockServletConfig; -import com.att.research.xacml.util.XACMLProperties; import com.mockrunner.mock.web.MockServletInputStream; -import org.openecomp.policy.common.logging.flexlogger.*; + +import junit.framework.TestCase; @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 static Logger LOGGER = FlexLogger.getLogger(XACMLPdpServletTest.class); private List headers = new ArrayList(); @@ -89,14 +83,10 @@ public class XACMLPdpServletTest extends TestCase{ 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(); @@ -120,7 +110,6 @@ public class XACMLPdpServletTest extends TestCase{ // 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(); } @@ -133,14 +122,12 @@ public class XACMLPdpServletTest extends TestCase{ } public void testInit(){ - logger.info("XACMLPdpServletTest - 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(); + LOGGER.error("Exception Occured"+e); fail(); } @@ -148,7 +135,7 @@ public class XACMLPdpServletTest extends TestCase{ } public void testDoGetNoTypeError(){ - logger.info("XACMLPdpServletTest - testDoGetNoTypeError"); + LOGGER.info("XACMLPdpServletTest - testDoGetNoTypeError"); try{ pdpServlet.init(servletConfig); pdpServlet.doGet(httpServletRequest, httpServletResponse); @@ -156,24 +143,23 @@ public class XACMLPdpServletTest extends TestCase{ assertTrue(true); }catch(Exception e){ System.out.println("Unexpected exception in testDoGetNoTypeError"); - e.printStackTrace(); + LOGGER.error("Exception Occured"+e); fail(); } } public void testDoGetConfigType(){ - logger.info("XACMLPdpServletTest - 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(); + LOGGER.error("Exception Occured"+e); fail(); } @@ -181,7 +167,7 @@ public class XACMLPdpServletTest extends TestCase{ public void testDoGetTypeHb(){ - logger.info("XACMLPdpServletTest - testDoGetTypeHb"); + LOGGER.info("XACMLPdpServletTest - testDoGetTypeHb"); try{ Mockito.when(httpServletRequest.getParameter("type")).thenReturn("hb"); pdpServlet.init(servletConfig); @@ -190,12 +176,12 @@ public class XACMLPdpServletTest extends TestCase{ assertTrue(true); }catch(Exception e){ System.out.println("Unexpected exception in testDoGetTypeHb"); - e.printStackTrace(); + LOGGER.error("Exception Occured"+e); fail(); } } public void testDoGetTypeStatus(){ - logger.info("XACMLPdpServletTest - testDoGetTypeStatus"); + LOGGER.info("XACMLPdpServletTest - testDoGetTypeStatus"); try{ Mockito.when(httpServletRequest.getParameter("type")).thenReturn("Status"); pdpServlet.init(servletConfig); @@ -204,100 +190,90 @@ public class XACMLPdpServletTest extends TestCase{ assertTrue(true); }catch(Exception e){ System.out.println("Unexpected exception in testDoGetTypeStatus"); - e.printStackTrace(); + LOGGER.error("Exception Occured"+e); fail(); } } public void testDoPost(){ - logger.info("XACMLPdpServletTest - 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(); + LOGGER.error("Exception Occured"+e); fail(); } } public void testDoPostToLong(){ - logger.info("XACMLPdpServletTest - 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(); + LOGGER.error("Exception Occured"+e); fail(); } } public void testDoPostContentLengthNegative(){ - logger.info("XACMLPdpServletTest - testDoPostToLong"); + 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(); + LOGGER.error("Exception Occured"+e); fail(); } } public void testDoPostContentTypeNonValid(){ - logger.info("XACMLPdpServletTest - testDoPostToLong"); + 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(); + LOGGER.error("Exception Occured"+e); fail(); } } public void testDoPostContentTypeConfigurationError(){ - logger.info("XACMLPdpServletTest - testDoPostToLong"); + 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(); + LOGGER.error("Exception Occured"+e); fail(); } } public void testDoPutCacheEmpty(){ - logger.info("XACMLPdpServletTest - testDoPutCacheEmpty"); + LOGGER.info("XACMLPdpServletTest - testDoPutCacheEmpty"); mockInput = Mockito.mock(ServletInputStream.class); try{ @@ -310,13 +286,13 @@ public class XACMLPdpServletTest extends TestCase{ assertTrue(true); }catch (Exception e){ System.out.println("Unexpected exception in testDoPutCacheEmpty"); - e.printStackTrace(); + LOGGER.error("Exception Occured"+e); fail(); } } public void testDoPutConfigPolicies(){ - logger.info("XACMLPdpServletTest - testDoPutConfigPolicies"); + LOGGER.info("XACMLPdpServletTest - testDoPutConfigPolicies"); byte[] b = new byte[20]; new Random().nextBytes(b); @@ -328,20 +304,16 @@ public class XACMLPdpServletTest extends TestCase{ 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(); + LOGGER.error("Exception Occured"+e); fail(); } } public void testDoPutToLong(){ - logger.info("XACMLPdpServletTest - testDoPutToLong"); + LOGGER.info("XACMLPdpServletTest - testDoPutToLong"); try{ Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies"); @@ -350,18 +322,17 @@ public class XACMLPdpServletTest extends TestCase{ 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(); + LOGGER.error("Exception Occured"+e); fail(); } } public void testDoPutInvalidContentType(){ - logger.info("XACMLPdpServletTest - testDoPutToLong"); + LOGGER.info("XACMLPdpServletTest - testDoPutToLong"); try{ Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies"); @@ -370,25 +341,24 @@ public class XACMLPdpServletTest extends TestCase{ 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(); + LOGGER.error("Exception Occured"+e); fail(); } } public void testDestroy(){ - logger.info("XACMLPdpServletTest - testDestroy"); + LOGGER.info("XACMLPdpServletTest - testDestroy"); try{ pdpServlet.init(servletConfig); pdpServlet.destroy(); }catch(Exception e){ System.out.println("Unexpected exception in testDestroy"); - e.printStackTrace(); + LOGGER.error("Exception Occured"+e); fail(); } }