Create REST endpoint to retrieve models
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / distribution / ServiceDistributionArtifactsBuilderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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.openecomp.sdc.be.distribution;
22
23 import org.junit.Test;
24 import org.mockito.InjectMocks;
25 import org.mockito.Mockito;
26 import org.openecomp.sdc.be.components.BeConfDependentTest;
27 import org.openecomp.sdc.be.components.distribution.engine.ArtifactInfoImpl;
28 import org.openecomp.sdc.be.components.distribution.engine.ServiceDistributionArtifactsBuilder;
29 import org.openecomp.sdc.be.model.ArtifactDefinition;
30 import org.openecomp.sdc.be.model.Service;
31 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
32 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
33 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
34
35 import java.lang.reflect.Method;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.stream.Collectors;
40
41 import static org.junit.Assert.assertEquals;
42 import static org.junit.Assert.assertNotNull;
43 import static org.junit.Assert.assertTrue;
44
45 public class ServiceDistributionArtifactsBuilderTest extends BeConfDependentTest {
46
47     private final ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class);
48
49     @InjectMocks
50     ServiceDistributionArtifactsBuilder serviceDistributionArtifactsBuilder = new ServiceDistributionArtifactsBuilder(toscaOperationFacade);
51
52     @SuppressWarnings({ "unchecked", "rawtypes" })
53     @Test
54     public void testConvertServiceArtifactsToArtifactInfo() {
55
56         Service service = new Service();
57         String serviceName = "myService";
58         String serviceVersion = "1.0";
59         String serviceId = "serviceId";
60         service.setName(serviceName);
61         service.setVersion(serviceVersion);
62         service.setUniqueId(serviceId);
63
64
65         String artifactName = "service-Myservice-template.yml";
66         String artifactLabel = "assettoscatemplate";
67         String esArtifactId = "123123dfgdfgd0";
68         byte[] payload = "some payload".getBytes();
69
70         ArtifactDefinition toscaTemplateArtifact = new ArtifactDefinition();
71         toscaTemplateArtifact.setArtifactName(artifactName);
72         toscaTemplateArtifact.setArtifactType(ArtifactTypeEnum.TOSCA_TEMPLATE.getType());
73         toscaTemplateArtifact.setArtifactLabel(artifactLabel);
74         toscaTemplateArtifact.setEsId(esArtifactId);
75         toscaTemplateArtifact.setUniqueId(esArtifactId);
76         toscaTemplateArtifact.setPayload(payload);
77
78         Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
79         toscaArtifacts.put(artifactLabel, toscaTemplateArtifact);
80         service.setToscaArtifacts(toscaArtifacts);
81
82         ArtifactDefinition deploymentArtifact = new ArtifactDefinition();
83         deploymentArtifact.setArtifactName("deployment.yaml");
84         deploymentArtifact.setArtifactGroupType(ArtifactGroupTypeEnum.DEPLOYMENT);
85         deploymentArtifact.setArtifactType(ArtifactTypeEnum.OTHER.getType());
86         deploymentArtifact.setArtifactLabel("deployment");
87         deploymentArtifact.setEsId("deployment007");
88         deploymentArtifact.setUniqueId("deployment007");
89         deploymentArtifact.setPayload(payload);
90         Map<String, ArtifactDefinition> deploymentArtifacts = new HashMap<>();
91         deploymentArtifacts.put("deployment", deploymentArtifact);
92         service.setDeploymentArtifacts(deploymentArtifacts);
93
94         Class<ServiceDistributionArtifactsBuilder> targetClass = ServiceDistributionArtifactsBuilder.class;
95         String methodName = "convertServiceArtifactsToArtifactInfo";
96         Object[] argObjects = {service};
97         Class[] argClasses = {Service.class};
98         try {
99             Method method = targetClass.getDeclaredMethod(methodName, argClasses);
100             method.setAccessible(true);
101             List<ArtifactInfoImpl> convertServiceArtifactsToArtifactInfoRes =
102                     (List<ArtifactInfoImpl>) method.invoke(serviceDistributionArtifactsBuilder, argObjects);
103             assertNotNull(convertServiceArtifactsToArtifactInfoRes);
104             assertEquals(2, convertServiceArtifactsToArtifactInfoRes.size());
105             List<String> artifactsNames = convertServiceArtifactsToArtifactInfoRes.stream().map(ArtifactInfoImpl::getArtifactName).collect(Collectors.toList());
106             assertTrue(artifactsNames.contains(artifactName) && artifactsNames.contains("deployment.yaml"));
107         }
108         catch (Exception e) {
109             e.printStackTrace();
110         }
111     }
112 }