Unit tests
[vid.git] / vid-app-common / src / test / java / org / onap / vid / asdc / BaseClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID ASDC Client
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.asdc;
22
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.Map;
26 import java.util.UUID;
27
28 import javax.ws.rs.NotFoundException;
29
30 import org.hamcrest.core.IsEqual;
31 import org.junit.Assert;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.rules.ErrorCollector;
35 import org.onap.vid.asdc.beans.Artifact;
36 import org.onap.vid.asdc.beans.Resource;
37 import org.onap.vid.asdc.beans.Service;
38 import org.onap.vid.asdc.beans.Service.DistributionStatus;
39 import org.onap.vid.asdc.beans.tosca.Group;
40 import org.onap.vid.asdc.beans.tosca.Input;
41 import org.onap.vid.asdc.beans.tosca.NodeTemplate;
42 import org.onap.vid.asdc.beans.tosca.ToscaCsar;
43 import org.onap.vid.asdc.beans.tosca.ToscaModel;
44
45 /**
46  * The Class BaseClientTest.
47  */
48 public class BaseClientTest {
49
50     /** The collector. */
51     @Rule
52     public ErrorCollector collector = new ErrorCollector();
53     
54         /**
55          * Run resource tests.
56          *
57          * @param client the client
58          * @throws AsdcCatalogException the asdc catalog exception
59          */
60         protected void runResourceTests(AsdcClient client) throws AsdcCatalogException {
61                 final Collection<Resource> resources = client.getResources();
62                 
63                 collector.checkThat("getResources() returned nothing", resources.isEmpty(), IsEqual.equalTo(false));
64                 
65                 final Resource resource = resources.iterator().next();
66                 
67                 testResource(resource);
68                 
69                 final Resource thisResource = client.getResource(UUID.fromString(resource.getUuid()));
70                 
71                 collector.checkThat(thisResource, IsEqual.equalTo(resource));
72                 
73                 for (Resource aResource : resources) {
74                         if (aResource.getArtifacts() != null && !aResource.getArtifacts().isEmpty()) {
75                         
76                                 final Artifact artifact = aResource.getArtifacts().iterator().next();
77                                 
78                                 testArtifact(artifact);
79                                 
80                                 final UUID resourceUuid = UUID.fromString(aResource.getUuid());
81                                 final UUID artifactUuid = UUID.fromString(artifact.getArtifactUUID());
82                                 final Artifact thisArtifact = client.getResourceArtifact(resourceUuid, artifactUuid);
83                                 
84                                 collector.checkThat(artifact, IsEqual.equalTo(thisArtifact));
85                         }
86                 }
87                 
88                 try {
89                         final Collection<Resource> badResources = client.getResources(Collections.singletonMap("category", new String[] {"Bad Resources"}));
90                         
91                         for (Resource badResource : badResources) {
92                                 collector.checkThat(badResource.getCategory(), IsEqual.equalTo("Bad Resources"));
93                         }
94                 } catch (NotFoundException e) {
95                         //No resources of this category were found
96                 }
97                 
98                 try {
99                         final Collection<Resource> reallyBadResources = client.getResources(Collections.singletonMap("subCategory", new String[] {"Really Bad Resources"}));
100                         
101                         for (Resource reallyBadResource : reallyBadResources) {
102                                 collector.checkThat(reallyBadResource.getSubCategory(), IsEqual.equalTo("Really Bad Resources"));
103                         }
104                 } catch (NotFoundException e) {
105                         //No resources of this subcategory were found
106                 }
107                 
108                 /*final ToscaCsar toscaCsar = client.getResourceToscaModel(UUID.fromString(resource.getUuid()));
109                 
110                 testToscaCsar(toscaCsar);*/
111         }
112         
113         /**
114          * Run service tests.
115          *
116          * @param client the client
117          * @throws AsdcCatalogException the asdc catalog exception
118          */
119         protected void runServiceTests(AsdcClient client) throws AsdcCatalogException {
120                 final Collection<Service> services = client.getServices();
121                 
122                 collector.checkThat("getServices() returned nothing", services.isEmpty(), IsEqual.equalTo(false));
123                 
124                 final Service service = services.iterator().next();
125                 
126                 testService(service);
127                 
128                 final Service thisService = client.getService(UUID.fromString(service.getUuid()));
129                 
130                 collector.checkThat(thisService, IsEqual.equalTo(service));
131                 
132                 for (Service aService : services) {
133                         if (aService.getArtifacts() != null && ! aService.getArtifacts().isEmpty()) {
134                                 final Artifact artifact = aService.getArtifacts().iterator().next();
135                                 
136                                 testArtifact(artifact);
137                                 
138                                 final UUID serviceUuid = UUID.fromString(aService.getUuid());
139                                 final UUID artifactUuid = UUID.fromString(artifact.getArtifactUUID());
140                                 final Artifact thisArtifact = client.getServiceArtifact(serviceUuid, artifactUuid);
141                                 
142                                 collector.checkThat(artifact, IsEqual.equalTo(thisArtifact));
143                                 break;
144                         }
145                 }
146
147                 try {
148                         final Collection<Service> distributedServices = client.getServices(Collections.singletonMap("distributionStatus", new String[] {"DISTRIBUTED"}));
149                         
150                         for (Service distributedService : distributedServices) {
151                                 collector.checkThat(distributedService.getDistributionStatus(), IsEqual.equalTo(DistributionStatus.DISTRIBUTED));
152                         }
153                 } catch (NotFoundException e) {
154                         //No services of this distributionStatus were found
155                 }
156
157                 try {
158                         final Collection<Service> badServices = client.getServices(Collections.singletonMap("category", new String[] {"Bad Services"}));
159
160                         for (Service badService : badServices) {
161                                 collector.checkThat(badService.getCategory(), IsEqual.equalTo("Bad Services"));
162                         }
163                 } catch (NotFoundException e) {
164                         //No services of this category were found
165                 }
166                 
167                 /*final ToscaCsar toscaCsar = client.getServiceToscaModel(UUID.fromString(service.getUuid()));
168                 
169                 testToscaCsar(toscaCsar);*/
170         }
171         
172         /**
173          * Test service.
174          *
175          * @param service the service
176          */
177         private void testService(Service service) {
178                 service.getArtifacts();
179                 service.getCategory();
180                 service.getDistributionStatus();
181                 service.getInvariantUUID();
182                 service.getLastUpdaterUserId();
183                 service.getLastUpdaterFullName();
184                 service.getLifecycleState();
185                 service.getName();
186                 service.getResources();
187                 service.getToscaModelURL();
188                 service.getUuid();
189                 service.getVersion();
190         }
191         
192         /**
193          * Test resource.
194          *
195          * @param resource the resource
196          */
197         private void testResource(Resource resource) {
198                 resource.getArtifacts();
199                 resource.getCategory();
200                 resource.getInvariantUUID();
201                 resource.getLastUpdaterUserId();
202                 resource.getLastUpdaterFullName();
203                 resource.getLifecycleState();
204                 resource.getName();
205                 resource.getResources();
206                 resource.getResourceType();
207                 resource.getSubCategory();
208                 resource.getToscaModel();
209                 resource.getToscaModelURL();
210                 resource.getToscaResourceName();
211                 resource.getUuid();
212                 resource.getVersion();
213         }
214         
215         /**
216          * Test artifact.
217          *
218          * @param artifact the artifact
219          */
220         private void testArtifact(Artifact artifact) {
221                 artifact.getArtifactChecksum();
222                 artifact.getArtifactDescription();
223                 artifact.getArtifactName();
224                 artifact.getArtifactTimeout();
225                 artifact.getArtifactType();
226                 artifact.getArtifactURL();
227                 artifact.getArtifactUUID();
228                 artifact.getArtifactVersion();
229                 artifact.getGeneratedFromUUID();
230         }
231         
232         /**
233          * Test tosca csar.
234          *
235          * @param toscaCsar the tosca csar
236          */
237         private void testToscaCsar(ToscaCsar toscaCsar) {
238                 testToscaModel(toscaCsar.getParent());
239                 
240                 for (ToscaModel childModel : toscaCsar.getChildren()) {
241                         testToscaModel(childModel);
242                 }
243         }
244         
245         /**
246          * Test tosca model.
247          *
248          * @param toscaModel the tosca model
249          */
250         private void testToscaModel(ToscaModel toscaModel) {
251                 
252                 toscaModel.getDescription();
253                 toscaModel.getMetadata().getCategory();
254                 toscaModel.getMetadata().getDescription();
255                 toscaModel.getMetadata().getInvariantUUID();
256                 toscaModel.getMetadata().getName();
257                 toscaModel.getMetadata().getType();
258                 toscaModel.getMetadata().gettemplate_name();
259                 toscaModel.getMetadata().getUUID();
260                 toscaModel.getMetadata().getVersion();
261                 //toscaModel.getMetadata().isServiceEcompNaming();
262                 toscaModel.getMetadata().isServiceHoming();
263                 
264                 if (!toscaModel.gettopology_template().getInputs().isEmpty()) {
265                         final Input input = toscaModel.gettopology_template().getInputs().values().iterator().next();
266                         input.getDefault();
267                         input.getDescription();
268                         input.getType();
269                         input.toString();
270                 }
271                 
272                 if (!toscaModel.gettopology_template().getnode_templates().isEmpty()) {
273                         final NodeTemplate nodeTemplate = toscaModel.gettopology_template().getnode_templates().values().iterator().next();
274                         nodeTemplate.getMetadata();
275                         nodeTemplate.getProperties();
276                         nodeTemplate.getRequirements();
277                         nodeTemplate.getType();
278                 }
279                 
280                 if (!toscaModel.gettopology_template().getGroups().isEmpty()) {
281                         final Group group = toscaModel.gettopology_template().getGroups().values().iterator().next();
282                         group.getMembers();
283                         group.getMetadata();
284                         group.getType();
285                 }
286                 
287                 if (!toscaModel.getImports().isEmpty()) {
288                         for (Map<String, Map<String, String>> imports : toscaModel.getImports()) {
289                                 imports.values().iterator().next().get("file");
290                         }
291                 }
292                 
293                 toscaModel.gettopology_template().getsubstitution_mappings().getnode_type();
294                 
295                 if (!toscaModel.gettopology_template().getsubstitution_mappings().getCapabilities().isEmpty()) {
296                         toscaModel.gettopology_template().getsubstitution_mappings().getCapabilities();
297                 }
298                 
299                 toscaModel.gettosca_definitions_version();
300         }
301         
302         /**
303          * Test try catch asdc catalog exception.
304          */
305         @Test
306         public void testTryCatchAsdcCatalogException() {
307                 try {
308                         throw new AsdcCatalogException("testing");
309                 } catch (AsdcCatalogException e) {
310                         Assert.assertEquals("testing", e.getMessage());
311                 }
312                 
313                 final Exception cause = new Exception();
314                 
315                 try {
316                         throw new AsdcCatalogException("testing", cause);
317                 } catch (AsdcCatalogException e) {
318                         Assert.assertEquals("testing", e.getMessage());
319                         Assert.assertEquals(cause, e.getCause());
320                 }
321         }
322 }