Guard policy Backend
[clamp.git] / src / test / java / org / onap / clamp / clds / it / CldsServiceItCase.java
index 0f65ef0..3c508bd 100644 (file)
@@ -2,23 +2,23 @@
  * ============LICENSE_START=======================================================
  * ONAP CLAMP
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights
+ * 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. 
+ * 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 
+ *
+ * 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============================================
  * ===================================================================
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
+ *
  */
 
 package org.onap.clamp.clds.it;
@@ -26,33 +26,48 @@ package org.onap.clamp.clds.it;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
 import com.att.aft.dme2.internal.apache.commons.lang.RandomStringUtils;
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.security.Principal;
+import java.security.GeneralSecurityException;
+import java.util.LinkedList;
+import java.util.List;
 import java.util.Properties;
 
-import javax.ws.rs.core.SecurityContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.xml.transform.TransformerException;
 
+import org.apache.commons.codec.DecoderException;
+import org.json.JSONException;
+import org.json.simple.parser.ParseException;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.mockito.Matchers;
 import org.mockito.Mockito;
-import org.onap.clamp.clds.AbstractItCase;
 import org.onap.clamp.clds.dao.CldsDao;
-import org.onap.clamp.clds.model.CldsHealthCheck;
 import org.onap.clamp.clds.model.CldsInfo;
 import org.onap.clamp.clds.model.CldsModel;
+import org.onap.clamp.clds.model.CldsServiceData;
 import org.onap.clamp.clds.model.CldsTemplate;
 import org.onap.clamp.clds.service.CldsService;
+import org.onap.clamp.clds.util.LoggingUtils;
 import org.onap.clamp.clds.util.ResourceFileUtil;
+import org.skyscreamer.jsonassert.JSONAssert;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
-import org.springframework.test.context.TestPropertySource;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.GrantedAuthority;
+import org.springframework.security.core.authority.SimpleGrantedAuthority;
+import org.springframework.security.core.context.SecurityContext;
+import org.springframework.security.core.userdetails.User;
+import org.springframework.security.core.userdetails.UserDetails;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
 /**
@@ -60,8 +75,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  */
 @RunWith(SpringJUnit4ClassRunner.class)
 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
