4e27c30f8a05c9d4741a5a515b8059923e403ae0
[sdc.git] /
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 static org.junit.Assert.assertTrue;
24
25 import java.lang.reflect.Method;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.stream.Collectors;
30
31 import org.junit.Test;
32 import org.mockito.InjectMocks;
33 import org.openecomp.sdc.be.components.BaseConfDependentTest;
34 import org.openecomp.sdc.be.components.distribution.engine.ArtifactInfoImpl;
35 import org.openecomp.sdc.be.components.distribution.engine.ServiceDistributionArtifactsBuilder;
36 import org.openecomp.sdc.be.model.ArtifactDefinition;
37 import org.openecomp.sdc.be.model.Service;
38 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
39 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
40
41 public class ServiceDistributionArtifactsBuilderTest extends BaseConfDependentTest {
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 }