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