summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/test/java/org/openecomp/sdc/healing/healers/ComponentQuestionnaireHealerTest.java
blob: 688d1d423630cddedcc601a94f85e449dea875bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package org.openecomp.sdc.healing.healers;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.ComputeDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.ImageDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComputeEntity;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ImageEntity;
import org.openecomp.sdc.versioning.dao.types.Version;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;

import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;

public class ComponentQuestionnaireHealerTest {
  private static final String HANDLE_NUM_OF_VMS_METHOD = "handleNumOfVm";
  private static final String GENERAL = "general";
  private static final String IMAGE = "image";
  private static final String FORMAT = "format";
  private static final String CPU_OVER_SUBSCRIPTION_RATIO = "CpuOverSubscriptionRatio";
  private static final String MEMORY_RAM = "MemoryRAM";
  private static final String VM_SIZING = "vmSizing";
  private static final String COMPUTE = "compute";
  private static final String NUM_OF_VMS = "numOfVMs";
  private static final String DISK = "disk";
  private static final String BOOT_DISK_SIZE_PER_VM = "bootDiskSizePerVM";
  private static final String EPHEMERAL_DISK_SIZE_PER_VM = "ephemeralDiskSizePerVM";
  private static final Version VERSION = new Version(0, 1);
  private static final String DUMMY_VSP_ID = "1495ef442f964cbfb00d82bd54292f89";
  private static final String DUMMY_COMPONENT_ID = "2495ef442f964cbfb00d82bd54292f89";
  private static final String DUMMY_COMPUTE_ID = "3495ef442f964cbfb00d82bd54292f89";
  private static final String DUMMY_IMAGE_ID = "4495ef442f964cbfb00d82bd54292f89";
  private static final String componentQuestionnaireData = "{\"compute\": {" +
      "\"guestOS\": {\"bitSize\": 64},\"vmSizing\": {\"IOOperationsPerSec\": \"0\"}," +
      "\"numOfVMs\": {\"CpuOverSubscriptionRatio\": \"1:1\",\"MemoryRAM\": \"2 GB\"}}," +
      "\"general\": {\"image\": {\"providedBy\": \"AIC\",\"format\":\"qcow2\"," +
      "\"bootDiskSizePerVM\": \"100\",\"ephemeralDiskSizePerVM\": \"200\"},\"hypervisor\": {" +
      "\"hypervisor\": \"KVM\" } },\"highAvailabilityAndLoadBalancing\": {" +
      "\"isComponentMandatory\": \"\",\"highAvailabilityMode\": \"\"},\"storage\": {" +
      "\"backup\": {\"backupNIC\": \"\",\"backupType\": \"On Site\" }," +
      "\"snapshotBackup\": {\"snapshotFrequency\": \"24\"}},\"network\": {\"networkCapacity\": {" +
      "\"protocolWithHighestTrafficProfileAcrossAllNICs\": \"\"}}}";

  private static final String componentQuestionnaireMissingDiskAttrData = "{\"compute\": {" +
      "\"guestOS\": {\"bitSize\": 64},\"vmSizing\": {\"IOOperationsPerSec\": \"0\"},\"numOfVMs\"" +
      ": {\"CpuOverSubscriptionRatio\": \"1:1\",\"MemoryRAM\": \"2 GB\"}},\"general\": " +
      "{\"image\": {\"providedBy\": \"AIC\",\"format\":\"qcow2\"}," +
      "\"hypervisor\": {\"hypervisor\": \"KVM\" } },\"highAvailabilityAndLoadBalancing\": {" +
      "\"isComponentMandatory\": \"\",\"highAvailabilityMode\": \"\"},\"storage\": {" +
      "\"backup\": {\"backupNIC\": \"\",\"backupType\": \"On Site\" }," +
      "\"snapshotBackup\": {\"snapshotFrequency\": \"24\"}},\"network\": {\"networkCapacity\": {" +
      "\"protocolWithHighestTrafficProfileAcrossAllNICs\": \"\"}}}";

