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