12fa2932cc34b247cf50d327630c37ed9748bd99
[clamp.git] / src / test / java / org / onap / clamp / clds / it / CldsDaoItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  *
22  */
23
24 package org.onap.clamp.clds.it;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29
30 import com.att.eelf.configuration.EELFLogger;
31 import com.att.eelf.configuration.EELFManager;
32
33 import java.io.IOException;
34 import java.security.GeneralSecurityException;
35 import java.util.ArrayList;
36 import java.util.List;
37
38 import javax.ws.rs.NotFoundException;
39
40 import org.apache.commons.codec.DecoderException;
41 import org.apache.commons.lang3.RandomStringUtils;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.onap.clamp.clds.client.req.sdc.SdcCatalogServices;
46 import org.onap.clamp.clds.dao.CldsDao;
47 import org.onap.clamp.clds.model.CldsDbServiceCache;
48 import org.onap.clamp.clds.model.CldsEvent;
49 import org.onap.clamp.clds.model.CldsModel;
50 import org.onap.clamp.clds.model.CldsMonitoringDetails;
51 import org.onap.clamp.clds.model.CldsServiceData;
52 import org.onap.clamp.clds.model.CldsTemplate;
53 import org.onap.clamp.clds.util.ResourceFileUtil;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.boot.test.context.SpringBootTest;
56 import org.springframework.test.context.junit4.SpringRunner;
57
58 /**
59  * Test CldsDAO calls through CldsModel and CldsEvent. This really test the DB
60  * and stored procedures.
61  */
62 @RunWith(SpringRunner.class)
63 @SpringBootTest
64 public class CldsDaoItCase {
65
66     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsDao.class);
67     @Autowired
68     public CldsDao cldsDao;
69     private String bpmnText;
70     private String imageText;
71     private String bpmnPropText;
72     @Autowired
73     private SdcCatalogServices sdcCatalogServices;
74
75     /**
76      * Setup the variable before the tests execution.
77      *
78      * @throws IOException
79      *         In case of issues when opening the files
80      */
81     @Before
82     public void setupBefore() throws IOException {
83         bpmnText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-template.xml");
84         imageText = ResourceFileUtil.getResourceAsString("example/dao/image-template.xml");
85         bpmnPropText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-prop.json");
86     }
87
88     @Test
89     public void testModelSave() {
90         String randomNameTemplate = RandomStringUtils.randomAlphanumeric(5);
91         // Add the template first
92         CldsTemplate newTemplate = new CldsTemplate();
93         newTemplate.setName(randomNameTemplate);
94         newTemplate.setBpmnText(bpmnText);
95         newTemplate.setImageText(imageText);
96         // Save the template in DB
97         cldsDao.setTemplate(newTemplate, "user");
98         // Test if it's well there
99         CldsTemplate newTemplateRead = cldsDao.getTemplate(randomNameTemplate);
100         assertEquals(bpmnText, newTemplateRead.getBpmnText());
101         assertEquals(imageText, newTemplateRead.getImageText());
102         // Save the model
103         CldsModel newModel = new CldsModel();
104         String randomNameModel = RandomStringUtils.randomAlphanumeric(5);
105         newModel.setName(randomNameModel);
106         newModel.setBpmnText(bpmnText);
107         newModel.setImageText(imageText);
108         newModel.setPropText(bpmnPropText);
109         newModel.setControlNamePrefix("ClosedLoop-");
110         newModel.setTemplateName(randomNameTemplate);
111         newModel.setTemplateId(newTemplate.getId());
112         newModel.setDocText(newTemplate.getPropText());
113         // Save the model in DB
114         cldsDao.setModel(newModel, "user");
115         // Test if the model can be retrieved
116         CldsModel newCldsModel = cldsDao.getModelTemplate(randomNameModel);
117         assertEquals(bpmnText, newCldsModel.getBpmnText());
118         assertEquals(imageText, newCldsModel.getImageText());
119         assertEquals(bpmnPropText, newCldsModel.getPropText());
120     }
121
122     @Test(expected = NotFoundException.class)
123     public void testGetModelNotFound() {
124         CldsModel.retrieve(cldsDao, "test-model-not-found", false);
125     }
126
127     @Test(expected = NotFoundException.class)
128     public void testGetTemplateNotFound() {
129         CldsTemplate.retrieve(cldsDao, "test-template-not-found", false);
130     }
131
132     @Test
133     public void testInsEvent() {
134         // Add the template first
135         CldsTemplate newTemplate = new CldsTemplate();
136         newTemplate.setName("test-template-for-event");
137         newTemplate.setBpmnText(bpmnText);
138         newTemplate.setImageText(imageText);
139         newTemplate.save(cldsDao, "user");
140         // Test if it's well there
141         CldsTemplate newTemplateRead = CldsTemplate.retrieve(cldsDao, "test-template-for-event", false);
142         assertEquals(bpmnText, newTemplateRead.getBpmnText());
143         assertEquals(imageText, newTemplateRead.getImageText());
144         // Save the model
145         CldsModel newModel = new CldsModel();
146         newModel.setName("test-model-for-event");
147         newModel.setBpmnText(bpmnText);
148         newModel.setImageText(imageText);
149         newModel.setPropText(bpmnPropText);
150         newModel.setControlNamePrefix("ClosedLoop-");
151         newModel.setTemplateName("test-template-for-event");
152         newModel.setTemplateId(newTemplate.getId());
153         newModel.setDocText(newTemplate.getPropText());
154         CldsEvent.insEvent(cldsDao, newModel, "user", CldsEvent.ACTION_RESTART, CldsEvent.ACTION_STATE_COMPLETED,
155             "process-instance-id");
156     }
157
158     @Test
159     public void testGetCldsMonitoringDetails() {
160         List<CldsMonitoringDetails> cldsMonitoringDetailsList = new ArrayList<CldsMonitoringDetails>();
161         cldsMonitoringDetailsList = cldsDao.getCLDSMonitoringDetails();
162         cldsMonitoringDetailsList.forEach(clName -> {
163             logger.info(clName.getCloseloopName());
164             assertNotNull(clName.getCloseloopName());
165         });
166     }
167
168     @Test
169     public void testCldsServiceCache() throws GeneralSecurityException, DecoderException, IOException {
170         CldsServiceData cldsServiceData = sdcCatalogServices
171             .getCldsServiceDataWithAlarmConditions("4cc5b45a-1f63-4194-8100-cd8e14248c92");
172         // Test not in cache so should be null
173         CldsServiceData cldsServiceDataCache = cldsDao.getCldsServiceCache("4cc5b45a-1f63-4194-8100-cd8e14248c92");
174         assertNull(cldsServiceDataCache);
175         cldsDao.setCldsServiceCache(new CldsDbServiceCache(cldsServiceData));
176         cldsServiceDataCache = cldsDao.getCldsServiceCache("4cc5b45a-1f63-4194-8100-cd8e14248c92");
177         assertNotNull(cldsServiceDataCache);
178         assertEquals("56441b4b-0467-41dc-9a0e-e68613838219", cldsServiceDataCache.getServiceUUID());
179         assertEquals("4cc5b45a-1f63-4194-8100-cd8e14248c92", cldsServiceDataCache.getServiceInvariantUUID());
180         assertEquals(2, cldsServiceDataCache.getCldsVfs().size());
181         assertNotNull(cldsServiceDataCache.getAgeOfRecord());
182         assertEquals(4, cldsServiceDataCache.getCldsVfs().get(0).getCldsVfcs().size());
183         assertEquals("07e266fc-49ab-4cd7-8378-ca4676f1b9ec",
184             cldsServiceDataCache.getCldsVfs().get(0).getVfInvariantResourceUUID());
185         assertEquals(0, cldsServiceDataCache.getCldsVfs().get(0).getCldsKPIList().size());
186         // Second update
187         cldsServiceData.setCldsVfs(null);
188         cldsDao.setCldsServiceCache(new CldsDbServiceCache(cldsServiceData));
189         cldsServiceDataCache = cldsDao.getCldsServiceCache("4cc5b45a-1f63-4194-8100-cd8e14248c92");
190         assertNotNull(cldsServiceDataCache);
191         assertNull(cldsServiceDataCache.getCldsVfs());
192         cldsDao.clearServiceCache();
193     }
194 }