* ============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.junit.Assert.assertNotNull;
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.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.NotFoundException;
+import javax.xml.transform.TransformerException;
+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.AbstractItCase;
import org.onap.clamp.clds.dao.CldsDao;
-import org.onap.clamp.clds.model.CldsHealthCheck;
+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.test.context.TestPropertySource;
+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;
+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;
/**
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
-@TestPropertySource(locations = "classpath:application-no-camunda.properties")
-public class CldsServiceItCase extends AbstractItCase {
+public class CldsServiceItCase {
+
@Autowired
- CldsService cldsService;
+ private CldsService cldsService;
private String bpmnText;
private String imageText;
private String bpmnPropText;
+ private String docText;
+
@Autowired
- public CldsDao cldsDao;
+ 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
+ * 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");
+ 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() 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());
@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());
}
@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 testGetCldsDetails() throws IOException {
+ List<CldsMonitoringDetails> cldsMonitoringDetailsList = cldsService.getCldsDetails();
+ assertNotNull(cldsMonitoringDetailsList);
}
- @Test
- public void testPutModel() throws Exception {
+ @Test(expected = NotFoundException.class)
+ 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();
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());
+ newModel.setDocText(docText);
// Test the PutModel method
- String randomNameModel = RandomStringUtils.randomAlphanumeric(5);
+
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);
+
+ assertTrue(cldsService.getModelNames().size() >= 1);
+
+ // 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()));
+
+ 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()));
+
+ 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()));
+
+ 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()));
+
+ 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()));
+
+ 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()));
+
+ 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);
+ }
+
+ @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");
+ }
+
+ @Test
+ public void testGetSdcProperties() throws IOException {
+ JSONAssert.assertEquals(
+ ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-global.json"),
+ cldsService.getSdcProperties(), true);
}
}