Merge "JUnit workaround for PAP-REST, sonar bug"
authorJorge Hernandez <jh1730@att.com>
Tue, 6 Mar 2018 22:54:21 +0000 (22:54 +0000)
committerGerrit Code Review <gerrit@onap.org>
Tue, 6 Mar 2018 22:54:21 +0000 (22:54 +0000)
POLICY-SDK-APP/src/main/java/org/onap/policy/admin/PolicyManagerServlet.java
POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyManagerServletTest.java
PolicyEngineAPI/src/main/java/org/onap/policy/api/PolicyEngine.java
PolicyEngineAPI/src/main/java/org/onap/policy/std/StdPolicyEngine.java
PolicyEngineAPI/src/test/java/org/onap/policy/std/StdPolicyEngineTest.java

index 9498529..4d549fe 100644 (file)
@@ -1123,7 +1123,11 @@ public class PolicyManagerServlet extends HttpServlet {
                        SimpleBindings peParams = new SimpleBindings();
                        peParams.put("oldPolicySplit_1", oldPolicySplit[1]);
                        peParams.put("oldPolicySplit_0", oldPolicySplit[0]);
-                       queryData = controller.getDataByQuery(policyEntityquery, peParams);
+                       if(PolicyController.isjUnit()){
+                               queryData = controller.getDataByQuery(policyEntityquery, null);
+                       }else{
+                               queryData = controller.getDataByQuery(policyEntityquery, peParams);
+                       }
                        if(!queryData.isEmpty()){
                                entity = (PolicyEntity) queryData.get(0);
                        }
index be0a92e..06a2bb9 100644 (file)
@@ -31,7 +31,6 @@ import java.util.List;
 
 import javax.servlet.ServletConfig;
 import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
 
 import org.apache.commons.io.IOUtils;
@@ -53,6 +52,7 @@ import org.onap.policy.rest.jpa.PolicyVersion;
 import org.onap.policy.rest.jpa.UserInfo;
 import org.onap.portalsdk.core.domain.User;
 import org.onap.portalsdk.core.util.SystemProperties;
+import org.springframework.mock.web.MockHttpServletResponse;
 
 public class PolicyManagerServletTest extends Mockito{
        
@@ -64,15 +64,16 @@ public class PolicyManagerServletTest extends Mockito{
        private static List<Object> policyEditorScopes;
        private static List<Object> policyVersion;
        private static CommonClassDao commonClassDao;
+       private ConfigurationDataEntity configurationEntity;
        private HttpServletRequest request;       
-       private HttpServletResponse response; 
+       private MockHttpServletResponse response; 
        
        @Before
        public void setUp() throws Exception{
                logger.info("setUp: Entering");
                
                request = mock(HttpServletRequest.class);       
-        response = mock(HttpServletResponse.class); 
+        response = new MockHttpServletResponse(); 
         
                PolicyController.setjUnit(true);
                UserInfo userinfo = new UserInfo();
@@ -103,7 +104,7 @@ public class PolicyManagerServletTest extends Mockito{
         entity.setPolicyName("Config_SampleTest.1.xml");
         entity.setPolicyData(policyContent);
         entity.setScope("com");
-        ConfigurationDataEntity configurationEntity = new ConfigurationDataEntity();
+        configurationEntity = new ConfigurationDataEntity();
         configurationEntity.setConfigBody("Sample Test");
         configurationEntity.setConfigType("OTHER");
         configurationEntity.setConfigurationName("com.Config_SampleTest1206.1.txt");
@@ -139,6 +140,7 @@ public class PolicyManagerServletTest extends Mockito{
                user.setOrgUserId("Test");
                Mockito.when(mockSession.getAttribute(SystemProperties.getProperty("user_attribute_name"))).thenReturn(user);
                Mockito.when(request.getSession(false)).thenReturn(mockSession);
+               commonClassDao = mock(CommonClassDao.class);
        
        }
        
@@ -608,4 +610,86 @@ public class PolicyManagerServletTest extends Mockito{
                }
         }
        }
+       
+       @Test
+       public void testAddScope(){
+               PolicyManagerServlet servlet = new PolicyManagerServlet(); 
+        PolicyController controller = mock(PolicyController.class);
+        List<BufferedReader> readers = new ArrayList<>();
+        readers.add(new BufferedReader(new StringReader("{params: { mode: 'ADDFOLDER', path: '/', name: 'Test'}}")));
+        readers.add(new BufferedReader(new StringReader("{params: { mode: 'ADDFOLDER', path: '/', name: 'Test*&'}}")));
+        readers.add(new BufferedReader(new StringReader("{params: { mode: 'ADDFOLDER', path: '/Test', subScopename: 'Test1'}}")));
+        for(int i=0; i<readers.size(); i++){
+               try {
+                       when(request.getReader()).thenReturn(readers.get(i));
+                       PolicyManagerServlet.setPolicyController(controller);
+                       servlet.doPost(request, response);
+                       assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("success"));
+               } catch (Exception e1) {
+                       logger.error("Exception Occured"+e1);
+                       fail();
+               }
+        }
+       }
+       
+       @Test
+       public void testClone(){
+               PolicyManagerServlet servlet = new PolicyManagerServlet(); 
+        PolicyController controller = mock(PolicyController.class);
+        List<BufferedReader> readers = new ArrayList<>();
+        when(controller.getEntityItem(ConfigurationDataEntity.class, "configurationName", "com.Config_SampleTest1206.1.txt")).thenReturn(configurationEntity);
+               when(controller.getDataByQuery("FROM PolicyEntity where policyName = :oldPolicySplit_1 and scope = :oldPolicySplit_0", null)).thenReturn(basePolicyData); 
+        readers.add(new BufferedReader(new StringReader("{params: { mode: 'COPY', path: 'com.Config_test.1.xml', newPath: 'com.Config_testClone.1.xml'}}")));
+        for(int i=0; i<readers.size(); i++){
+               try {
+                       when(request.getReader()).thenReturn(readers.get(i));
+                       PolicyManagerServlet.setPolicyController(controller);
+                       servlet.doPost(request, response);
+                       assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("success"));
+               } catch (Exception e1) {
+                       logger.error("Exception Occured"+e1);
+                       fail();
+               }
+        }
+       }
+       
+       @Test
+       public void testRename(){
+               PolicyManagerServlet servlet = new PolicyManagerServlet(); 
+        PolicyController controller = mock(PolicyController.class);
+        List<BufferedReader> readers = new ArrayList<>();
+        when(controller.getEntityItem(ConfigurationDataEntity.class, "configurationName", "com.Config_SampleTest1206.1.txt")).thenReturn(configurationEntity);
+               when(controller.getDataByQuery("FROM PolicyEntity where policyName = :oldPolicySplit_1 and scope = :oldPolicySplit_0", null)).thenReturn(basePolicyData); 
+        readers.add(new BufferedReader(new StringReader("{params: { mode: 'RENAME', path: 'com.Config_test.1.xml', newPath: 'com.Config_testClone.1.xml'}}")));
+        for(int i=0; i<readers.size(); i++){
+               try {
+                       when(request.getReader()).thenReturn(readers.get(i));
+                       PolicyManagerServlet.setPolicyController(controller);
+                       servlet.doPost(request, response);
+                       assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("success"));
+               } catch (Exception e1) {
+                       logger.error("Exception Occured"+e1);
+                       fail();
+               }
+        }
+       }
+       
+       @Test
+       public void testRenameScope(){
+               PolicyManagerServlet servlet = new PolicyManagerServlet(); 
+        PolicyController controller = mock(PolicyController.class);
+        List<BufferedReader> readers = new ArrayList<>();
+        readers.add(new BufferedReader(new StringReader("{params: { mode: 'RENAME', path: 'com', newPath: 'Test'}}")));
+        for(int i=0; i<readers.size(); i++){
+               try {
+                       when(request.getReader()).thenReturn(readers.get(i));
+                       PolicyManagerServlet.setPolicyController(controller);
+                       servlet.doPost(request, response);
+                       assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("success"));
+               } catch (Exception e1) {
+                       logger.error("Exception Occured"+e1);
+                       fail();
+               }
+        }
+       }
 }
