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