Springboot 2.0 upgrade
[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.stubFor;
28 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.fail;
31 import static org.mockito.ArgumentMatchers.any;
32 import static org.mockito.Matchers.anyString;
33 import static org.mockito.Mockito.doNothing;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.times;
36 import static org.mockito.Mockito.verify;
37
38 import java.util.HashMap;
39 import java.util.Map;
40 import java.util.Optional;
41
42 import org.apache.http.HttpStatus;
43 import org.junit.Test;
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.onap.so.client.aai.objects.AAIOperationalEnvironment;
48 import org.springframework.beans.factory.annotation.Autowired;
49
50
51
52 public class AAIClientHelperTest extends BaseTest{
53     
54         @Autowired
55         private AAIClientHelper clientHelper;
56         
57         @Test
58         public void testGetAaiOperationalEnvironmentSuccess() throws Exception { 
59                 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
60                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/ecompOperationalEnvironment.json").withStatus(HttpStatus.SC_ACCEPTED)));
61                 
62                 AAIResultWrapper wrapper = clientHelper.getAaiOperationalEnvironment("EMOE-001");
63                 Optional<AAIOperationalEnvironment> aaiOpEnv = wrapper.asBean(AAIOperationalEnvironment.class);
64                 assertEquals("EMOE-001", aaiOpEnv.get().getOperationalEnvironmentId());
65         }
66         
67         @Test
68         public void testUpdateSuccess() { 
69                 stubFor(post(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
70                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
71                 
72                 AAIOperationalEnvironment ecompEnv = new AAIOperationalEnvironment();
73                 ecompEnv.setTenantContext("Test");
74                 ecompEnv.setWorkloadContext("ECOMPL_PSL");
75                 
76                 try {
77                         AAIClientHelper clientHelper = mock(AAIClientHelper.class);
78                         doNothing().when(clientHelper).updateAaiOperationalEnvironment(any(String.class), any(AAIOperationalEnvironment.class));
79                         clientHelper.updateAaiOperationalEnvironment("EMOE-001", ecompEnv);
80                         
81                         verify(clientHelper, times(1)).updateAaiOperationalEnvironment("EMOE-001", ecompEnv);
82                 } catch(Exception e) {
83                         fail("shouldn't reach here");
84                 }
85         }
86         
87         @Test
88         public void testUpdateMapSuccess() { 
89                 stubFor(post(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
90                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
91                 
92                 Map<String, String> payload = new HashMap<String, String>();
93                 payload.put("tenant-context", "Test");
94                 payload.put("workload-context", "ECOMPL_PSL");
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                 stubFor(put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
110                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
111                 
112                 AAIOperationalEnvironment ecompEnv = new AAIOperationalEnvironment();
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(AAIOperationalEnvironment.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                 stubFor(put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
131                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
132                 
133                 AAIOperationalEnvironment ecompEnv = new AAIOperationalEnvironment();
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 }