2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.asdc;
23 import java.util.Collection;
24 import java.util.Collections;
26 import java.util.UUID;
28 import javax.ws.rs.NotFoundException;
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;
46 * The Class BaseClientTest.
48 public class BaseClientTest {
52 public ErrorCollector collector = new ErrorCollector();
57 * @param client the client
58 * @throws AsdcCatalogException the asdc catalog exception
60 protected void runResourceTests(AsdcClient client) throws AsdcCatalogException {
61 final Collection<Resource> resources = client.getResources();
63 collector.checkThat("getResources() returned nothing", resources.isEmpty(), IsEqual.equalTo(false));
65 final Resource resource = resources.iterator().next();
67 testResource(resource);
69 final Resource thisResource = client.getResource(UUID.fromString(resource.getUuid()));
71 collector.checkThat(thisResource, IsEqual.equalTo(resource));
73 for (Resource aResource : resources) {
74 if (aResource.getArtifacts() != null && !aResource.getArtifacts().isEmpty()) {
76 final Artifact artifact = aResource.getArtifacts().iterator().next();
78 testArtifact(artifact);
80 final UUID resourceUuid = UUID.fromString(aResource.getUuid());
81 final UUID artifactUuid = UUID.fromString(artifact.getArtifactUUID());
82 final Artifact thisArtifact = client.getResourceArtifact(resourceUuid, artifactUuid);
84 collector.checkThat(artifact, IsEqual.equalTo(thisArtifact));
89 final Collection<Resource> badResources = client.getResources(Collections.singletonMap("category", new String[] {"Bad Resources"}));
91 for (Resource badResource : badResources) {
92 collector.checkThat(badResource.getCategory(), IsEqual.equalTo("Bad Resources"));
94 } catch (NotFoundException e) {
95 //No resources of this category were found
99 final Collection<Resource> reallyBadResources = client.getResources(Collections.singletonMap("subCategory", new String[] {"Really Bad Resources"}));
101 for (Resource reallyBadResource : reallyBadResources) {
102 collector.checkThat(reallyBadResource.getSubCategory(), IsEqual.equalTo("Really Bad Resources"));
104 } catch (NotFoundException e) {
105 //No resources of this subcategory were found
108 /*final ToscaCsar toscaCsar = client.getResourceToscaModel(UUID.fromString(resource.getUuid()));
110 testToscaCsar(toscaCsar);*/
116 * @param client the client
117 * @throws AsdcCatalogException the asdc catalog exception
119 protected void runServiceTests(AsdcClient client) throws AsdcCatalogException {
120 final Collection<Service> services = client.getServices();
122 collector.checkThat("getServices() returned nothing", services.isEmpty(), IsEqual.equalTo(false));
124 final Service service = services.iterator().next();
126 testService(service);
128 final Service thisService = client.getService(UUID.fromString(service.getUuid()));
130 collector.checkThat(thisService, IsEqual.equalTo(service));
132 for (Service aService : services) {
133 if (aService.getArtifacts() != null && ! aService.getArtifacts().isEmpty()) {
134 final Artifact artifact = aService.getArtifacts().iterator().next();
136 testArtifact(artifact);
138 final UUID serviceUuid = UUID.fromString(aService.getUuid());
139 final UUID artifactUuid = UUID.fromString(artifact.getArtifactUUID());
140 final Artifact thisArtifact = client.getServiceArtifact(serviceUuid, artifactUuid);
142 collector.checkThat(artifact, IsEqual.equalTo(thisArtifact));
148 final Collection<Service> distributedServices = client.getServices(Collections.singletonMap("distributionStatus", new String[] {"DISTRIBUTED"}));
150 for (Service distributedService : distributedServices) {
151 collector.checkThat(distributedService.getDistributionStatus(), IsEqual.equalTo(DistributionStatus.DISTRIBUTED));
153 } catch (NotFoundException e) {
154 //No services of this distributionStatus were found
158 final Collection<Service> badServices = client.getServices(Collections.singletonMap("category", new String[] {"Bad Services"}));
160 for (Service badService : badServices) {
161 collector.checkThat(badService.getCategory(), IsEqual.equalTo("Bad Services"));
163 } catch (NotFoundException e) {
164 //No services of this category were found
167 /*final ToscaCsar toscaCsar = client.getServiceToscaModel(UUID.fromString(service.getUuid()));
169 testToscaCsar(toscaCsar);*/
175 * @param service the service
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();
186 service.getResources();
187 service.getToscaModelURL();
189 service.getVersion();
195 * @param resource the resource
197 private void testResource(Resource resource) {
198 resource.getArtifacts();
199 resource.getCategory();
200 resource.getInvariantUUID();
201 resource.getLastUpdaterUserId();
202 resource.getLastUpdaterFullName();
203 resource.getLifecycleState();
205 resource.getResources();
206 resource.getResourceType();
207 resource.getSubCategory();
208 resource.getToscaModel();
209 resource.getToscaModelURL();
210 resource.getToscaResourceName();
212 resource.getVersion();
218 * @param artifact the artifact
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();
235 * @param toscaCsar the tosca csar
237 private void testToscaCsar(ToscaCsar toscaCsar) {
238 testToscaModel(toscaCsar.getParent());
240 for (ToscaModel childModel : toscaCsar.getChildren()) {
241 testToscaModel(childModel);
248 * @param toscaModel the tosca model
250 private void testToscaModel(ToscaModel toscaModel) {
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();
264 if (!toscaModel.gettopology_template().getInputs().isEmpty()) {
265 final Input input = toscaModel.gettopology_template().getInputs().values().iterator().next();
267 input.getDescription();
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();
280 if (!toscaModel.gettopology_template().getGroups().isEmpty()) {
281 final Group group = toscaModel.gettopology_template().getGroups().values().iterator().next();
287 if (!toscaModel.getImports().isEmpty()) {
288 for (Map<String, Map<String, String>> imports : toscaModel.getImports()) {
289 imports.values().iterator().next().get("file");
293 toscaModel.gettopology_template().getsubstitution_mappings().getnode_type();
295 if (!toscaModel.gettopology_template().getsubstitution_mappings().getCapabilities().isEmpty()) {
296 toscaModel.gettopology_template().getsubstitution_mappings().getCapabilities();
299 toscaModel.gettosca_definitions_version();
303 * Test try catch asdc catalog exception.
306 public void testTryCatchAsdcCatalogException() {
308 throw new AsdcCatalogException("testing");
309 } catch (AsdcCatalogException e) {
310 Assert.assertEquals("testing", e.getMessage());
313 final Exception cause = new Exception();
316 throw new AsdcCatalogException("testing", cause);
317 } catch (AsdcCatalogException e) {
318 Assert.assertEquals("testing", e.getMessage());
319 Assert.assertEquals(cause, e.getCause());