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