re base code
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / ComponentBusinessLogicTest.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.components;
22
23 import fj.data.Either;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.openecomp.sdc.be.DummyConfigurationManager;
31 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
32 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
33 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
34 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
35 import org.openecomp.sdc.be.config.ConfigurationManager;
36 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
37 import org.openecomp.sdc.be.model.ArtifactDefinition;
38 import org.openecomp.sdc.be.model.ComponentInstance;
39 import org.openecomp.sdc.be.model.Resource;
40 import org.openecomp.sdc.be.model.User;
41 import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;
42 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
43 import org.openecomp.sdc.exception.ResponseFormat;
44
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Map;
48
49 import static org.assertj.core.api.Assertions.assertThat;
50 import static org.mockito.Mockito.when;
51
52 @RunWith(MockitoJUnitRunner.class)
53 public class ComponentBusinessLogicTest {
54
55     private static final User USER = new User();
56     private static final String ARTIFACT_LABEL = "toscaArtifact1";
57     private static final String ARTIFACT_LABEL2 = "toscaArtifact2";
58
59     @InjectMocks
60     private ComponentBusinessLogic testInstance = new ComponentBusinessLogic() {
61         @Override
62         public Either<List<String>, ResponseFormat> deleteMarkedComponents() {
63             return null;
64         }
65
66         @Override
67         public ComponentInstanceBusinessLogic getComponentInstanceBL() {
68             return null;
69         }
70
71         @Override
72         public Either<List<ComponentInstance>, ResponseFormat> getComponentInstancesFilteredByPropertiesAndInputs(String componentId, String userId) {
73             return null;
74         }
75
76         @Override
77         public Either<UiComponentDataTransfer, ResponseFormat> getUiComponentDataTransferByComponentId(String componentId, List<String> dataParamsToReturn) {
78             return null;
79         }
80     };
81
82     @Mock
83     private ArtifactsBusinessLogic artifactsBusinessLogic;
84
85     @BeforeClass
86     public static void setUp() throws Exception {
87         new DummyConfigurationManager();
88     }
89
90     @SuppressWarnings("unchecked")
91     @Test
92     public void setToscaArtifactsPlaceHolders_normalizeArtifactName() throws Exception {
93         Resource resource = new ResourceBuilder().setUniqueId("uid")
94                 .setComponentType(ComponentTypeEnum.RESOURCE)
95                 .setSystemName("myResource")
96                 .build();
97         Map<String, Object> artifactsFromConfig = new HashMap<>();
98         artifactsFromConfig.put(ARTIFACT_LABEL, buildArtifactMap("artifact:not normalized.yml"));
99         artifactsFromConfig.put(ARTIFACT_LABEL2, buildArtifactMap("alreadyNormalized.csar"));
100         when(ConfigurationManager.getConfigurationManager().getConfiguration().getToscaArtifacts()).thenReturn(artifactsFromConfig);
101         when(artifactsBusinessLogic.createArtifactPlaceHolderInfo(resource.getUniqueId(), ARTIFACT_LABEL, (Map<String, Object>) artifactsFromConfig.get(ARTIFACT_LABEL), USER, ArtifactGroupTypeEnum.TOSCA))
102                 .thenReturn(buildArtifactDef(ARTIFACT_LABEL));
103         when(artifactsBusinessLogic.createArtifactPlaceHolderInfo(resource.getUniqueId(), ARTIFACT_LABEL2, (Map<String, Object>) artifactsFromConfig.get(ARTIFACT_LABEL2), USER, ArtifactGroupTypeEnum.TOSCA))
104                 .thenReturn(buildArtifactDef(ARTIFACT_LABEL2));
105         testInstance.setToscaArtifactsPlaceHolders(resource, USER);
106
107         Map<String, ArtifactDefinition> toscaArtifacts = resource.getToscaArtifacts();
108         assertThat(toscaArtifacts).hasSize(2);
109         ArtifactDefinition artifactDefinition = toscaArtifacts.get(ARTIFACT_LABEL);
110         assertThat(artifactDefinition.getArtifactName()).isEqualTo("resource-myResourceartifactnot-normalized.yml");
111         ArtifactDefinition artifactDefinition2 = toscaArtifacts.get(ARTIFACT_LABEL2);
112         assertThat(artifactDefinition2.getArtifactName()).isEqualTo("resource-myResourcealreadyNormalized.csar");
113     }
114
115     private Map<String, Object> buildArtifactMap(String artifactName) {
116         Map<String, Object> artifact = new HashMap<>();
117         artifact.put("artifactName", artifactName);
118         return artifact;
119     }
120
121     private ArtifactDefinition buildArtifactDef(String artifactLabel) {
122         ArtifactDefinition artifactDefinition = new ArtifactDefinition();
123         artifactDefinition.setArtifactLabel(artifactLabel);
124         return artifactDefinition;
125     }
126 }