6c5e968835001eabc3cb9e6bf69aa1abbce0d1d9
[so.git] / mso-api-handlers / mso-api-handler-infra / src / test / java / org / onap / so / apihandlerinfra / tenantisolation / helpers / AAIClientHelperTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.apihandlerinfra.tenantisolation.helpers;
22
23 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
24 import static com.github.tomakehurst.wiremock.client.WireMock.get;
25 import static com.github.tomakehurst.wiremock.client.WireMock.post;
26 import static com.github.tomakehurst.wiremock.client.WireMock.put;
27 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.fail;
30 import static org.mockito.ArgumentMatchers.any;
31 import static org.mockito.ArgumentMatchers.anyString;
32 import static org.mockito.Mockito.doNothing;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.times;
35 import static org.mockito.Mockito.verify;
36
37 import java.util.HashMap;
38 import java.util.Map;
39 import java.util.Optional;
40
41 import org.apache.http.HttpStatus;
42 import org.junit.Test;
43 import org.onap.aai.domain.yang.OperationalEnvironment;
44 import org.onap.so.apihandlerinfra.BaseTest;
45 import org.onap.so.client.aai.AAIVersion;
46 import org.onap.so.client.aai.entities.AAIResultWrapper;
47 import org.springframework.beans.factory.annotation.Autowired;
48
49
50
51 public class AAIClientHelperTest extends BaseTest{
52     
53         @Autowired
54         private AAIClientHelper clientHelper;
55         
56         @Test
57         public void testGetAaiOperationalEnvironmentSuccess() throws Exception { 
58                 wireMockServer.stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
59                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/ecompOperationalEnvironment.json").withStatus(HttpStatus.SC_ACCEPTED)));
60                 
61                 AAIResultWrapper wrapper = clientHelper.getAaiOperationalEnvironment("EMOE-001");
62                 Optional<OperationalEnvironment> aaiOpEnv = wrapper.asBean(OperationalEnvironment.class);
63                 assertEquals("EMOE-001", aaiOpEnv.get().getOperationalEnvironmentId());
64         }
65         
66         @Test
67         public void testUpdateSuccess() { 
68                 wireMockServer.stubFor(post(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
69                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
70                 
71                 OperationalEnvironment ecompEnv = new OperationalEnvironment();
72                 ecompEnv.setTenantContext("Test");
73                 ecompEnv.setWorkloadContext("ECOMPL_PSL");
74                 
75                 try {
76                         AAIClientHelper clientHelper = mock(AAIClientHelper.class);
77                         doNothing().when(clientHelper).updateAaiOperationalEnvironment(any(String.class), any(OperationalEnvironment.class));
78                         clientHelper.updateAaiOperationalEnvironment("EMOE-001", ecompEnv);
79                         
80                         verify(clientHelper, times(1)).updateAaiOperationalEnvironment("EMOE-001", ecompEnv);
81                 } catch(Exception e) {
82                         fail("shouldn't reach here");
83                 }
84         }
85         
86         @Test
87         public void testUpdateMapSuccess() { 
88                 wireMockServer.stubFor(post(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
89                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
90                 
91                 Map<String, String> payload = new HashMap<String, String>();
92                 payload.put("tenant-context", "Test");
93                 payload.put("workload-context", "ECOMPL_PSL");
94                 payload.put("operational-environment-status", "ACTIVE");
95                 
96                 try {
97                         AAIClientHelper clientHelper = mock(AAIClientHelper.class);
98                         doNothing().when(clientHelper).updateAaiOperationalEnvironment("EMOE-001", payload);
99                         clientHelper.updateAaiOperationalEnvironment("EMOE-001", payload);
100                         
101                         verify(clientHelper, times(1)).updateAaiOperationalEnvironment("EMOE-001", payload);
102                 } catch(Exception e) {
103                         fail("shouldn't reach here");
104                 }
105         }       
106         
107         @Test
108         public void testCreateSuccess() { 
109                 wireMockServer.stubFor(put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
110                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
111                 
112                 OperationalEnvironment ecompEnv = new OperationalEnvironment();
113                 ecompEnv.setOperationalEnvironmentId("opeEvnId");
114                 ecompEnv.setTenantContext("Test");
115                 ecompEnv.setWorkloadContext("ECOMPL_PSL");
116                 
117                 try {
118                         AAIClientHelper clientHelper = mock(AAIClientHelper.class);
119                         doNothing().when(clientHelper).createOperationalEnvironment(any(OperationalEnvironment.class));
120                         clientHelper.createOperationalEnvironment(ecompEnv);
121                         
122                         verify(clientHelper, times(1)).createOperationalEnvironment(ecompEnv);
123                 } catch(Exception e) {
124                         fail("shouldn't reach here");
125                 }
126         }
127         
128         @Test
129         public void testcreateRelationshipSuccess() { 
130                 wireMockServer.stubFor(put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
131                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
132                 
133                 OperationalEnvironment ecompEnv = new OperationalEnvironment();
134                 ecompEnv.setTenantContext("Test");
135                 ecompEnv.setWorkloadContext("ECOMPL_PSL");
136                 
137                 try {
138                         AAIClientHelper clientHelper = mock(AAIClientHelper.class);
139                         doNothing().when(clientHelper).createRelationship(anyString(), anyString());
140                         clientHelper.createRelationship("managingEcomp", "vnfOp");
141                         
142                         verify(clientHelper, times(1)).createRelationship("managingEcomp", "vnfOp");
143                 } catch(Exception e) {
144                         fail("shouldn't reach here");
145                 }
146         }
147 }