re base code
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / distribution / engine / EnvironmentsEngineMockTest.java
1 package org.openecomp.sdc.be.components.distribution.engine;
2
3 import fj.data.Either;
4 import org.apache.http.HttpStatus;
5 import org.junit.Before;
6 import org.junit.Test;
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;
20
21 import java.io.IOException;
22 import java.util.Arrays;
23 import java.util.List;
24 import java.util.Map;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.Mockito.when;
29
30 @RunWith(value = MockitoJUnitRunner.class)
31 public class EnvironmentsEngineMockTest {
32
33     @InjectMocks
34     private EnvironmentsEngine envEngine;
35     @Mock
36     private DmaapConsumer dmaapConsumer;
37     @Mock
38     private OperationalEnvironmentDao operationalEnvironmentDao;
39     @Mock
40     private DME2EndpointIteratorCreator epIterCreator;
41     @Mock
42     private ConfigurationManager configurationManager;
43     @Mock
44     private DistributionEngineConfiguration distributionEngineConfiguration;
45     @Mock
46     private AaiRequestHandler aaiRequestHandler;
47
48     @Before
49     public void preStart() {
50         when(configurationManager.getDistributionEngineConfiguration()).thenReturn(distributionEngineConfiguration);
51         envEngine.setConfigurationManager(configurationManager);
52     }
53
54     @Test
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);
59
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"));
65
66         envEngine.init();
67
68         Map<String, OperationalEnvironmentEntry> mapEnvs = envEngine.getEnvironments();
69         assertEquals("unexpected size of map",3, mapEnvs.size());
70     }
71
72
73     @Test
74     public void testGetFullOperationalEnvByIdSuccess() {
75         String json = getFullOperationalEnvJson();
76         
77         HttpResponse<String> restResponse = new HttpResponse<String>(json, HttpStatus.SC_OK, "Successfully completed");
78         when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
79         
80         Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
81         assertTrue("The operational environment request ran as not expected", response.isLeft());
82         
83         OperationalEnvInfo operationalEnvInfo = response.left().value();
84
85         assertEquals("The operational environment json is not as expected", operationalEnvInfo.toString(), json);
86     }
87
88     @Test
89     public void testGetPartialOperationalEnvByIdSuccess() {
90         String json = getPartialOperationalEnvJson();
91         
92         HttpResponse<String> restResponse = new HttpResponse<String>(json, HttpStatus.SC_OK, "Successfully completed");
93         when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
94         
95         Either<OperationalEnvInfo, Integer> response = envEngine.getOperationalEnvById("DummyId");
96         assertTrue("The operational environment request ran as not expected", response.isLeft());
97         
98         OperationalEnvInfo operationalEnvInfo = response.left().value();
99
100         assertEquals("The operational environment json is not as expected", operationalEnvInfo.toString(), json);
101     }
102
103     
104     @Test
105     public void testGetOperationalEnvByIdFailedByJsonConvert() {
106         String jsonCorrupted = getCorruptedOperationalEnvJson();
107         
108         HttpResponse<String> restResponse = new HttpResponse<String>(jsonCorrupted, HttpStatus.SC_OK, "Successfully Completed");
109         when(aaiRequestHandler.getOperationalEnvById(Mockito.anyString())).thenReturn(restResponse);
110         
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());
114     }
115
116     @Test
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);
121         
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());
125     }
126
127
128     @Test(expected = IOException.class)
129     public void testCorruptedOperationalEnvJson() throws IOException {
130         String jsonCorrupted = getCorruptedOperationalEnvJson();
131         OperationalEnvInfo.createFromJson(jsonCorrupted);
132     }
133     
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\"}";
139     }
140
141     private String getPartialOperationalEnvJson() {
142         return "{" +
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\":[]" +
152                 "}" +
153              "}";
154     }
155
156     private String getFullOperationalEnvJson() {
157         return  "{" +
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\":[" +
167                 "{" +
168                 "\"related-to\":\"operational-environment\"," +
169                 "\"relationship-label\":\"managedBy\"," +
170                 "\"related-link\":\"/aai/v12/cloud-infrastructure/operational-environments/operational-environment/OEid3\"," +
171                 "\"relationship-data\":[" +
172                 "{" +
173                 "\"relationship-key\":\"operational-environment.operational-environment-id\"," +
174                 "\"relationship-value\":\"OEid3\"" +
175                 "}" +
176                 "]," +
177                 "\"related-to-property\":[" +
178                 "{" +
179                 "\"property-key\":\"operational-environment.operational-environment-name\"," +
180                 "\"property-value\":\"OEname3\"" +
181                 "}]}]}}";
182     }
183     
184     private OperationalEnvironmentEntry createOpEnvEntry(String name) {
185         OperationalEnvironmentEntry entry = new OperationalEnvironmentEntry();
186         entry.setEnvironmentId(name);
187         return entry;
188     }
189
190 }