  private static final String componentQuestionnaireWithoutVMSizingData = "{\"compute\": {" +
      "\"guestOS\": {\"bitSize\": 64},\"numOfVMs\": {\"CpuOverSubscriptionRatio\": \"1:1\"," +
      "\"maximum\": \"400\"," +
      "\"MemoryRAM\": \"2 GB\"}},\"general\": {\"image\": {\"providedBy\": \"AIC\",\"format\"" +
      ":\"qcow2\",\"bootDiskSizePerVM\": \"100\",\"ephemeralDiskSizePerVM\": \"200\"}," +
      "\"hypervisor\": {\"hypervisor\": \"KVM\" } },\"highAvailabilityAndLoadBalancing\": {" +
      "\"isComponentMandatory\": \"\",\"highAvailabilityMode\": \"\"},\"storage\": {" +
      "\"backup\": {\"backupNIC\": \"\",\"backupType\": \"On Site\" }," +
      "\"snapshotBackup\": {\"snapshotFrequency\": \"24\"}},\"network\": {\"networkCapacity\": {" +
      "\"protocolWithHighestTrafficProfileAcrossAllNICs\": \"\"}}}";

  private static final String componentQuestionnaireWithoutNumOfVMData = "{\"compute\": " +
      "{\"guestOS\": {\"bitSize\": 64}," +
      "\"vmSizing\": {\"IOOperationsPerSec\": \"0\"}}," +
      "\"general\": {\"image\": {\"providedBy\": \"AIC\",\"format\":\"qcow2\"," +
      "\"bootDiskSizePerVM\": \"100\",\"ephemeralDiskSizePerVM\": \"200\"}," +
      "\"hypervisor\": {\"hypervisor\": \"KVM\" } },\"highAvailabilityAndLoadBalancing\": {" +
      "\"isComponentMandatory\": \"\",\"highAvailabilityMode\": \"\"},\"storage\": {" +
      "\"backup\": {\"backupNIC\": \"\",\"backupType\": \"On Site\" }," +
      "\"snapshotBackup\": {\"snapshotFrequency\": \"24\"}},\"network\": {\"networkCapacity\": {" +
      "\"protocolWithHighestTrafficProfileAcrossAllNICs\": \"\"}}}";

  private static final String componentQuestionnaireWithMemoryRamData = "{\"compute\": " +
      "{\"guestOS\": {\"bitSize\": 64}," +
      "\"vmSizing\": {\"IOOperationsPerSec\": \"0\"},\"numOfVMs\": {\"MemoryRAM\": \"2 GB\"}}," +
      "\"general\": {\"image\": {\"providedBy\": \"AIC\",\"format\":\"qcow2\"," +
      "\"bootDiskSizePerVM\": \"100\",\"ephemeralDiskSizePerVM\": \"200\"}," +
      "\"hypervisor\": {\"hypervisor\": \"KVM\" } },\"highAvailabilityAndLoadBalancing\": {" +
      "\"isComponentMandatory\": \"\",\"highAvailabilityMode\": \"\"},\"storage\": {" +
      "\"backup\": {\"backupNIC\": \"\",\"backupType\": \"On Site\" }," +
      "\"snapshotBackup\": {\"snapshotFrequency\": \"24\"}},\"network\": {\"networkCapacity\": {" +
      "\"protocolWithHighestTrafficProfileAcrossAllNICs\": \"\"}}}";

