e780a259ea0a7c098a0b8d75ba0ccdfd1f7049ef
[so.git] / asdc-controller / src / test / java / org / onap / so / asdc / installer / bpmn / BpmnInstallerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.asdc.installer.bpmn;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.ArgumentMatchers.anyString;
30 import static org.mockito.Mockito.doReturn;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.spy;
33 import static org.mockito.Mockito.times;
34 import static org.mockito.Mockito.verify;
35
36 import java.io.File;
37 import java.io.FileInputStream;
38 import java.io.InputStream;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 import javax.transaction.Transactional;
43 import org.apache.commons.io.IOUtils;
44 import org.apache.http.HttpEntity;
45 import org.apache.http.HttpResponse;
46 import org.apache.http.ProtocolVersion;
47 import org.apache.http.client.HttpClient;
48 import org.apache.http.client.methods.HttpPost;
49 import org.apache.http.message.BasicHttpResponse;
50 import org.apache.http.message.BasicStatusLine;
51 import org.junit.AfterClass;
52 import org.junit.Before;
53 import org.junit.Rule;
54 import org.junit.Test;
55 import org.junit.rules.TemporaryFolder;
56
57 @Transactional
58 public class BpmnInstallerTest {
59
60     private BpmnInstaller bpmnInstaller = new BpmnInstaller();
61
62     private static final String TEST_CSAR = "src/test/resources/resource-examples/WorkflowBpmn/service-CxSvc-csar.csar";
63     private Path tempDirectoryPath;
64
65     @Rule
66     public TemporaryFolder folder = new TemporaryFolder();
67
68     @Before
69     public void init() throws Exception {
70         System.setProperty("mso.config.path", folder.getRoot().toString());
71         // we need to have this directory created for InstallBPMN test success
72         tempDirectoryPath = Paths.get(folder.getRoot().toString(), "ASDC");
73         Files.createDirectories(tempDirectoryPath);
74     }
75
76     @AfterClass
77     public static void cleanup() {
78         System.clearProperty("mso.config.path");
79     }
80     
81     @Test
82     public void buildMimeMultiPart_Test() throws Exception {
83         Path tempFilePath = Paths.get(tempDirectoryPath.toAbsolutePath().toString(), "TestBB.bpmn");
84         Files.createFile(tempFilePath);
85         HttpEntity entity = bpmnInstaller.buildMimeMultipart("TestBB.bpmn");            
86         String mimeMultipartBodyFilePath = "src/test/resources" + "/mime-multipart-body.txt";
87         
88         File mimeMultipartBody = new File(mimeMultipartBodyFilePath);
89         InputStream expectedContent = new FileInputStream(mimeMultipartBody);
90         
91         assertThat(IOUtils.contentEquals(expectedContent, entity.getContent()));
92         
93         IOUtils.closeQuietly(expectedContent);
94     }
95
96     @Test
97     public void installBpmn_Test() throws Exception {
98         BpmnInstaller bpmnInstallerSpy = spy(bpmnInstaller);
99         HttpResponse response = new BasicHttpResponse(
100             new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, ""));
101         HttpClient httpClient = mock(HttpClient.class);
102         doReturn(response).when(httpClient).execute(any(HttpPost.class));
103         bpmnInstallerSpy.installBpmn(TEST_CSAR);
104         verify(bpmnInstallerSpy, times(1)).sendDeploymentRequest(anyString());
105     }
106
107     @Test
108     public void containsWorkflowsSuccess() {
109         boolean result = bpmnInstaller.containsWorkflows(TEST_CSAR);
110         assertTrue(result);
111     }
112
113     @Test
114     public void containsWorkflowsFailure() {
115         boolean result = bpmnInstaller.containsWorkflows("DOESNOTEXIST.csar");
116         assertFalse(result);
117     }
118 }