-@TestPropertySource(locations = "classpath:application-no-camunda.properties")
-public class CldsServiceItCase extends AbstractItCase {
+public class CldsServiceItCase {
 
     @Autowired
     private CldsService cldsService;
@@ -70,10 +84,12 @@ public class CldsServiceItCase extends AbstractItCase {
     private String bpmnPropText;
     @Autowired
     private CldsDao cldsDao;
-
+    private Authentication authentication;
+    private List<GrantedAuthority> authList =  new LinkedList<GrantedAuthority>();
+    private LoggingUtils util;
     /**
      * Setup the variable before the tests execution.
-     * 
+     *
      * @throws IOException
      *             In case of issues when opening the files
      */
@@ -82,14 +98,29 @@ public class CldsServiceItCase extends AbstractItCase {
         bpmnText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-template.xml");
         imageText = ResourceFileUtil.getResourceAsString("example/dao/image-template.xml");
         bpmnPropText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-prop.json");
+
+        authList.add(new SimpleGrantedAuthority("permission-type-cl-manage|dev|*"));
+        authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|read"));
+        authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|update"));
+        authList.add(new SimpleGrantedAuthority("permission-type-template|dev|read"));
+        authList.add(new SimpleGrantedAuthority("permission-type-template|dev|update"));
+        authList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|*"));
+        authentication =  new UsernamePasswordAuthenticationToken(new User("admin", "", authList), "", authList);
+
+        util = Mockito.mock(LoggingUtils.class);
+        Mockito.doNothing().when(util).entering(Matchers.any(HttpServletRequest.class), Matchers.any(String.class));
+        cldsService.setLoggingUtil(util);
     }
 
     @Test
-    public void testCldsInfoNotAuthorized() throws Exception {
+    public void testCldsInfoNotAuthorized() {
         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
-        Principal principal = Mockito.mock(Principal.class);
-        Mockito.when(principal.getName()).thenReturn("admin");
-        Mockito.when(securityContext.getUserPrincipal()).thenReturn(principal);
+        Authentication localAuth = Mockito.mock(Authentication.class);
+        UserDetails userDetails = Mockito.mock(UserDetails.class);
+        Mockito.when(userDetails.getUsername()).thenReturn("admin");
+        Mockito.when(securityContext.getAuthentication()).thenReturn(localAuth);
+        Mockito.when(localAuth.getPrincipal()).thenReturn(userDetails);
+
         cldsService.setSecurityContext(securityContext);
         CldsInfo cldsInfo = cldsService.getCldsInfo();
         assertFalse(cldsInfo.isPermissionReadCl());
@@ -101,13 +132,8 @@ public class CldsServiceItCase extends AbstractItCase {
     @Test
     public void testCldsInfoAuthorized() throws Exception {
         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
-        Principal principal = Mockito.mock(Principal.class);
-        Mockito.when(principal.getName()).thenReturn("admin");
-        Mockito.when(securityContext.getUserPrincipal()).thenReturn(principal);
-        Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|read")).thenReturn(true);
-        Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|update")).thenReturn(true);
-        Mockito.when(securityContext.isUserInRole("permission-type-template|dev|read")).thenReturn(true);
-        Mockito.when(securityContext.isUserInRole("permission-type-template|dev|update")).thenReturn(true);
+        Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
+
         cldsService.setSecurityContext(securityContext);
         CldsInfo cldsInfo = cldsService.getCldsInfo();
         assertTrue(cldsInfo.isPermissionReadCl());
@@ -123,24 +149,10 @@ public class CldsServiceItCase extends AbstractItCase {
     }
 
     @Test
-    public void testGetHealthCheck() throws Exception {
-        CldsHealthCheck cldsHealthCheck = cldsService.gethealthcheck();
-        assertNotNull(cldsHealthCheck);
-        assertEquals("UP", cldsHealthCheck.getHealthCheckStatus());
-        assertEquals("CLDS-APP", cldsHealthCheck.getHealthCheckComponent());
-        assertEquals("OK", cldsHealthCheck.getDescription());
-    }
-
-    @Test
-    public void testPutModel() throws Exception {
+    public void testCompleteFlow() throws TransformerException, ParseException {
         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
-        Principal principal = Mockito.mock(Principal.class);
-        Mockito.when(principal.getName()).thenReturn("admin");
-        Mockito.when(securityContext.getUserPrincipal()).thenReturn(principal);
-        Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|read")).thenReturn(true);
-        Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|update")).thenReturn(true);
-        Mockito.when(securityContext.isUserInRole("permission-type-template|dev|read")).thenReturn(true);
-        Mockito.when(securityContext.isUserInRole("permission-type-template|dev|update")).thenReturn(true);
+        Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
+
         cldsService.setSecurityContext(securityContext);
         // Add the template first
         CldsTemplate newTemplate = new CldsTemplate();
@@ -155,20 +167,58 @@ public class CldsServiceItCase extends AbstractItCase {
         assertEquals(bpmnText, newTemplateRead.getBpmnText());
         assertEquals(imageText, newTemplateRead.getImageText());
         // Save the model
+        String randomNameModel = RandomStringUtils.randomAlphanumeric(5);
         CldsModel newModel = new CldsModel();
-        newModel.setName(randomNameTemplate);
+        newModel.setName(randomNameModel);
         newModel.setBpmnText(bpmnText);
         newModel.setImageText(imageText);
         newModel.setPropText(bpmnPropText);
         newModel.setControlNamePrefix("ClosedLoop-");
-        newModel.setTemplateName("test-template");
+        newModel.setTemplateName(randomNameTemplate);
         newModel.setTemplateId(newTemplate.getId());
         newModel.setDocText(newTemplate.getPropText());
-        newModel.setDocId(newTemplate.getPropId());
         // Test the PutModel method
-        String randomNameModel = RandomStringUtils.randomAlphanumeric(5);
+
         cldsService.putModel(randomNameModel, newModel);
         // Verify whether it has been added properly or not
         assertNotNull(cldsDao.getModel(randomNameModel));
+
+        // Verify with GetModel
+        assertEquals(cldsService.getModel(randomNameModel).getTemplateName(),randomNameTemplate);
+        assertEquals(cldsService.getModel(randomNameModel).getName(),randomNameModel);
+
+        assertTrue(cldsService.getModelNames().size() >= 1);
+    }
+
+    @Test
+    public void testGetSdcServices() throws GeneralSecurityException, DecoderException, JSONException, IOException {
+        String result = cldsService.getSdcServices();
+        JSONAssert.assertEquals(
+            ResourceFileUtil.getResourceAsString("example/sdc/expected-result/all-sdc-services.json"), result,
+            true);
+    }
+
+    @Test
+    public void testGetSdcPropertiesByServiceUuidForRefresh()
+        throws GeneralSecurityException, DecoderException, JSONException, IOException {
+        SecurityContext securityContext = Mockito.mock(SecurityContext.class);
+        Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
+
+        cldsService.setSecurityContext(securityContext);
+        // Test basic functionalities
+        String result = cldsService.getSdcPropertiesByServiceUUIDForRefresh("4cc5b45a-1f63-4194-8100-cd8e14248c92",
+            false);
+        JSONAssert.assertEquals(
+            ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-4cc5b45a.json"),
+            result, true);
+        // Now test the Cache effect
+        CldsServiceData cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
+        // Should not be there, so should be null
+        assertNull(cldsServiceDataCache);
+        cldsService.getSdcPropertiesByServiceUUIDForRefresh("c95b0e7c-c1f0-4287-9928-7964c5377a46", true);
+        // Should be there now, so should NOT be null
+        cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
+        assertNotNull(cldsServiceDataCache);
+        cldsDao.clearServiceCache();
     }
 }