  private static final String componentQuestionnaireWithCPURatioData = "{\"compute\": " +
      "{\"guestOS\": {\"bitSize\": 64},\"vmSizing\": {\"IOOperationsPerSec\": " +
      "\"0\"},\"numOfVMs\": {\"CpuOverSubscriptionRatio\": " +
      "\"1:1\"}},\"general\": {\"image\": {\"providedBy\": \"AIC\",\"format\":\"qcow2\"," +
      "\"bootDiskSizePerVM\": \"100\",\"ephemeralDiskSizePerVM\": \"200\"}," +
      "\"hypervisor\": {\"hypervisor\": \"KVM\" } },\"highAvailabilityAndLoadBalancing\": {" +
      "\"isComponentMandatory\": \"\",\"highAvailabilityMode\": \"\"},\"storage\": {" +
      "\"backup\": {\"backupNIC\": \"\",\"backupType\": \"On Site\" }," +
      "\"snapshotBackup\": {\"snapshotFrequency\": \"24\"}},\"network\": {\"networkCapacity\": {" +
      "\"protocolWithHighestTrafficProfileAcrossAllNICs\": \"\"}}}";

  private static final JsonParser jsonParser = new JsonParser();
  private ComponentEntity componentEntity;

  @Mock
  private ImageDao imageDao;

  @Mock
  private ComputeDao computeDao;

  @Mock
  private ComponentDao componentDao;

  @InjectMocks
  private ComponentQuestionnaireHealer componentQuestionnaireHealer;

  @Before
  public void init() throws Exception {
    MockitoAnnotations.initMocks(ComponentQuestionnaireHealerTest.this);
  }

  @Test
  public void healQuestionnaireNullTest() throws Exception {
    prepareHealingData();
    componentEntity.setQuestionnaireData(null);
    Object returnObject = componentQuestionnaireHealer.heal(DUMMY_VSP_ID, VERSION);
    Assert.assertTrue(returnObject instanceof Collection);
    Collection<ComponentEntity> componentEntities = (Collection<ComponentEntity>) returnObject;
    componentEntities.forEach(componentEntity -> {
      Assert.assertNull(componentEntity.getQuestionnaireData());
    });
  }

