Remove SDC query
[clamp.git] / src / test / java / org / onap / clamp / clds / it / CldsServiceItCase.java
index 3c508bd..347de4a 100644 (file)
@@ -26,23 +26,19 @@ 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.GeneralSecurityException;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Properties;
 
 import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.NotFoundException;
 import javax.xml.transform.TransformerException;
 
-import org.apache.commons.codec.DecoderException;
-import org.json.JSONException;
+import org.apache.commons.lang3.RandomStringUtils;
 import org.json.simple.parser.ParseException;
 import org.junit.Before;
 import org.junit.Test;
@@ -50,10 +46,12 @@ 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.CldsServiceData;
+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;
@@ -61,6 +59,8 @@ 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;
@@ -82,22 +82,26 @@ public class CldsServiceItCase {
     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 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"));
@@ -105,11 +109,13 @@ public class CldsServiceItCase {
         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);
+        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
@@ -149,6 +155,12 @@ public class CldsServiceItCase {
     }
 
     @Test
+    public void testGetCldsDetails() throws IOException {
+        List<CldsMonitoringDetails> cldsMonitoringDetailsList = cldsService.getCldsDetails();
+        assertNotNull(cldsMonitoringDetailsList);
+    }
+
+    @Test(expected = NotFoundException.class)
     public void testCompleteFlow() throws TransformerException, ParseException {
         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
@@ -176,49 +188,92 @@ public class CldsServiceItCase {
         newModel.setControlNamePrefix("ClosedLoop-");
         newModel.setTemplateName(randomNameTemplate);
         newModel.setTemplateId(newTemplate.getId());
-        newModel.setDocText(newTemplate.getPropText());
+        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(cldsService.getModel(randomNameModel).getTemplateName(),randomNameTemplate);
-        assertEquals(cldsService.getModel(randomNameModel).getName(),randomNameModel);
+        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 testGetSdcServices() throws GeneralSecurityException, DecoderException, JSONException, IOException {
-        String result = cldsService.getSdcServices();
-        JSONAssert.assertEquals(
-            ResourceFileUtil.getResourceAsString("example/sdc/expected-result/all-sdc-services.json"), result,
-            true);
+    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 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);
+    public void testGetSdcProperties() throws IOException {
         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();
+            ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-global.json"),
+            cldsService.getSdcProperties(), true);
     }
 }