Add collaboration feature
[sdc.git] / openecomp-be / lib / openecomp-healing-lib / openecomp-sdc-healing-impl / src / test / java / org / openecomp / sdc / healing / healers / ComponentQuestionnaireHealerTest.java
1 package org.openecomp.sdc.healing.healers;
2
3 import com.google.gson.JsonObject;
4 import com.google.gson.JsonParser;
5 import org.junit.Assert;
6 import org.junit.Before;
7 import org.junit.Test;
8 import org.mockito.InjectMocks;
9 import org.mockito.Mock;
10 import org.mockito.MockitoAnnotations;
11 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
12 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComputeDao;
13 import org.openecomp.sdc.vendorsoftwareproduct.dao.ImageDao;
14 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
15 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComputeEntity;
16 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ImageEntity;
17 import org.openecomp.sdc.versioning.dao.types.Version;
18
19 import java.lang.reflect.InvocationTargetException;
20 import java.lang.reflect.Method;
21 import java.util.ArrayList;
22 import java.util.Collection;
23
24 import static org.mockito.Matchers.anyObject;
25 import static org.mockito.Mockito.doNothing;
26 import static org.mockito.Mockito.doReturn;
27
28 public class ComponentQuestionnaireHealerTest {
29   private static final String HANDLE_NUM_OF_VMS_METHOD = "handleNumOfVm";
30   private static final String GENERAL = "general";
31   private static final String IMAGE = "image";
32   private static final String FORMAT = "format";
33   private static final String CPU_OVER_SUBSCRIPTION_RATIO = "CpuOverSubscriptionRatio";
34   private static final String MEMORY_RAM = "MemoryRAM";
35   private static final String VM_SIZING = "vmSizing";
36   private static final String COMPUTE = "compute";
37   private static final String NUM_OF_VMS = "numOfVMs";
38   private static final String DISK = "disk";
39   private static final String BOOT_DISK_SIZE_PER_VM = "bootDiskSizePerVM";
40   private static final String EPHEMERAL_DISK_SIZE_PER_VM = "ephemeralDiskSizePerVM";
41   private static final Version VERSION = new Version(0, 1);
42   private static final String DUMMY_VSP_ID = "1495ef442f964cbfb00d82bd54292f89";
43   private static final String DUMMY_COMPONENT_ID = "2495ef442f964cbfb00d82bd54292f89";
44   private static final String DUMMY_COMPUTE_ID = "3495ef442f964cbfb00d82bd54292f89";
45   private static final String DUMMY_IMAGE_ID = "4495ef442f964cbfb00d82bd54292f89";
46   private static final String componentQuestionnaireData = "{\"compute\": {" +
47       "\"guestOS\": {\"bitSize\": 64},\"vmSizing\": {\"IOOperationsPerSec\": \"0\"}," +
48       "\"numOfVMs\": {\"CpuOverSubscriptionRatio\": \"1:1\",\"MemoryRAM\": \"2 GB\"}}," +
49       "\"general\": {\"image\": {\"providedBy\": \"AIC\",\"format\":\"qcow2\"," +
50       "\"bootDiskSizePerVM\": \"100\",\"ephemeralDiskSizePerVM\": \"200\"},\"hypervisor\": {" +
51       "\"hypervisor\": \"KVM\" } },\"highAvailabilityAndLoadBalancing\": {" +
52       "\"isComponentMandatory\": \"\",\"highAvailabilityMode\": \"\"},\"storage\": {" +
53       "\"backup\": {\"backupNIC\": \"\",\"backupType\": \"On Site\" }," +
54       "\"snapshotBackup\": {\"snapshotFrequency\": \"24\"}},\"network\": {\"networkCapacity\": {" +
55       "\"protocolWithHighestTrafficProfileAcrossAllNICs\": \"\"}}}";
56
57   private static final String componentQuestionnaireMissingDiskAttrData = "{\"compute\": {" +
58       "\"guestOS\": {\"bitSize\": 64},\"vmSizing\": {\"IOOperationsPerSec\": \"0\"},\"numOfVMs\"" +
59       ": {\"CpuOverSubscriptionRatio\": \"1:1\",\"MemoryRAM\": \"2 GB\"}},\"general\": " +
60       "{\"image\": {\"providedBy\": \"AIC\",\"format\":\"qcow2\"}," +
61       "\"hypervisor\": {\"hypervisor\": \"KVM\" } },\"highAvailabilityAndLoadBalancing\": {" +
62       "\"isComponentMandatory\": \"\",\"highAvailabilityMode\": \"\"},\"storage\": {" +
63       "\"backup\": {\"backupNIC\": \"\",\"backupType\": \"On Site\" }," +
64       "\"snapshotBackup\": {\"snapshotFrequency\": \"24\"}},\"network\": {\"networkCapacity\": {" +
65       "\"protocolWithHighestTrafficProfileAcrossAllNICs\": \"\"}}}";
66
67   private static final String componentQuestionnaireWithoutVMSizingData = "{\"compute\": {" +
68       "\"guestOS\": {\"bitSize\": 64},\"numOfVMs\": {\"CpuOverSubscriptionRatio\": \"1:1\"," +
69       "\"maximum\": \"400\"," +
70       "\"MemoryRAM\": \"2 GB\"}},\"general\": {\"image\": {\"providedBy\": \"AIC\",\"format\"" +
71       ":\"qcow2\",\"bootDiskSizePerVM\": \"100\",\"ephemeralDiskSizePerVM\": \"200\"}," +
72       "\"hypervisor\": {\"hypervisor\": \"KVM\" } },\"highAvailabilityAndLoadBalancing\": {" +
73       "\"isComponentMandatory\": \"\",\"highAvailabilityMode\": \"\"},\"storage\": {" +
74       "\"backup\": {\"backupNIC\": \"\",\"backupType\": \"On Site\" }," +
75       "\"snapshotBackup\": {\"snapshotFrequency\": \"24\"}},\"network\": {\"networkCapacity\": {" +
76       "\"protocolWithHighestTrafficProfileAcrossAllNICs\": \"\"}}}";
77
78   private static final String componentQuestionnaireWithoutNumOfVMData = "{\"compute\": " +
79       "{\"guestOS\": {\"bitSize\": 64}," +
80       "\"vmSizing\": {\"IOOperationsPerSec\": \"0\"}}," +
81       "\"general\": {\"image\": {\"providedBy\": \"AIC\",\"format\":\"qcow2\"," +
82       "\"bootDiskSizePerVM\": \"100\",\"ephemeralDiskSizePerVM\": \"200\"}," +
83       "\"hypervisor\": {\"hypervisor\": \"KVM\" } },\"highAvailabilityAndLoadBalancing\": {" +
84       "\"isComponentMandatory\": \"\",\"highAvailabilityMode\": \"\"},\"storage\": {" +
85       "\"backup\": {\"backupNIC\": \"\",\"backupType\": \"On Site\" }," +
86       "\"snapshotBackup\": {\"snapshotFrequency\": \"24\"}},\"network\": {\"networkCapacity\": {" +
87       "\"protocolWithHighestTrafficProfileAcrossAllNICs\": \"\"}}}";
88
89   private static final String componentQuestionnaireWithMemoryRamData = "{\"compute\": " +
90       "{\"guestOS\": {\"bitSize\": 64}," +
91       "\"vmSizing\": {\"IOOperationsPerSec\": \"0\"},\"numOfVMs\": {\"MemoryRAM\": \"2 GB\"}}," +
92       "\"general\": {\"image\": {\"providedBy\": \"AIC\",\"format\":\"qcow2\"," +
93       "\"bootDiskSizePerVM\": \"100\",\"ephemeralDiskSizePerVM\": \"200\"}," +
94       "\"hypervisor\": {\"hypervisor\": \"KVM\" } },\"highAvailabilityAndLoadBalancing\": {" +
95       "\"isComponentMandatory\": \"\",\"highAvailabilityMode\": \"\"},\"storage\": {" +
96       "\"backup\": {\"backupNIC\": \"\",\"backupType\": \"On Site\" }," +
97       "\"snapshotBackup\": {\"snapshotFrequency\": \"24\"}},\"network\": {\"networkCapacity\": {" +
98       "\"protocolWithHighestTrafficProfileAcrossAllNICs\": \"\"}}}";
99
100   private static final String componentQuestionnaireWithCPURatioData = "{\"compute\": " +
101       "{\"guestOS\": {\"bitSize\": 64},\"vmSizing\": {\"IOOperationsPerSec\": " +
102       "\"0\"},\"numOfVMs\": {\"CpuOverSubscriptionRatio\": " +
103       "\"1:1\"}},\"general\": {\"image\": {\"providedBy\": \"AIC\",\"format\":\"qcow2\"," +
104       "\"bootDiskSizePerVM\": \"100\",\"ephemeralDiskSizePerVM\": \"200\"}," +
105       "\"hypervisor\": {\"hypervisor\": \"KVM\" } },\"highAvailabilityAndLoadBalancing\": {" +
106       "\"isComponentMandatory\": \"\",\"highAvailabilityMode\": \"\"},\"storage\": {" +
107       "\"backup\": {\"backupNIC\": \"\",\"backupType\": \"On Site\" }," +
108       "\"snapshotBackup\": {\"snapshotFrequency\": \"24\"}},\"network\": {\"networkCapacity\": {" +
109       "\"protocolWithHighestTrafficProfileAcrossAllNICs\": \"\"}}}";
110
111   private static final JsonParser jsonParser = new JsonParser();
112   private ComponentEntity componentEntity;
113
114   @Mock
115   private ImageDao imageDao;
116
117   @Mock
118   private ComputeDao computeDao;
119
120   @Mock
121   private ComponentDao componentDao;
122
123   @InjectMocks
124   private ComponentQuestionnaireHealer componentQuestionnaireHealer;
125
126   @Before
127   public void init() throws Exception {
128     MockitoAnnotations.initMocks(ComponentQuestionnaireHealerTest.this);
129   }
130
131   @Test
132   public void healQuestionnaireNullTest() throws Exception {
133     prepareHealingData();
134     componentEntity.setQuestionnaireData(null);
135     Object returnObject = componentQuestionnaireHealer.heal(DUMMY_VSP_ID, VERSION);
136     Assert.assertTrue(returnObject instanceof Collection);
137     Collection<ComponentEntity> componentEntities = (Collection<ComponentEntity>) returnObject;
138     componentEntities.forEach(componentEntity -> {
139       Assert.assertNull(componentEntity.getQuestionnaireData());
140     });
141   }
142
143   @Test
144   public void healAllCasesTest() throws Exception {
145     prepareHealingData();
146
147     Object returnObject = componentQuestionnaireHealer.heal(DUMMY_VSP_ID, VERSION);
148     Assert.assertTrue(returnObject instanceof Collection);
149     Collection<ComponentEntity> componentEntities = (Collection<ComponentEntity>) returnObject;
150     componentEntities.forEach(componentEntity -> {
151       JsonObject json = (JsonObject) jsonParser.parse(componentEntity.getQuestionnaireData());
152       Assert.assertNotNull(json.getAsJsonObject(GENERAL).getAsJsonObject(DISK));
153       Assert.assertNotNull(json.getAsJsonObject(GENERAL).getAsJsonObject(DISK)
154           .getAsJsonPrimitive(BOOT_DISK_SIZE_PER_VM));
155       Assert.assertNotNull(json.getAsJsonObject(GENERAL).getAsJsonObject(DISK)
156           .getAsJsonPrimitive(EPHEMERAL_DISK_SIZE_PER_VM));
157       Assert.assertNotNull(json.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS));
158       Assert.assertNull(json.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS)
159           .getAsJsonPrimitive(MEMORY_RAM));
160       Assert.assertNull(json.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS)
161           .getAsJsonPrimitive(CPU_OVER_SUBSCRIPTION_RATIO));
162       Assert.assertNull(json.getAsJsonObject(GENERAL).getAsJsonObject(IMAGE)
163           .getAsJsonPrimitive(FORMAT));
164       Assert.assertNull(json.getAsJsonObject(COMPUTE).getAsJsonObject(VM_SIZING));
165     });
166   }
167
168   @Test
169   public void healDiskAttrMissingTest() throws Exception {
170     prepareHealingData();
171     componentEntity.setQuestionnaireData(componentQuestionnaireMissingDiskAttrData);
172     Object returnObject = componentQuestionnaireHealer.heal(DUMMY_VSP_ID, VERSION);
173     Assert.assertTrue(returnObject instanceof Collection);
174     Collection<ComponentEntity> componentEntities = (Collection<ComponentEntity>) returnObject;
175     componentEntities.forEach(componentEntity -> {
176       JsonObject json = (JsonObject) jsonParser.parse(componentEntity.getQuestionnaireData());
177       Assert.assertNull(json.getAsJsonObject(COMPUTE).getAsJsonObject(VM_SIZING));
178     });
179   }
180
181   @Test
182   public void handleVMSizingWithVMSizingTest()
183       throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
184     JsonObject jsonObject = (JsonObject) jsonParser.parse(componentQuestionnaireWithoutNumOfVMData);
185     Method method = ComponentQuestionnaireHealer.class.getDeclaredMethod("handleVmSizing",
186         JsonObject.class);
187     method.setAccessible(true);
188     method.invoke(componentQuestionnaireHealer, jsonObject.getAsJsonObject(COMPUTE));
189
190     Assert.assertNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(VM_SIZING));
191   }
192
193   @Test
194   public void handleNumOfVMWithoutVMSizingTest()
195       throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
196     JsonObject jsonObject = (JsonObject) jsonParser
197         .parse(componentQuestionnaireWithoutVMSizingData);
198     provideAccessToPrivateMethod(HANDLE_NUM_OF_VMS_METHOD, jsonObject);
199
200     Assert.assertNotNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS));
201     Assert.assertNotNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS)
202         .getAsJsonPrimitive("maximum"));
203   }
204
205   @Test
206   public void handleVMSizingWithOnlyMemoryRAMTest()
207       throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
208     JsonObject jsonObject = (JsonObject) jsonParser.parse(componentQuestionnaireWithMemoryRamData);
209     provideAccessToPrivateMethod(HANDLE_NUM_OF_VMS_METHOD, jsonObject);
210
211     Assert.assertNotNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS));
212     Assert.assertNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS)
213         .getAsJsonPrimitive(MEMORY_RAM));
214   }
215
216   @Test
217   public void handleVMSizingWithOnlyCpuRatioTest()
218       throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
219     JsonObject jsonObject = (JsonObject) jsonParser.parse(componentQuestionnaireWithCPURatioData);
220     provideAccessToPrivateMethod(HANDLE_NUM_OF_VMS_METHOD, jsonObject);
221
222     Assert.assertNotNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS));
223     Assert.assertNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS)
224         .getAsJsonPrimitive(CPU_OVER_SUBSCRIPTION_RATIO));
225   }
226
227   private void provideAccessToPrivateMethod(String methodName, JsonObject jsonObject)
228       throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
229     Method method = ComponentQuestionnaireHealer.class
230         .getDeclaredMethod(methodName, JsonObject.class, JsonObject.class);
231     method.setAccessible(true);
232
233     method.invoke(componentQuestionnaireHealer, jsonObject.getAsJsonObject(COMPUTE), null);
234   }
235
236   private void prepareHealingData() {
237     componentEntity = new ComponentEntity(DUMMY_VSP_ID, VERSION, DUMMY_COMPONENT_ID);
238     componentEntity.setQuestionnaireData(componentQuestionnaireData);
239
240     Collection<ComponentEntity> componentEntities = new ArrayList<>();
241     componentEntities.add(componentEntity);
242     doReturn(componentEntities).when(componentDao).list(anyObject());
243     doReturn(componentEntity).when(componentDao).getQuestionnaireData(DUMMY_VSP_ID,
244         VERSION, DUMMY_COMPONENT_ID);
245
246     ComputeEntity computeEntity = new ComputeEntity(DUMMY_VSP_ID, VERSION,
247         DUMMY_COMPONENT_ID, DUMMY_COMPUTE_ID);
248     Collection<ComputeEntity> computeEntities = new ArrayList<>();
249     computeEntities.add(computeEntity);
250     doReturn(computeEntities).when(computeDao).list(anyObject());
251
252     ImageEntity imageEntity = new ImageEntity(DUMMY_VSP_ID, VERSION,
253         DUMMY_COMPONENT_ID, DUMMY_IMAGE_ID);
254     Collection<ImageEntity> imageEntities = new ArrayList<>();
255     imageEntities.add(imageEntity);
256     doReturn(imageEntities).when(imageDao).list(anyObject());
257
258     doNothing().when(componentDao).updateQuestionnaireData(anyObject(),
259         anyObject(), anyObject(), anyObject());
260   }
261 }