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