  @Test
  public void healAllCasesTest() throws Exception {
    prepareHealingData();

    Object returnObject = componentQuestionnaireHealer.heal(DUMMY_VSP_ID, VERSION);
    Assert.assertTrue(returnObject instanceof Collection);
    Collection<ComponentEntity> componentEntities = (Collection<ComponentEntity>) returnObject;
    componentEntities.forEach(componentEntity -> {
      JsonObject json = (JsonObject) jsonParser.parse(componentEntity.getQuestionnaireData());
      Assert.assertNotNull(json.getAsJsonObject(GENERAL).getAsJsonObject(DISK));
      Assert.assertNotNull(json.getAsJsonObject(GENERAL).getAsJsonObject(DISK)
          .getAsJsonPrimitive(BOOT_DISK_SIZE_PER_VM));
      Assert.assertNotNull(json.getAsJsonObject(GENERAL).getAsJsonObject(DISK)
          .getAsJsonPrimitive(EPHEMERAL_DISK_SIZE_PER_VM));
      Assert.assertNotNull(json.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS));
      Assert.assertNull(json.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS)
          .getAsJsonPrimitive(MEMORY_RAM));
      Assert.assertNull(json.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS)
          .getAsJsonPrimitive(CPU_OVER_SUBSCRIPTION_RATIO));
      Assert.assertNull(json.getAsJsonObject(GENERAL).getAsJsonObject(IMAGE)
          .getAsJsonPrimitive(FORMAT));
      Assert.assertNull(json.getAsJsonObject(COMPUTE).getAsJsonObject(VM_SIZING));
    });
  }

  @Test
  public void healDiskAttrMissingTest() throws Exception {
    prepareHealingData();
    componentEntity.setQuestionnaireData(componentQuestionnaireMissingDiskAttrData);
    Object returnObject = componentQuestionnaireHealer.heal(DUMMY_VSP_ID, VERSION);
    Assert.assertTrue(returnObject instanceof Collection);
    Collection<ComponentEntity> componentEntities = (Collection<ComponentEntity>) returnObject;
    componentEntities.forEach(componentEntity -> {
      JsonObject json = (JsonObject) jsonParser.parse(componentEntity.getQuestionnaireData());
      Assert.assertNull(json.getAsJsonObject(COMPUTE).getAsJsonObject(VM_SIZING));
    });
  }

  @Test
  public void handleVMSizingWithVMSizingTest()
      throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    JsonObject jsonObject = (JsonObject) jsonParser.parse(componentQuestionnaireWithoutNumOfVMData);
    Method method = ComponentQuestionnaireHealer.class.getDeclaredMethod("handleVmSizing",
        JsonObject.class);
    method.setAccessible(true);
    method.invoke(componentQuestionnaireHealer, jsonObject.getAsJsonObject(COMPUTE));

    Assert.assertNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(VM_SIZING));
  }

  @Test
  public void handleNumOfVMWithoutVMSizingTest()
      throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    JsonObject jsonObject = (JsonObject) jsonParser
        .parse(componentQuestionnaireWithoutVMSizingData);
    provideAccessToPrivateMethod(HANDLE_NUM_OF_VMS_METHOD, jsonObject);

    Assert.assertNotNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS));
    Assert.assertNotNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS)
        .getAsJsonPrimitive("maximum"));
  }

  @Test
  public void handleVMSizingWithOnlyMemoryRAMTest()
      throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    JsonObject jsonObject = (JsonObject) jsonParser.parse(componentQuestionnaireWithMemoryRamData);
    provideAccessToPrivateMethod(HANDLE_NUM_OF_VMS_METHOD, jsonObject);

    Assert.assertNotNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS));
    Assert.assertNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS)
        .getAsJsonPrimitive(MEMORY_RAM));
  }

  @Test
  public void handleVMSizingWithOnlyCpuRatioTest()
      throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    JsonObject jsonObject = (JsonObject) jsonParser.parse(componentQuestionnaireWithCPURatioData);
    provideAccessToPrivateMethod(HANDLE_NUM_OF_VMS_METHOD, jsonObject);

    Assert.assertNotNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS));
    Assert.assertNull(jsonObject.getAsJsonObject(COMPUTE).getAsJsonObject(NUM_OF_VMS)
        .getAsJsonPrimitive(CPU_OVER_SUBSCRIPTION_RATIO));
  }

  private void provideAccessToPrivateMethod(String methodName, JsonObject jsonObject)
      throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    Method method = ComponentQuestionnaireHealer.class
        .getDeclaredMethod(methodName, JsonObject.class, JsonObject.class);
    method.setAccessible(true);

    method.invoke(componentQuestionnaireHealer, jsonObject.getAsJsonObject(COMPUTE), null);
  }

  private void prepareHealingData() {
    componentEntity = new ComponentEntity(DUMMY_VSP_ID, VERSION, DUMMY_COMPONENT_ID);
    componentEntity.setQuestionnaireData(componentQuestionnaireData);

    Collection<ComponentEntity> componentEntities = new ArrayList<>();
    componentEntities.add(componentEntity);
    doReturn(componentEntities).when(componentDao).list(anyObject());
    doReturn(componentEntity).when(componentDao).getQuestionnaireData(DUMMY_VSP_ID,
        VERSION, DUMMY_COMPONENT_ID);

    ComputeEntity computeEntity = new ComputeEntity(DUMMY_VSP_ID, VERSION,
        DUMMY_COMPONENT_ID, DUMMY_COMPUTE_ID);
    Collection<ComputeEntity> computeEntities = new ArrayList<>();
    computeEntities.add(computeEntity);
    doReturn(computeEntities).when(computeDao).list(anyObject());

    ImageEntity imageEntity = new ImageEntity(DUMMY_VSP_ID, VERSION,
        DUMMY_COMPONENT_ID, DUMMY_IMAGE_ID);
    Collection<ImageEntity> imageEntities = new ArrayList<>();
    imageEntities.add(imageEntity);
    doReturn(imageEntities).when(imageDao).list(anyObject());

    doNothing().when(componentDao).updateQuestionnaireData(anyObject(),
        anyObject(), anyObject(), anyObject());
  }
}