Add unit tests
[clamp.git] / src / test / java / org / onap / clamp / clds / it / CldsServiceItCase.java
index 0193613..072d577 100644 (file)
@@ -2,57 +2,62 @@
  * ============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;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
-
-import com.att.aft.dme2.internal.apache.commons.lang.RandomStringUtils;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.security.Principal;
+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.ws.rs.NotAuthorizedException;
 
 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.CldsTemplate;
 import org.onap.clamp.clds.service.CldsService;
-import org.onap.clamp.clds.util.ResourceFileUtil;
+import org.onap.clamp.clds.util.LoggingUtils;
 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,35 +65,35 @@ 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 {
-    @Autowired
-    CldsService cldsService;
-    private String bpmnText;
-    private String imageText;
-    private String bpmnPropText;
+public class CldsServiceItCase {
+
     @Autowired
-    public CldsDao cldsDao;
+    private CldsService cldsService;
 
+    private LoggingUtils util;
+    private SecurityContext securityContext = mock(SecurityContext.class);
+    private Authentication auth = Mockito.mock(Authentication.class);
+    private UserDetails userDetails = Mockito.mock(UserDetails.class);
+    private List<GrantedAuthority> authorityList = new LinkedList<GrantedAuthority>();
     /**
      * Setup the variable before the tests execution.
-     * 
-     * @throws IOException
-     *             In case of issues when opening the files
+     *
+     * @throws IOException In case of issues when opening the files
      */
     @Before
     public void setupBefore() throws IOException {
-        bpmnText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-template.xml");
-        imageText = ResourceFileUtil.getResourceAsString("example/dao/image-template.xml");
-        bpmnPropText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-prop.json");
+        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 {
-        SecurityContext securityContext = Mockito.mock(SecurityContext.class);
-        Principal principal = Mockito.mock(Principal.class);
-        Mockito.when(principal.getName()).thenReturn("admin");
-        Mockito.when(securityContext.getUserPrincipal()).thenReturn(principal);
+    public void testCldsInfoNotAuthorized() {
+        Mockito.when(userDetails.getUsername()).thenReturn("admin");
+        Mockito.when(securityContext.getAuthentication()).thenReturn(auth);
+        Mockito.when(auth.getPrincipal()).thenReturn(userDetails);
+
         cldsService.setSecurityContext(securityContext);
         CldsInfo cldsInfo = cldsService.getCldsInfo();
         assertFalse(cldsInfo.isPermissionReadCl());
@@ -99,14 +104,19 @@ 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);
+        Authentication authentication;
+        List<GrantedAuthority> authList = new LinkedList<GrantedAuthority>();
+        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|*"));
+        authList.add(new SimpleGrantedAuthority("permission-type-cl-event|dev|*"));
+        authentication = new UsernamePasswordAuthenticationToken(new User("admin", "", authList), "", authList);
+
+        Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
+
         cldsService.setSecurityContext(securityContext);
         CldsInfo cldsInfo = cldsService.getCldsInfo();
         assertTrue(cldsInfo.isPermissionReadCl());
@@ -116,58 +126,115 @@ public class CldsServiceItCase extends AbstractItCase {
         Properties prop = new Properties();
         InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("clds-version.properties");
         prop.load(in);
+        assertNotNull(in);
         in.close();
         assertEquals(cldsInfo.getCldsVersion(), prop.getProperty("clds.version"));
         assertEquals(cldsInfo.getUserName(), "admin");
     }
 
+    @Test(expected = NotAuthorizedException.class)
+    public void isAuthorizedForVfTestNotAuthorized1() throws Exception {
+        when(userDetails.getUsername()).thenReturn("testName");
+        when(auth.getPrincipal()).thenReturn(userDetails);
+        when(securityContext.getAuthentication()).thenReturn(auth);
+        cldsService.setSecurityContext(securityContext);
+        boolean res = cldsService.isAuthorizedForVf("testId");
+        assertThat(res).isTrue();
+    }
+
+    @Test(expected = NotAuthorizedException.class)
+    public void isAuthorizedForVfTestNotAuthorized2() throws Exception {
+        when(userDetails.getUsername()).thenReturn("testName");
+        when(auth.getPrincipal()).thenReturn(userDetails);
+        authorityList.add(new SimpleGrantedAuthority("permission-type-filter-vf|prod|*"));
+        when((List<GrantedAuthority>)auth.getAuthorities()).thenReturn(authorityList);
+        when(securityContext.getAuthentication()).thenReturn(auth);
+        cldsService.setSecurityContext(securityContext);
+        boolean res = cldsService.isAuthorizedForVf("testId");
+        assertThat(res).isTrue();
+    }
+
+    @Test(expected = NotAuthorizedException.class)
+    public void isAuthorizedForVfTestNotAuthorized3() throws Exception {
+        when(userDetails.getUsername()).thenReturn("testName");
+        when(auth.getPrincipal()).thenReturn(userDetails);
+        authorityList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|testId2"));
+        when((List<GrantedAuthority>)auth.getAuthorities()).thenReturn(authorityList);
+        when(securityContext.getAuthentication()).thenReturn(auth);
+        cldsService.setSecurityContext(securityContext);
+        boolean res = cldsService.isAuthorizedForVf("testId");
+        assertThat(res).isTrue();
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void isAuthorizedForVfTestNotAuthorized4() throws Exception {
+        when(userDetails.getUsername()).thenReturn("testName");
+        when(auth.getPrincipal()).thenReturn(userDetails);
+        when(securityContext.getAuthentication()).thenReturn(null);
+        cldsService.setSecurityContext(securityContext);
+        boolean res = cldsService.isAuthorizedForVf("testId");
+        assertThat(res).isTrue();
+    }
+
     @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());
+    public void isAuthorizedForVfTest1() throws Exception {
+        when(userDetails.getUsername()).thenReturn("testName");
+        when(auth.getPrincipal()).thenReturn(userDetails);
+        authorityList.add(new SimpleGrantedAuthority("permission-type-filter-vf|*|*"));
+        when((List<GrantedAuthority>)auth.getAuthorities()).thenReturn(authorityList);
+        when(securityContext.getAuthentication()).thenReturn(auth);
+
+        cldsService.setSecurityContext(securityContext);
+        boolean res = cldsService.isAuthorizedForVf("testId");
+        assertThat(res).isTrue();
+    }
+
+    @Test
+    public void isAuthorizedForVfTest2() throws Exception {
+        when(userDetails.getUsername()).thenReturn("testName");
+        when(auth.getPrincipal()).thenReturn(userDetails);
+        authorityList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|*"));
+        when((List<GrantedAuthority>)auth.getAuthorities()).thenReturn(authorityList);
+        when(securityContext.getAuthentication()).thenReturn(auth);
+
+        cldsService.setSecurityContext(securityContext);
+        boolean res = cldsService.isAuthorizedForVf("testId");
+        assertThat(res).isTrue();
+    }
+
+    @Test
+    public void isAuthorizedForVfTest3() throws Exception {
+        when(userDetails.getUsername()).thenReturn("testName");
+        when(auth.getPrincipal()).thenReturn(userDetails);
+        authorityList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|testId"));
+        when((List<GrantedAuthority>)auth.getAuthorities()).thenReturn(authorityList);
+        when(securityContext.getAuthentication()).thenReturn(auth);
+
+        cldsService.setSecurityContext(securityContext);
+        boolean res = cldsService.isAuthorizedForVf("testId");
+        assertThat(res).isTrue();
     }
 
     @Test
-    public void testPutModel() 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);
+    public void isAuthorizedForVfTest4() throws Exception {
+        when(userDetails.getUsername()).thenReturn("testName");
+        when(auth.getPrincipal()).thenReturn(userDetails);
+        authorityList.add(new SimpleGrantedAuthority("permission-type-filter-vf|*|testId"));
+        when((List<GrantedAuthority>)auth.getAuthorities()).thenReturn(authorityList);
+        when(securityContext.getAuthentication()).thenReturn(auth);
+
+        cldsService.setSecurityContext(securityContext);
+        boolean res = cldsService.isAuthorizedForVf("testId");
+        assertThat(res).isTrue();
+    }
+
+    @Test
+    public void getUserIdTest() throws Exception {
+        when(userDetails.getUsername()).thenReturn("testName");
+        when(auth.getPrincipal()).thenReturn(userDetails);
+        when(securityContext.getAuthentication()).thenReturn(auth);
+
         cldsService.setSecurityContext(securityContext);
-        // Add the template first
-        CldsTemplate newTemplate = new CldsTemplate();
-        String randomNameTemplate = RandomStringUtils.randomAlphanumeric(5);
-        newTemplate.setName(randomNameTemplate);
-        newTemplate.setBpmnText(bpmnText);
-        newTemplate.setImageText(imageText);
-        // Save the template in DB
-        cldsDao.setTemplate(newTemplate, "user");
-        // Test if it's well there
-        CldsTemplate newTemplateRead = cldsDao.getTemplate(randomNameTemplate);
-        assertEquals(bpmnText, newTemplateRead.getBpmnText());
-        assertEquals(imageText, newTemplateRead.getImageText());
-        // Save the model
-        CldsModel newModel = new CldsModel();
-        newModel.setName(randomNameTemplate);
-        newModel.setBpmnText(bpmnText);
-        newModel.setImageText(imageText);
-        newModel.setPropText(bpmnPropText);
-        newModel.setControlNamePrefix("ClosedLoop-");
-        newModel.setTemplateName("test-template");
-        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));
+        assertThat(cldsService.getUserId()).isEqualTo("testName");
     }
 }