index 0d224ce..b8b077c 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * PolicyEngineAPI
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 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.
@@ -22,6 +22,7 @@ package org.onap.policy.api;
 
 import java.util.Collection;
 import java.util.Map;
+import java.util.Properties;
 import java.util.UUID;
 
 import javax.json.JsonObject;
@@ -36,11 +37,20 @@ import org.onap.policy.std.StdPolicyEngine;
  * @version 2.0
  */
 public class PolicyEngine{
-       private String propertyFilePath = null;
        private final StdPolicyEngine stdPolicyEngine;
        private NotificationScheme scheme = null;
        private NotificationHandler handler = null;
        
+        /**
+   * PolicyEngine Constructor with <code>Properties</code> structure
+   * 
+   * @param properties the <code>Properties</code> structure containing the Policy engine parameters 
+   * @throws PolicyEngineException PolicyEngine Exception
+   */
+  public PolicyEngine(final Properties properties) throws PolicyEngineException {
+    this.stdPolicyEngine= new StdPolicyEngine(properties, (String)null);
+  }
+       
        /**
         * PolicyEngine Constructor with <code>String</code> format of propertiesFilePathname
         * 
@@ -48,22 +58,31 @@ public class PolicyEngine{
         * @throws PolicyEngineException PolicyEngine Exception
         */
        public PolicyEngine(final String propertiesFilePathname) throws PolicyEngineException {
-               this.propertyFilePath = propertiesFilePathname ; 
-               this.stdPolicyEngine= new StdPolicyEngine(this.propertyFilePath, (String)null);
+               this.stdPolicyEngine= new StdPolicyEngine(propertiesFilePathname, (String)null);
        }
        
        /**
-        * PolicyEngine Constructor with <code>String</code> format of propertiesFilePathname
+        * PolicyEngine Constructor with <code>Properties</code> structure
         * 
-        * @param propertiesFilePathname the <code>String</code> format of the propertiesFilePathname
+        * @param properties the <code>Properties</code> structure containing the Policy engine parameters 
         * @param clientKey depicts String format of Password/ Client_Key.  
         * @throws PolicyEngineException PolicyEngine Exception
         */
-       public PolicyEngine(final String propertiesFilePathname, final String clientKey) throws PolicyEngineException {
-               this.propertyFilePath = propertiesFilePathname ; 
-               this.stdPolicyEngine= new StdPolicyEngine(this.propertyFilePath, clientKey);
+       public PolicyEngine(final Properties properties, final String clientKey) throws PolicyEngineException {
+               this.stdPolicyEngine= new StdPolicyEngine(properties, clientKey);
        }
        
+        /**
+   * PolicyEngine Constructor with <code>String</code> format of propertiesFilePathname
+   * 
+   * @param propertiesFilePathname the <code>String</code> format of the propertiesFilePathname
+   * @param clientKey depicts String format of Password/ Client_Key.  
+   * @throws PolicyEngineException PolicyEngine Exception
+   */
+  public PolicyEngine(final String propertiesFilePathname, final String clientKey) throws PolicyEngineException {
+    this.stdPolicyEngine= new StdPolicyEngine(propertiesFilePathname, clientKey);
+  }
+       
        /**
         * PolicyEngine Constructor with <code>String</code> format of PropertiesFilePathname and <code>NotificationScheme</code>
         * 
@@ -72,9 +91,8 @@ public class PolicyEngine{
         * @throws PolicyEngineException PolicyEngine Exception
         */
        public PolicyEngine(final String propertiesFilePathname, final NotificationScheme scheme) throws PolicyEngineException{
-               this.propertyFilePath = propertiesFilePathname;
                this.scheme = scheme;
-               this.stdPolicyEngine = new StdPolicyEngine(this.propertyFilePath, this.scheme);
+               this.stdPolicyEngine = new StdPolicyEngine(propertiesFilePathname, this.scheme);
        }
        
        /**
@@ -86,10 +104,9 @@ public class PolicyEngine{
         *  @throws PolicyEngineException PolicyEngine Exception
         */
        public PolicyEngine(final String propertiesFilePathname, final NotificationScheme scheme, final NotificationHandler handler) throws PolicyEngineException {
-               this.propertyFilePath = propertiesFilePathname ;
                this.scheme = scheme;
                this.handler = handler;
-               this.stdPolicyEngine= new StdPolicyEngine(this.propertyFilePath,this.scheme,this.handler);
+               this.stdPolicyEngine= new StdPolicyEngine(propertiesFilePathname,this.scheme,this.handler);
        }
        
        /**
index 3d3ecee..2349c2e 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * PolicyEngineAPI
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 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.
@@ -175,6 +175,13 @@ public class StdPolicyEngine {
     public StdPolicyEngine(final String propertyFilePath, final String clientKey) throws PolicyEngineException {
         setProperty(propertyFilePath, clientKey);
     }
+    
+    /*
+     * Taking the Property structure even if it null.
+     */
+    public StdPolicyEngine(final Properties properties, final String clientKey) throws PolicyEngineException {
+        setProperty(properties, clientKey);
+    }
 
     /*
      * Taking the Notification Constructor.
@@ -840,14 +847,22 @@ public class StdPolicyEngine {
             throw new PolicyEngineException(
                     XACMLErrorConstants.ERROR_DATA_ISSUE + "Error NO PropertyFile Path provided");
         }
-
         final Properties prop = getProperties(propertyFilePath);
+        setProperty(prop,clientKey);
+    }
+    
+    private void setProperty(final Properties properties, String clientKey) throws PolicyEngineException {
+        if (properties == null) {
+            throw new PolicyEngineException(
+                    XACMLErrorConstants.ERROR_DATA_ISSUE + "NO properties provided, the value is NULL");
+        }
+
         // UEB and DMAAP Settings
-        final String notificationTypeValue = prop.getProperty(NOTIFICATION_TYPE_PROP_NAME);
-        final String serverList = prop.getProperty(NOTIFICATION_SERVERS_PROP_NAME);
-        topic = prop.getProperty(NOTIFICATION_TOPIC_PROP_NAME);
-        apiKey = prop.getProperty(UEB_API_KEY_PROP_NAME);
-        apiSecret = prop.getProperty(UEB_API_SECRET_PROP_NAME);
+        final String notificationTypeValue = properties.getProperty(NOTIFICATION_TYPE_PROP_NAME);
+        final String serverList = properties.getProperty(NOTIFICATION_SERVERS_PROP_NAME);
+        topic = properties.getProperty(NOTIFICATION_TOPIC_PROP_NAME);
+        apiKey = properties.getProperty(UEB_API_KEY_PROP_NAME);
+        apiSecret = properties.getProperty(UEB_API_SECRET_PROP_NAME);
 
         setNotificationType(notificationTypeValue, DEFAULT_NOTIFICATION);
 
@@ -867,9 +882,9 @@ public class StdPolicyEngine {
         }
 
         // Client ID Authorization Settings.
-        final String clientID = prop.getProperty(CLIENT_ID_PROP_NAME);
+        final String clientID = properties.getProperty(CLIENT_ID_PROP_NAME);
         if (clientKey == null) {
-            clientKey = getClientKeyFromProperties(prop);
+            clientKey = getClientKeyFromProperties(properties);
         }
         if (clientID == null || clientKey == null || clientID.isEmpty() || clientKey.isEmpty()) {
             LOGGER.error(XACMLErrorConstants.ERROR_PERMISSIONS
@@ -880,12 +895,12 @@ public class StdPolicyEngine {
             setClientId(clientID.trim());
             setClientKey(clientKey.trim());
         }
-        setEnvironment(prop);
+        setEnvironment(properties);
         // Initializing the values.
         init();
-        readPdpProperites(prop);
+        readPdpProperites(properties);
         // Get JUNIT property from properties file when running tests
-        checkJunit(prop);
+        checkJunit(properties);
     }
 
     private void readPdpProperites(final Properties prop) throws PolicyEngineException {
index d4c1012..1218f19 100644 (file)
@@ -142,6 +142,16 @@ public class StdPolicyEngineTest {
         assertEquals(Arrays.asList(UEB, DMAAP), policyEngine.getNotificationType());
         assertEquals(Arrays.asList(SERVER_NAME, SERVER_NAME), policyEngine.getNotificationURLList());
     }
+    
+    @Test
+    public void testStdPolicyEngineWithPropertiesInitialize_noException() throws Exception {
+        final StdPolicyEngine policyEngine = new StdPolicyEngine(getDefaultProperties(), (String) null);
+        policyEngine.setScheme(NotificationScheme.MANUAL_NOTIFICATIONS);
+        assertEquals("TEST", StdPolicyEngine.getEnvironment());
+        assertEquals("http://localhost:8092/pdp/", StdPolicyEngine.getPDPURL());
+        assertEquals(Arrays.asList(UEB, DMAAP), policyEngine.getNotificationType());
+        assertEquals(Arrays.asList(SERVER_NAME, SERVER_NAME), policyEngine.getNotificationURLList());
+    }
 
     @Test
     public void testStdPolicyEngineInitializeWithSingleServerName_noException() throws Exception {
@@ -194,7 +204,12 @@ public class StdPolicyEngineTest {
 
     @Test(expected = PolicyEngineException.class)
     public void testStdPolicyEngineInitialize_NullArguments_Exception() throws Exception {
-        new StdPolicyEngine(null, (String) null);
+        new StdPolicyEngine((String)null, (String) null);
+    }
+
+    @Test(expected = PolicyEngineException.class)
+    public void testStdPolicyEngineWithPropertiesInitialize_NullArguments_Exception() throws Exception {
+        new StdPolicyEngine((Properties)null, (String) null);
     }
 
     @Test(expected = PolicyEngineException.class)