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