1 package org.openecomp.sdc.be.components.distribution.engine;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5 import static org.mockito.Mockito.when;
7 import java.io.IOException;
8 import java.util.Arrays;
12 import org.apache.http.HttpStatus;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16 import org.mockito.InjectMocks;
17 import org.mockito.Mock;
18 import org.mockito.Mockito;
19 import org.mockito.junit.MockitoJUnitRunner;
20 import org.openecomp.sdc.be.config.ConfigurationManager;
21 import org.openecomp.sdc.be.config.DistributionEngineConfiguration;
22 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
23 import org.openecomp.sdc.be.dao.cassandra.OperationalEnvironmentDao;
24 import org.openecomp.sdc.be.datatypes.enums.EnvironmentStatusEnum;
25 import org.openecomp.sdc.be.info.OperationalEnvInfo;
26 import org.openecomp.sdc.be.resources.data.OperationalEnvironmentEntry;
27 import org.openecomp.sdc.common.http.client.api.HttpResponse;
29 import fj.data.Either;
31 @RunWith(value = MockitoJUnitRunner.class)
32 public class EnvironmentsEngineMockTest {
35 private EnvironmentsEngine envEngine;
37 private DmaapConsumer dmaapConsumer;
39 private OperationalEnvironmentDao operationalEnvironmentDao;
41 private DME2EndpointIteratorCreator epIterCreator;
43 private ConfigurationManager configurationManager;
45 private DistributionEngineConfiguration distributionEngineConfiguration;
47 private AaiRequestHandler aaiRequestHandler;
50 public void preStart() {
51 when(configurationManager.getDistributionEngineConfiguration()).thenReturn(distributionEngineConfiguration);
52 envEngine.setConfigurationManager(configurationManager);
56 public void testInit() {
57 List<OperationalEnvironmentEntry> entryList = Arrays.asList(createOpEnvEntry("Env1"), createOpEnvEntry("Env2"));
58 Either<List<OperationalEnvironmentEntry>, CassandraOperationStatus> successEither = Either.left(entryList);
59 when(operationalEnvironmentDao.getByEnvironmentsStatus(EnvironmentStatusEnum.COMPLETED)).thenReturn(successEither);
61 when(distributionEngineConfiguration.getEnvironments()).thenReturn(Arrays.asList("Env Loaded From Configuration"));
62 when(distributionEngineConfiguration.getUebPublicKey()).thenReturn("Dummy Public Key");
63 when(distributionEngineConfiguration.getUebSecretKey()).thenReturn("Dummy Private Key");
64 when(distributionEngineConfiguration.getUebServers()).thenReturn(
65 Arrays.asList("uebsb91kcdc.it.att.com:3904", "uebsb92kcdc.it.att.com:3904", "uebsb91kcdc.it.att.com:3904"));
69 Map<String, OperationalEnvironmentEntry> mapEnvs = envEngine.getEnvironments();
70 assertEquals("unexpected size of map",3, mapEnvs.size());
75 public void testGetFullOperationalEnvByIdSuccess() {
76 String json = getFullOperationalEnvJson();
78 HttpResponse<String> restResponse = new HttpResponse<String>(json, HttpStatus.SC_OK, "Successfully completed");
79 when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
81 Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
82 assertTrue("The operational environment request ran as not expected", response.isLeft());
84 OperationalEnvInfo operationalEnvInfo = response.left().value();
86 assertEquals("The operational environment json is not as expected", operationalEnvInfo.toString(), json);
90 public void testGetPartialOperationalEnvByIdSuccess() {
91 String json = getPartialOperationalEnvJson();
93 HttpResponse<String> restResponse = new HttpResponse<String>(json, HttpStatus.SC_OK, "Successfully completed");
94 when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
96 Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
97 assertTrue("The operational environment request ran as not expected", response.isLeft());
99 OperationalEnvInfo operationalEnvInfo = response.left().value();
101 assertEquals("The operational environment json is not as expected", operationalEnvInfo.toString(), json);
106 public void testGetOperationalEnvByIdFailedByJsonConvert() {
107 String jsonCorrupted = getCorruptedOperationalEnvJson();
109 HttpResponse<String> restResponse = new HttpResponse<String>(jsonCorrupted, HttpStatus.SC_OK, "Successfully Completed");
110 when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
112 Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
113 assertTrue("The operational environment request ran as not expected", response.isRight());
114 assertEquals("The operational environment request status code is not as expected", (Integer)HttpStatus.SC_INTERNAL_SERVER_ERROR, response.right().value());
118 public void testGetOperationalEnvByIdFailed404() {
119 String json = getFullOperationalEnvJson();
120 HttpResponse<String> restResponse = new HttpResponse<String>(json, HttpStatus.SC_NOT_FOUND, "Not Found");
121 when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
123 Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
124 assertTrue("The operational environment request ran as not expected", response.isRight());
125 assertEquals("The operational environment request status code is not as expected", (Integer)HttpStatus.SC_NOT_FOUND, response.right().value());
129 @Test(expected = IOException.class)
130 public void testCorruptedOperationalEnvJson() throws IOException {
131 String jsonCorrupted = getCorruptedOperationalEnvJson();
132 OperationalEnvInfo.createFromJson(jsonCorrupted);
135 private String getCorruptedOperationalEnvJson() {
136 return "{\"OPERATIONAL-environment-name\":\"Op Env Name\","
137 + "\"OPERATIONAL-environment-type\":\"VNF\","
138 + "\"OPERATIONAL-environment-status\":\"Activate\","
139 + "\"tenant-context\":\"Test\"}";
142 private String getPartialOperationalEnvJson() {
144 "\"operational-environment-id\":\"UUID of Operational Environment\"," +
145 "\"operational-environment-name\":\"Op Env Name\"," +
146 "\"operational-environment-type\":\"VNF\"," +
147 "\"operational-environment-status\":\"Activate\"," +
148 "\"tenant-context\":\"Test\"," +
149 "\"workload-context\":\"VNF_Development\"," +
150 "\"resource-version\":\"1505228226913\"," +
151 "\"relationship-list\":{" +
152 "\"relationship\":[]" +
157 private String getFullOperationalEnvJson() {
159 "\"operational-environment-id\":\"OEid1\"," +
160 "\"operational-environment-name\":\"OEname1\"," +
161 "\"operational-environment-type\":\"OEtype1\"," +
162 "\"operational-environment-status\":\"OEstatus1\"," +
163 "\"tenant-context\":\"OEtenantcontext1\"," +
164 "\"workload-context\":\"OEworkloadcontext1\"," +
165 "\"resource-version\":\"1511363173278\"," +
166 "\"relationship-list\":{" +
167 "\"relationship\":[" +
169 "\"related-to\":\"operational-environment\"," +
170 "\"relationship-label\":\"managedBy\"," +
171 "\"related-link\":\"/aai/v12/cloud-infrastructure/operational-environments/operational-environment/OEid3\"," +
172 "\"relationship-data\":[" +
174 "\"relationship-key\":\"operational-environment.operational-environment-id\"," +
175 "\"relationship-value\":\"OEid3\"" +
178 "\"related-to-property\":[" +
180 "\"property-key\":\"operational-environment.operational-environment-name\"," +
181 "\"property-value\":\"OEname3\"" +
185 private OperationalEnvironmentEntry createOpEnvEntry(String name) {
186 OperationalEnvironmentEntry entry = new OperationalEnvironmentEntry();
187 entry.setEnvironmentId(name);