e1d6a9ded8c43a9c32517468e5beeb2d2d111824
[sdc.git] /
1 package org.openecomp.sdc.healing.healers;
2
3 import org.junit.AfterClass;
4 import org.junit.Assert;
5 import org.junit.Before;
6 import org.junit.BeforeClass;
7 import org.junit.Test;
8 import org.mockito.InjectMocks;
9 import org.mockito.Mock;
10 import org.mockito.Mockito;
11 import org.mockito.MockitoAnnotations;
12 import org.openecomp.core.model.dao.ServiceModelDao;
13 import org.openecomp.core.model.types.ServiceElement;
14 import org.openecomp.sdc.common.togglz.ToggleableFeature;
15 import org.openecomp.sdc.common.utils.SdcCommon;
16 import org.openecomp.sdc.healing.healers.util.TestUtil;
17 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
18 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
19 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
20 import org.openecomp.sdc.tosca.services.ToscaAnalyzerService;
21 import org.openecomp.sdc.versioning.dao.types.Version;
22 import org.togglz.testing.TestFeatureManager;
23 import org.togglz.testing.TestFeatureManagerProvider;
24
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Optional;
28
29 import static org.mockito.Matchers.any;
30 import static org.mockito.Matchers.anyObject;
31 import static org.mockito.Matchers.eq;
32
33 public class ForwarderCapabilityHealerTest {
34   private static final String IN_SUFFIX = "/in";
35   private static final String OUT_SUFFIX = "/out";
36   private static final String BASE_DIRECTORY = "/mock/healers/forwarder";
37   private static final String ENTRY_DEFINITION_SERVICE_TEMPLATE = "MainServiceTemplate.yaml";
38   private static TestFeatureManager manager;
39
40   private Map<String,Object> params = new HashMap<>();
41
42   @Mock
43   private ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDao;
44   @Mock
45   private ToscaAnalyzerService toscaAnalyzerService;
46   @InjectMocks
47   private ForwarderCapabilityHealer forwarderCapabilityHealer;
48
49   @BeforeClass
50   public static void enableForwarderFeature(){
51     manager = new TestFeatureManager(ToggleableFeature.class);
52     if (!ToggleableFeature.FORWARDER_CAPABILITY.isActive()) {
53       manager.enable(ToggleableFeature.FORWARDER_CAPABILITY);
54     }
55   }
56
57   @AfterClass
58   public static void disableForwarderFeature() {
59     manager.disable(ToggleableFeature.FORWARDER_CAPABILITY);
60     manager = null;
61     TestFeatureManagerProvider.setFeatureManager(null);
62   }
63
64   @Before
65   public void setUp() throws Exception {
66     MockitoAnnotations.initMocks(ForwarderCapabilityHealerTest.this);
67     params.put(SdcCommon.VSP_ID,"1");
68     params.put(SdcCommon.VERSION, new Version(1,1));
69   }
70
71
72   @Test
73   public void testHealingSubstitutionMappingsNeutronPort() throws Exception {
74     testForwarderHealer(
75         "/testSubMappingNeutronPort", "org.openecomp.resource.cp.nodes.heat.network.neutron.Port", true);
76   }
77
78   @Test
79   public void testHealingSubstitutionMappingsContrailPort() throws Exception {
80     testForwarderHealer(
81         "/testSubMappingContrailPort", "org.openecomp.resource.cp.nodes.heat.network.contrail.Port", true);
82   }
83
84   @Test
85   public void testHealingSubstitutionMappingsExtNeutronPort() throws Exception {
86     testForwarderHealer(
87         "/testSubMappingExtNeutronPort", "org.openecomp.resource.cp.v2.extNeutronCP", true);
88   }
89
90   @Test
91   public void testHealingSubstitutionMappingsExtContrailPort() throws Exception {
92     testForwarderHealer(
93         "/testSubMappingExtContrailPort", "org.openecomp.resource.cp.v2.extContrailCP", true);
94   }
95
96   @Test
97   public void testHealingGlobalServiceTemplates () throws Exception {
98     testForwarderHealer("/testGlobalServiceTemplates", null, false);
99   }
100
101   @Test
102   public void testHealingNoPorts() throws Exception {
103     testForwarderHealer("/testNoPorts", null, false);
104   }
105
106   private void testForwarderHealer(String testDirectory,
107                                    String portType,
108                                    boolean needToTestSubMapping) throws Exception {
109
110     ToscaServiceModel toscaServiceModel = TestUtil.loadToscaServiceModel(
111         BASE_DIRECTORY + testDirectory + IN_SUFFIX, null, ENTRY_DEFINITION_SERVICE_TEMPLATE);
112
113     Mockito.doReturn(toscaServiceModel)
114         .when(serviceModelDao).getServiceModel(any(), any());
115
116     if(needToTestSubMapping) {
117       Mockito.doReturn(true)
118           .when(toscaAnalyzerService).isTypeOf(
119           eq(getMockPortNodeTemplate(portType)),
120           eq(ToscaNodeType.NATIVE_NETWORK_PORT),
121           anyObject(), anyObject());
122     }
123
124     validateServiceModelAfterHealing(testDirectory);
125   }
126
127   private void validateServiceModelAfterHealing(String testDirectory) throws Exception {
128     Optional<ToscaServiceModel> serviceModelObject =
129         (Optional<ToscaServiceModel>) forwarderCapabilityHealer.heal(params);
130
131     Assert.assertTrue(serviceModelObject.isPresent());
132     TestUtil
133         .compareToscaServiceModels(
134             BASE_DIRECTORY + testDirectory + OUT_SUFFIX, serviceModelObject.get());
135   }
136
137   private NodeTemplate getMockPortNodeTemplate(String portType) {
138     NodeTemplate nodeTemplate = new NodeTemplate();
139     nodeTemplate.setType(portType);
140
141     Map<String, Object> properties = new HashMap<>();
142     properties.put("exCP_naming", "port_pd01_port_exCP_naming");
143     nodeTemplate.setProperties(properties);
144
145     return nodeTemplate;
146   }
147
148 }