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 static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
-import javax.ws.rs.NotFoundException;
-import javax.xml.transform.TransformerException;
+import javax.ws.rs.NotAuthorizedException;
-import org.apache.commons.lang3.RandomStringUtils;
-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.dao.CldsDao;
-import org.onap.clamp.clds.model.CldsEvent;
import org.onap.clamp.clds.model.CldsInfo;
-import org.onap.clamp.clds.model.CldsModel;
-import org.onap.clamp.clds.model.CldsMonitoringDetails;
-import org.onap.clamp.clds.model.CldsTemplate;
-import org.onap.clamp.clds.model.DcaeEvent;
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.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
@Autowired
private CldsService cldsService;
- private String bpmnText;
- private String imageText;
- private String bpmnPropText;
- private String docText;
- @Autowired
- private CldsDao cldsDao;
- private Authentication authentication;
- private List<GrantedAuthority> authList = new LinkedList<GrantedAuthority>();
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/model-properties/tca_new/tca-template.xml");
- imageText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/tca-img.xml");
- bpmnPropText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/model-properties.json");
- docText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/doc-text.yaml");
-
- 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);
-
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() {
- SecurityContext securityContext = Mockito.mock(SecurityContext.class);
- 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);
+ Mockito.when(securityContext.getAuthentication()).thenReturn(auth);
+ Mockito.when(auth.getPrincipal()).thenReturn(userDetails);
cldsService.setSecurityContext(securityContext);
CldsInfo cldsInfo = cldsService.getCldsInfo();
@Test
public void testCldsInfoAuthorized() throws Exception {
- SecurityContext securityContext = Mockito.mock(SecurityContext.class);
+ 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);
assertEquals(cldsInfo.getUserName(), "admin");
}
- @Test
- public void testGetCldsDetails() throws IOException {
- List<CldsMonitoringDetails> cldsMonitoringDetailsList = cldsService.getCldsDetails();
- assertNotNull(cldsMonitoringDetailsList);
+ @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 = NotFoundException.class)
- public void testCompleteFlow() throws TransformerException, ParseException {
- SecurityContext securityContext = Mockito.mock(SecurityContext.class);
- Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
-
+ @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);
- // 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
- String randomNameModel = RandomStringUtils.randomAlphanumeric(5);
- CldsModel newModel = new CldsModel();
- newModel.setName(randomNameModel);
- newModel.setBpmnText(bpmnText);
- newModel.setImageText(imageText);
- newModel.setPropText(bpmnPropText);
- newModel.setControlNamePrefix("ClosedLoop-");
- newModel.setTemplateName(randomNameTemplate);
- newModel.setTemplateId(newTemplate.getId());
- newModel.setDocText(docText);
- // Test the PutModel method
-
- cldsService.putModel(randomNameModel, newModel);
-
- assertEquals(bpmnText, cldsService.getBpmnXml(randomNameModel));
- assertEquals(imageText, cldsService.getImageXml(randomNameModel));
-
- // Verify whether it has been added properly or not
- assertNotNull(cldsDao.getModel(randomNameModel));
-
- CldsModel model = cldsService.getModel(randomNameModel);
- // Verify with GetModel
- assertEquals(model.getTemplateName(), randomNameTemplate);
- assertEquals(model.getName(), randomNameModel);
+ boolean res = cldsService.isAuthorizedForVf("testId");
+ assertThat(res).isTrue();
+ }
- assertTrue(cldsService.getModelNames().size() >= 1);
+ @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();
+ }
- // Should fail
- ResponseEntity<?> responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_SUBMIT,
- randomNameModel, "false", cldsService.getModel(randomNameModel));
- assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
- assertNotNull(responseEntity.getBody());
- assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(((CldsModel) responseEntity.getBody()).getStatus()));
- assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(cldsService.getModel(randomNameModel).getStatus()));
+ @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();
+ }
- responseEntity = cldsService.deployModel(randomNameModel, cldsService.getModel(randomNameModel));
- assertNotNull(responseEntity);
- assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
- assertNotNull(responseEntity.getBody());
- assertTrue(CldsModel.STATUS_ACTIVE.equals(((CldsModel) responseEntity.getBody()).getStatus()));
- assertTrue(CldsModel.STATUS_ACTIVE.equals(cldsService.getModel(randomNameModel).getStatus()));
+ @Test
+ 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);
- responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_STOP, randomNameModel, "false",
- cldsService.getModel(randomNameModel));
- assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
- assertNotNull(responseEntity.getBody());
- assertTrue(CldsModel.STATUS_STOPPED.equals(((CldsModel) responseEntity.getBody()).getStatus()));
- assertTrue(CldsModel.STATUS_STOPPED.equals(cldsService.getModel(randomNameModel).getStatus()));
+ cldsService.setSecurityContext(securityContext);
+ boolean res = cldsService.isAuthorizedForVf("testId");
+ assertThat(res).isTrue();
+ }
- responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_RESTART, randomNameModel, "false",
- cldsService.getModel(randomNameModel));
- assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
- assertNotNull(responseEntity.getBody());
- assertTrue(CldsModel.STATUS_ACTIVE.equals(((CldsModel) responseEntity.getBody()).getStatus()));
- assertTrue(CldsModel.STATUS_ACTIVE.equals(cldsService.getModel(randomNameModel).getStatus()));
+ @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);
- responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_UPDATE, randomNameModel, "false",
- cldsService.getModel(randomNameModel));
- assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
- assertNotNull(responseEntity.getBody());
- assertTrue(CldsModel.STATUS_ACTIVE.equals(((CldsModel) responseEntity.getBody()).getStatus()));
- assertTrue(CldsModel.STATUS_ACTIVE.equals(cldsService.getModel(randomNameModel).getStatus()));
+ cldsService.setSecurityContext(securityContext);
+ boolean res = cldsService.isAuthorizedForVf("testId");
+ assertThat(res).isTrue();
+ }
- responseEntity = cldsService.unDeployModel(randomNameModel, cldsService.getModel(randomNameModel));
- assertNotNull(responseEntity);
- assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
- assertNotNull(responseEntity.getBody());
- assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(((CldsModel) responseEntity.getBody()).getStatus()));
- assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(cldsService.getModel(randomNameModel).getStatus()));
+ @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);
- responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_DELETE, randomNameModel, "false",
- cldsService.getModel(randomNameModel));
- assertNotNull(responseEntity);
- assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
- assertNotNull(responseEntity.getBody());
- // This will raise an exception
- cldsService.getModel(randomNameModel);
+ cldsService.setSecurityContext(securityContext);
+ boolean res = cldsService.isAuthorizedForVf("testId");
+ assertThat(res).isTrue();
}
@Test
- public void testDcaePost() {
- DcaeEvent dcaeEvent = new DcaeEvent();
- dcaeEvent.setArtifactName("ClosedLoop_with-enough-characters_TestArtifact.yml");
- dcaeEvent.setEvent(DcaeEvent.EVENT_CREATED);
- dcaeEvent.setResourceUUID("1");
- dcaeEvent.setServiceUUID("2");
- assertEquals(cldsService.postDcaeEvent("false", dcaeEvent),
- "event=created serviceUUID=2 resourceUUID=1 artifactName="
- + "ClosedLoop_with-enough-characters_TestArtifact.yml instance count=0 isTest=false");
+ 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 testGetSdcProperties() throws IOException {
- JSONAssert.assertEquals(
- ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-global.json"),
- cldsService.getSdcProperties(), true);
+ public void getUserIdTest() throws Exception {
+ when(userDetails.getUsername()).thenReturn("testName");
+ when(auth.getPrincipal()).thenReturn(userDetails);
+ when(securityContext.getAuthentication()).thenReturn(auth);
+
+ cldsService.setSecurityContext(securityContext);
+ assertThat(cldsService.getUserId()).isEqualTo("testName");
}
}