Fix for Checkstyle
[clamp.git] / src / test / java / org / onap / clamp / clds / it / CldsDaoItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds.it;
25
26 import static org.junit.Assert.assertEquals;
27
28 import com.att.aft.dme2.internal.apache.commons.lang.RandomStringUtils;
29
30 import java.io.IOException;
31
32 import javax.ws.rs.NotFoundException;
33
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.onap.clamp.clds.AbstractItCase;
38 import org.onap.clamp.clds.dao.CldsDao;
39 import org.onap.clamp.clds.model.CldsEvent;
40 import org.onap.clamp.clds.model.CldsModel;
41 import org.onap.clamp.clds.model.CldsTemplate;
42 import org.onap.clamp.clds.util.ResourceFileUtil;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.boot.test.context.SpringBootTest;
45 import org.springframework.test.context.TestPropertySource;
46 import org.springframework.test.context.junit4.SpringRunner;
47
48 /**
49  * Test CldsDAO calls through CldsModel and CldsEvent. This really test the DB
50  * and stored procedures.
51  */
52 @RunWith(SpringRunner.class)
53 @SpringBootTest
54 @TestPropertySource(locations = "classpath:application-no-camunda.properties")
55 public class CldsDaoItCase extends AbstractItCase {
56     @Autowired
57     public CldsDao cldsDao;
58     private String bpmnText;
59     private String imageText;
60     private String bpmnPropText;
61
62     /**
63      * Setup the variable before the tests execution.
64      * 
65      * @throws IOException
66      *             In case of issues when opening the files
67      */
68     @Before
69     public void setupBefore() throws IOException {
70         bpmnText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-template.xml");
71         imageText = ResourceFileUtil.getResourceAsString("example/dao/image-template.xml");
72         bpmnPropText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-prop.json");
73     }
74
75     @Test
76     public void testModelSave() throws IOException {
77         String randomNameTemplate = RandomStringUtils.randomAlphanumeric(5);
78         // Add the template first
79         CldsTemplate newTemplate = new CldsTemplate();
80         newTemplate.setName(randomNameTemplate);
81         newTemplate.setBpmnText(bpmnText);
82         newTemplate.setImageText(imageText);
83         // Save the template in DB
84         cldsDao.setTemplate(newTemplate, "user");
85         // Test if it's well there
86         CldsTemplate newTemplateRead = cldsDao.getTemplate(randomNameTemplate);
87         assertEquals(bpmnText, newTemplateRead.getBpmnText());
88         assertEquals(imageText, newTemplateRead.getImageText());
89         // Save the model
90         CldsModel newModel = new CldsModel();
91         String randomNameModel = RandomStringUtils.randomAlphanumeric(5);
92         newModel.setName(randomNameModel);
93         newModel.setBpmnText(bpmnText);
94         newModel.setImageText(imageText);
95         newModel.setPropText(bpmnPropText);
96         newModel.setControlNamePrefix("ClosedLoop-");
97         newModel.setTemplateName(randomNameTemplate);
98         newModel.setTemplateId(newTemplate.getId());
99         newModel.setDocText(newTemplate.getPropText());
100         newModel.setDocId(newTemplate.getPropId());
101         // Save the model in DB
102         cldsDao.setModel(newModel, "user");
103         // Test if the model can be retrieved
104         CldsModel newCldsModel = cldsDao.getModelTemplate(randomNameModel);
105         assertEquals(bpmnText, newCldsModel.getBpmnText());
106         assertEquals(imageText, newCldsModel.getImageText());
107         assertEquals(bpmnPropText, newCldsModel.getPropText());
108     }
109
110     @Test(expected = NotFoundException.class)
111     public void testGetModelNotFound() {
112         CldsModel.retrieve(cldsDao, "test-model-not-found", false);
113     }
114
115     @Test(expected = NotFoundException.class)
116     public void testGetTemplateNotFound() {
117         CldsTemplate.retrieve(cldsDao, "test-template-not-found", false);
118     }
119
120     @Test
121     public void testInsEvent() {
122         // Add the template first
123         CldsTemplate newTemplate = new CldsTemplate();
124         newTemplate.setName("test-template-for-event");
125         newTemplate.setBpmnText(bpmnText);
126         newTemplate.setImageText(imageText);
127         newTemplate.save(cldsDao, "user");
128         // Test if it's well there
129         CldsTemplate newTemplateRead = CldsTemplate.retrieve(cldsDao, "test-template-for-event", false);
130         assertEquals(bpmnText, newTemplateRead.getBpmnText());
131         assertEquals(imageText, newTemplateRead.getImageText());
132         // Save the model
133         CldsModel newModel = new CldsModel();
134         newModel.setName("test-model-for-event");
135         newModel.setBpmnText(bpmnText);
136         newModel.setImageText(imageText);
137         newModel.setPropText(bpmnPropText);
138         newModel.setControlNamePrefix("ClosedLoop-");
139         newModel.setTemplateName("test-template-for-event");
140         newModel.setTemplateId(newTemplate.getId());
141         newModel.setDocText(newTemplate.getPropText());
142         newModel.setDocId(newTemplate.getPropId());
143         CldsEvent.insEvent(cldsDao, newModel, "user", CldsEvent.ACTION_RESTART, CldsEvent.ACTION_STATE_COMPLETED,
144                 "process-instance-id");
145     }
146 }