9cd18bc9a2e3aa3d9aa2decf5c238a10f3b46b73
[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.components.impl;
22
23 import fj.data.Either;
24 import java.util.Optional;
25 import java.util.Set;
26 import java.util.stream.Collectors;
27 import java.util.stream.Stream;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.mockito.InjectMocks;
32 import org.mockito.Mockito;
33 import org.mockito.MockitoAnnotations;
34 import org.mockito.invocation.InvocationOnMock;
35 import org.mockito.stubbing.Answer;
36 import org.openecomp.sdc.be.impl.ComponentsUtils;
37 import org.openecomp.sdc.be.model.InterfaceDefinition;
38 import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation;
39 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
40 import org.openecomp.sdc.exception.ResponseFormat;
41
42 import java.io.IOException;
43 import java.nio.file.Files;
44 import java.nio.file.Path;
45 import java.nio.file.Paths;
46 import java.util.List;
47
48 import static org.hamcrest.MatcherAssert.assertThat;
49 import static org.hamcrest.Matchers.containsInAnyOrder;
50 import static org.hamcrest.Matchers.empty;
51 import static org.hamcrest.Matchers.hasSize;
52 import static org.hamcrest.Matchers.not;
53 import static org.hamcrest.core.Is.is;
54 import static org.junit.Assert.assertTrue;
55 import static org.mockito.Mockito.when;
56
57 public class InterfaceLifecycleTypeImportManagerTest {
58
59     @InjectMocks
60     private InterfaceLifecycleTypeImportManager importManager = new InterfaceLifecycleTypeImportManager();
61     public static final CommonImportManager commonImportManager = Mockito.mock(CommonImportManager.class);
62     public static final IInterfaceLifecycleOperation interfaceLifecycleOperation = Mockito.mock(IInterfaceLifecycleOperation.class);
63     public static final ComponentsUtils componentsUtils = Mockito.mock(ComponentsUtils.class);
64
65     @BeforeClass
66     public static void beforeClass() throws IOException {
67         when(interfaceLifecycleOperation.createInterfaceType(Mockito.any(InterfaceDefinition.class))).thenAnswer(new Answer<Either<InterfaceDefinition, StorageOperationStatus>>() {
68             public Either<InterfaceDefinition, StorageOperationStatus> answer(InvocationOnMock invocation) {
69                 Object[] args = invocation.getArguments();
70                 return Either.left((InterfaceDefinition) args[0]);
71             }
72
73         });
74         when(commonImportManager.createElementTypesFromYml(Mockito.anyString(), Mockito.any())).thenCallRealMethod();
75         when(commonImportManager.createElementTypesFromToscaJsonMap(Mockito.any(), Mockito.any())).thenCallRealMethod();
76     }
77
78     @Before
79     public void initMocks() {
80         MockitoAnnotations.initMocks(this);
81     }
82
83     @Test
84     public void createLifecycleTypesTest() throws IOException {
85         final String ymlContent = getYmlContent();
86         final Either<List<InterfaceDefinition>, ResponseFormat> createCapabilityTypes =
87             importManager.createLifecycleTypes(ymlContent, "test");
88         assertTrue(createCapabilityTypes.isLeft());
89         final List<InterfaceDefinition> interfaceDefinitionList = createCapabilityTypes.left().value();
90         assertThat("Interface definitions should not be empty", interfaceDefinitionList, is(not(empty())));
91         final int expectedSize = 2;
92         assertThat(String.format("Interface definitions should have the size %s", expectedSize),
93             interfaceDefinitionList, hasSize(expectedSize));
94         final String standardInterfaceType = "tosca.interfaces.node.lifecycle.Standard";
95         final String nslcmInterfaceType = "tosca.interfaces.nfv.Nslcm";
96         final Optional<InterfaceDefinition> standardInterfaceOpt = interfaceDefinitionList.stream().filter(
97             interfaceDefinition -> standardInterfaceType.equals(interfaceDefinition.getType()))
98             .findFirst();
99         final Optional<InterfaceDefinition> nslcmInterfaceOpt = interfaceDefinitionList.stream().filter(
100             interfaceDefinition -> nslcmInterfaceType.equals(interfaceDefinition.getType()))
101             .findFirst();
102         assertThat("", standardInterfaceOpt.isPresent(), is(true));
103         assertThat("", nslcmInterfaceOpt.isPresent(), is(true));
104         final InterfaceDefinition standardInterface = standardInterfaceOpt.get();
105         final Set<String> expectedStandardInterfaceOperationSet = Stream
106             .of("create", "configure", "start", "stop", "delete").collect(Collectors.toSet());
107         assertThat(String.format("%s derived_from should be as expected", standardInterfaceType),
108             standardInterface.getDerivedFrom(), is("tosca.interfaces.Root"));
109         assertThat(String.format("%s operations should have the expected size", standardInterfaceType),
110             standardInterface.getOperationsMap().keySet(), hasSize(expectedStandardInterfaceOperationSet.size()));
111         assertThat(String.format("%s should contains the expected operations", standardInterfaceType),
112             standardInterface.getOperationsMap().keySet(),
113             containsInAnyOrder(expectedStandardInterfaceOperationSet.toArray()));
114
115         final InterfaceDefinition nslcmInterface = nslcmInterfaceOpt.get();
116         assertThat(String.format("%s derived_from should be as expected", nslcmInterfaceType),
117             nslcmInterface.getDerivedFrom(), is("tosca.interfaces.Root"));
118         assertThat(String.format("%s description should be as expected", nslcmInterfaceType),
119             nslcmInterface.getDescription(),
120             is("This interface encompasses a set of TOSCA "
121                 + "operations corresponding to NS LCM operations defined in ETSI GS NFV-IFA 013. as well as to preamble "
122                 + "and postamble procedures to the execution of the NS LCM operations."));
123         final Set<String> expectedNsclmInterfaceOperationSet = Stream
124             .of("instantiate_start", "instantiate", "instantiate_end", "terminate_start", "terminate",
125                 "terminate_end", "update_start", "update", "update_end", "scale_start", "scale", "scale_end",
126                 "heal_start", "heal", "heal_end").collect(Collectors.toSet());
127         assertThat(String.format("%s operations should have the expected size", nslcmInterfaceType),
128             nslcmInterface.getOperationsMap().keySet(),
129             hasSize(expectedNsclmInterfaceOperationSet.size()));
130         assertThat(String.format("%s should contains the expected operations", nslcmInterfaceType),
131             nslcmInterface.getOperationsMap().keySet(),
132             containsInAnyOrder(expectedNsclmInterfaceOperationSet.toArray()));
133     }
134
135     private String getYmlContent() throws IOException {
136         Path filePath = Paths.get("src/test/resources/types/interfaceLifecycleTypes.yml");
137         byte[] fileContent = Files.readAllBytes(filePath);
138         return new String(fileContent);
139     }
140 }