Code refactoring
[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() {
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         // Save the model in DB
101         cldsDao.setModel(newModel, "user");
102         // Test if the model can be retrieved
103         CldsModel newCldsModel = cldsDao.getModelTemplate(randomNameModel);
104         assertEquals(bpmnText, newCldsModel.getBpmnText());
105         assertEquals(imageText, newCldsModel.getImageText());
106         assertEquals(bpmnPropText, newCldsModel.getPropText());
107     }
108
109     @Test(expected = NotFoundException.class)
110     public void testGetModelNotFound() {
111         CldsModel.retrieve(cldsDao, "test-model-not-found", false);
112     }
113
114     @Test(expected = NotFoundException.class)
115     public void testGetTemplateNotFound() {
116         CldsTemplate.retrieve(cldsDao, "test-template-not-found", false);
117     }
118
119     @Test
120     public void testInsEvent() {
121         // Add the template first
122         CldsTemplate newTemplate = new CldsTemplate();
123         newTemplate.setName("test-template-for-event");
124         newTemplate.setBpmnText(bpmnText);
125         newTemplate.setImageText(imageText);
126         newTemplate.save(cldsDao, "user");
127         // Test if it's well there
128         CldsTemplate newTemplateRead = CldsTemplate.retrieve(cldsDao, "test-template-for-event", false);
129         assertEquals(bpmnText, newTemplateRead.getBpmnText());
130         assertEquals(imageText, newTemplateRead.getImageText());
131         // Save the model
132         CldsModel newModel = new CldsModel();
133         newModel.setName("test-model-for-event");
134         newModel.setBpmnText(bpmnText);
135         newModel.setImageText(imageText);
136         newModel.setPropText(bpmnPropText);
137         newModel.setControlNamePrefix("ClosedLoop-");
138         newModel.setTemplateName("test-template-for-event");
139         newModel.setTemplateId(newTemplate.getId());
140         newModel.setDocText(newTemplate.getPropText());
141         CldsEvent.insEvent(cldsDao, newModel, "user", CldsEvent.ACTION_RESTART, CldsEvent.ACTION_STATE_COMPLETED,
142                 "process-instance-id");
143     }
144 }