1 package org.openecomp.sdc.be.components.distribution.engine;
4 import org.apache.http.HttpStatus;
5 import org.junit.Before;
7 import org.junit.runner.RunWith;
8 import org.mockito.InjectMocks;
9 import org.mockito.Mock;
10 import org.mockito.Mockito;
11 import org.mockito.junit.MockitoJUnitRunner;
12 import org.openecomp.sdc.be.config.ConfigurationManager;
13 import org.openecomp.sdc.be.config.DistributionEngineConfiguration;
14 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
15 import org.openecomp.sdc.be.dao.cassandra.OperationalEnvironmentDao;
16 import org.openecomp.sdc.be.datatypes.enums.EnvironmentStatusEnum;
17 import org.openecomp.sdc.be.info.OperationalEnvInfo;
18 import org.openecomp.sdc.be.resources.data.OperationalEnvironmentEntry;
19 import org.openecomp.sdc.common.http.client.api.HttpResponse;
21 import java.io.IOException;
22 import java.util.Arrays;
23 import java.util.List;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.Mockito.when;
30 @RunWith(value = MockitoJUnitRunner.class)
31 public class EnvironmentsEngineMockTest {
34 private EnvironmentsEngine envEngine;
36 private DmaapConsumer dmaapConsumer;
38 private OperationalEnvironmentDao operationalEnvironmentDao;
40 private DME2EndpointIteratorCreator epIterCreator;
42 private ConfigurationManager configurationManager;
44 private DistributionEngineConfiguration distributionEngineConfiguration;
46 private AaiRequestHandler aaiRequestHandler;
49 public void preStart() {
50 when(configurationManager.getDistributionEngineConfiguration()).thenReturn(distributionEngineConfiguration);
51 envEngine.setConfigurationManager(configurationManager);
55 public void testInit() {
56 List<OperationalEnvironmentEntry> entryList = Arrays.asList(createOpEnvEntry("Env1"), createOpEnvEntry("Env2"));
57 Either<List<OperationalEnvironmentEntry>, CassandraOperationStatus> successEither = Either.left(entryList);
58 when(operationalEnvironmentDao.getByEnvironmentsStatus(EnvironmentStatusEnum.COMPLETED)).thenReturn(successEither);
60 when(distributionEngineConfiguration.getEnvironments()).thenReturn(Arrays.asList("Env Loaded From Configuration"));
61 when(distributionEngineConfiguration.getUebPublicKey()).thenReturn("Dummy Public Key");
62 when(distributionEngineConfiguration.getUebSecretKey()).thenReturn("Dummy Private Key");
63 when(distributionEngineConfiguration.getUebServers()).thenReturn(
64 Arrays.asList("uebsb91kcdc.it.att.com:3904", "uebsb92kcdc.it.att.com:3904", "uebsb91kcdc.it.att.com:3904"));
68 Map<String, OperationalEnvironmentEntry> mapEnvs = envEngine.getEnvironments();
69 assertEquals("unexpected size of map",3, mapEnvs.size());
74 public void testGetFullOperationalEnvByIdSuccess() {
75 String json = getFullOperationalEnvJson();
77 HttpResponse<String> restResponse = new HttpResponse<String>(json, HttpStatus.SC_OK, "Successfully completed");
78 when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
80 Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
81 assertTrue("The operational environment request ran as not expected", response.isLeft());
83 OperationalEnvInfo operationalEnvInfo = response.left().value();
85 assertEquals("The operational environment json is not as expected", operationalEnvInfo.toString(), json);
89 public void testGetPartialOperationalEnvByIdSuccess() {
90 String json = getPartialOperationalEnvJson();
92 HttpResponse<String> restResponse = new HttpResponse<String>(json, HttpStatus.SC_OK, "Successfully completed");
93 when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
95 Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
96 assertTrue("The operational environment request ran as not expected", response.isLeft());
98 OperationalEnvInfo operationalEnvInfo = response.left().value();
100 assertEquals("The operational environment json is not as expected", operationalEnvInfo.toString(), json);
105 public void testGetOperationalEnvByIdFailedByJsonConvert() {
106 String jsonCorrupted = getCorruptedOperationalEnvJson();
108 HttpResponse<String> restResponse = new HttpResponse<String>(jsonCorrupted, HttpStatus.SC_OK, "Successfully Completed");
109 when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
111 Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
112 assertTrue("The operational environment request ran as not expected", response.isRight());
113 assertEquals("The operational environment request status code is not as expected", (Integer)HttpStatus.SC_INTERNAL_SERVER_ERROR, response.right().value());
117 public void testGetOperationalEnvByIdFailed404() {
118 String json = getFullOperationalEnvJson();
119 HttpResponse<String> restResponse = new HttpResponse<String>(json, HttpStatus.SC_NOT_FOUND, "Not Found");
120 when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
122 Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
123 assertTrue("The operational environment request ran as not expected", response.isRight());
124 assertEquals("The operational environment request status code is not as expected", (Integer)HttpStatus.SC_NOT_FOUND, response.right().value());
128 @Test(expected = IOException.class)
129 public void testCorruptedOperationalEnvJson() throws IOException {
130 String jsonCorrupted = getCorruptedOperationalEnvJson();
131 OperationalEnvInfo.createFromJson(jsonCorrupted);
134 private String getCorruptedOperationalEnvJson() {
135 return "{\"OPERATIONAL-environment-name\":\"Op Env Name\","
136 + "\"OPERATIONAL-environment-type\":\"VNF\","
137 + "\"OPERATIONAL-environment-status\":\"Activate\","
138 + "\"tenant-context\":\"Test\"}";
141 private String getPartialOperationalEnvJson() {
143 "\"operational-environment-id\":\"UUID of Operational Environment\"," +
144 "\"operational-environment-name\":\"Op Env Name\"," +
145 "\"operational-environment-type\":\"VNF\"," +
146 "\"operational-environment-status\":\"Activate\"," +
147 "\"tenant-context\":\"Test\"," +
148 "\"workload-context\":\"VNF_Development\"," +
149 "\"resource-version\":\"1505228226913\"," +
150 "\"relationship-list\":{" +
151 "\"relationship\":[]" +
156 private String getFullOperationalEnvJson() {
158 "\"operational-environment-id\":\"OEid1\"," +
159 "\"operational-environment-name\":\"OEname1\"," +
160 "\"operational-environment-type\":\"OEtype1\"," +
161 "\"operational-environment-status\":\"OEstatus1\"," +
162 "\"tenant-context\":\"OEtenantcontext1\"," +
163 "\"workload-context\":\"OEworkloadcontext1\"," +
164 "\"resource-version\":\"1511363173278\"," +
165 "\"relationship-list\":{" +
166 "\"relationship\":[" +
168 "\"related-to\":\"operational-environment\"," +
169 "\"relationship-label\":\"managedBy\"," +
170 "\"related-link\":\"/aai/v12/cloud-infrastructure/operational-environments/operational-environment/OEid3\"," +
171 "\"relationship-data\":[" +
173 "\"relationship-key\":\"operational-environment.operational-environment-id\"," +
174 "\"relationship-value\":\"OEid3\"" +
177 "\"related-to-property\":[" +
179 "\"property-key\":\"operational-environment.operational-environment-name\"," +
180 "\"property-value\":\"OEname3\"" +
184 private OperationalEnvironmentEntry createOpEnvEntry(String name) {
185 OperationalEnvironmentEntry entry = new OperationalEnvironmentEntry();
186 entry.setEnvironmentId(name);