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.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;
19 import java.lang.reflect.InvocationTargetException;
20 import java.lang.reflect.Method;
21 import java.util.ArrayList;
22 import java.util.Collection;
24 import static org.mockito.Matchers.anyObject;
25 import static org.mockito.Mockito.doNothing;
26 import static org.mockito.Mockito.doReturn;
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\": \"\"}}}";
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\": \"\"}}}";
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\": \"\"}}}";
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\": \"\"}}}";
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\": \"\"}}}";
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\": \"\"}}}";
111 private static final JsonParser jsonParser = new JsonParser();
112 private ComponentEntity componentEntity;
115 private ImageDao imageDao;
118 private ComputeDao computeDao;
121 private ComponentDao componentDao;
124 private ComponentQuestionnaireHealer componentQuestionnaireHealer;
127 public void init() throws Exception {
128 MockitoAnnotations.initMocks(ComponentQuestionnaireHealerTest.this);
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());
144 public void healAllCasesTest() throws Exception {
145 prepareHealingData();
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));
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));
182 public void handleVMSizingWithVMSizingTest()
183 throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
184 JsonObject jsonObject = (JsonObject) jsonParser.parse(componentQuestionnaireWithoutNumOfVMData);
185 Method method = ComponentQuestionnaireHealer.class.getDeclaredMethod("handleVmSizing",
187 method.setAccessible(true);
188 method.invoke(componentQuestionnaireHealer, jsonObject.getAsJsonObject(COMPUTE));
190 Assert.assertNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(VM_SIZING));
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);
200 Assert.assertNotNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS));
201 Assert.assertNotNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS)
202 .getAsJsonPrimitive("maximum"));
206 public void handleVMSizingWithOnlyMemoryRAMTest()
207 throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
208 JsonObject jsonObject = (JsonObject) jsonParser.parse(componentQuestionnaireWithMemoryRamData);
209 provideAccessToPrivateMethod(HANDLE_NUM_OF_VMS_METHOD, jsonObject);
211 Assert.assertNotNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS));
212 Assert.assertNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS)
213 .getAsJsonPrimitive(MEMORY_RAM));
217 public void handleVMSizingWithOnlyCpuRatioTest()
218 throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
219 JsonObject jsonObject = (JsonObject) jsonParser.parse(componentQuestionnaireWithCPURatioData);
220 provideAccessToPrivateMethod(HANDLE_NUM_OF_VMS_METHOD, jsonObject);
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));
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);
233 method.invoke(componentQuestionnaireHealer, jsonObject.getAsJsonObject(COMPUTE), null);
236 private void prepareHealingData() {
237 componentEntity = new ComponentEntity(DUMMY_VSP_ID, VERSION, DUMMY_COMPONENT_ID);
238 componentEntity.setQuestionnaireData(componentQuestionnaireData);
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);
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());
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());
258 doNothing().when(componentDao).updateQuestionnaireData(anyObject(),
259 anyObject(), anyObject(), anyObject());