2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 package org.onap.clamp.clds.it;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
30 import com.att.aft.dme2.internal.apache.commons.lang.RandomStringUtils;
31 import com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
34 import java.io.IOException;
35 import java.security.GeneralSecurityException;
36 import java.util.ArrayList;
37 import java.util.List;
39 import javax.ws.rs.NotFoundException;
41 import org.apache.commons.codec.DecoderException;
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.CLDSMonitoringDetails;
48 import org.onap.clamp.clds.model.CldsDBServiceCache;
49 import org.onap.clamp.clds.model.CldsEvent;
50 import org.onap.clamp.clds.model.CldsModel;
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;
59 * Test CldsDAO calls through CldsModel and CldsEvent. This really test the DB
60 * and stored procedures.
62 @RunWith(SpringRunner.class)
64 public class CldsDaoItCase {
66 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsDao.class);
68 public CldsDao cldsDao;
69 private String bpmnText;
70 private String imageText;
71 private String bpmnPropText;
73 private SdcCatalogServices sdcCatalogServices;
76 * Setup the variable before the tests execution.
79 * In case of issues when opening the files
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");
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());
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());
122 @Test(expected = NotFoundException.class)
123 public void testGetModelNotFound() {
124 CldsModel.retrieve(cldsDao, "test-model-not-found", false);
127 @Test(expected = NotFoundException.class)
128 public void testGetTemplateNotFound() {
129 CldsTemplate.retrieve(cldsDao, "test-template-not-found", false);
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());
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");
159 public void testGetCLDSMonitoringDetails() {
160 List<CLDSMonitoringDetails> cldsMonitoringDetailsList = new ArrayList<CLDSMonitoringDetails>();
161 cldsMonitoringDetailsList = cldsDao.getCLDSMonitoringDetails();
162 cldsMonitoringDetailsList.forEach(clName -> {
163 logger.info(clName.getCloseloopName()); // Uncomment this line to
164 // view the result on
166 assertNotNull(clName.getCloseloopName());
171 public void testCldsServiceCache() throws GeneralSecurityException, DecoderException, IOException {
172 CldsServiceData cldsServiceData = sdcCatalogServices
173 .getCldsServiceDataWithAlarmConditions("4cc5b45a-1f63-4194-8100-cd8e14248c92");
174 // Test not in cache so should be null
175 CldsServiceData cldsServiceDataCache = cldsDao.getCldsServiceCache("4cc5b45a-1f63-4194-8100-cd8e14248c92");
176 assertNull(cldsServiceDataCache);
177 cldsDao.setCldsServiceCache(new CldsDBServiceCache(cldsServiceData));
178 cldsServiceDataCache = cldsDao.getCldsServiceCache("4cc5b45a-1f63-4194-8100-cd8e14248c92");
179 assertNotNull(cldsServiceDataCache);
180 assertEquals("56441b4b-0467-41dc-9a0e-e68613838219", cldsServiceDataCache.getServiceUUID());
181 assertEquals("4cc5b45a-1f63-4194-8100-cd8e14248c92", cldsServiceDataCache.getServiceInvariantUUID());
182 assertEquals(2, cldsServiceDataCache.getCldsVfs().size());
183 assertNotNull(cldsServiceDataCache.getAgeOfRecord());
184 assertEquals(4, cldsServiceDataCache.getCldsVfs().get(0).getCldsVfcs().size());
185 assertEquals("07e266fc-49ab-4cd7-8378-ca4676f1b9ec",
186 cldsServiceDataCache.getCldsVfs().get(0).getVfInvariantResourceUUID());
187 assertEquals(0, cldsServiceDataCache.getCldsVfs().get(0).getCldsKPIList().size());
189 cldsServiceData.setCldsVfs(null);
190 cldsDao.setCldsServiceCache(new CldsDBServiceCache(cldsServiceData));
191 cldsServiceDataCache = cldsDao.getCldsServiceCache("4cc5b45a-1f63-4194-8100-cd8e14248c92");
192 assertNotNull(cldsServiceDataCache);
193 assertNull(cldsServiceDataCache.getCldsVfs());
194 cldsDao.clearServiceCache();