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