Rename Role's subscriberId field
[vid.git] / vid-app-common / src / test / java / org / onap / vid / bl / AaiServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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.vid.bl;
22
23 import static org.hamcrest.MatcherAssert.assertThat;
24 import static org.hamcrest.Matchers.arrayWithSize;
25 import static org.hamcrest.Matchers.equalTo;
26 import static org.testng.Assert.assertEquals;
27 import static org.testng.Assert.assertNotNull;
28
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.List;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.MockitoAnnotations;
36 import org.onap.vid.aai.AaiClientInterface;
37 import org.onap.vid.aai.AaiResponse;
38 import org.onap.vid.aai.model.AaiGetPnfResponse;
39 import org.onap.vid.aai.model.AaiGetPnfs.Pnf;
40 import org.onap.vid.aai.model.AaiGetTenatns.GetTenantsResponse;
41 import org.onap.vid.aai.model.LogicalLinkResponse;
42 import org.onap.vid.aai.model.Relationship;
43 import org.onap.vid.aai.model.RelationshipData;
44 import org.onap.vid.aai.model.RelationshipList;
45 import org.onap.vid.aai.model.ServiceRelationships;
46 import org.onap.vid.roles.Role;
47 import org.onap.vid.roles.RoleValidator;
48 import org.onap.vid.services.AaiServiceImpl;
49 import org.testng.annotations.BeforeMethod;
50 import org.testng.annotations.DataProvider;
51 import org.testng.annotations.Test;
52
53 public class AaiServiceTest {
54
55     @InjectMocks
56     private AaiServiceImpl aaiService;
57
58     @Mock
59     private AaiClientInterface aaiClientInterface;
60
61
62
63     @BeforeMethod
64     public void initMocks(){
65         MockitoAnnotations.initMocks(this);
66     }
67
68     @Test
69     public void testGetSpecificPnf(){
70         Pnf pnf = Pnf.builder().withPnfId("11111").build();
71         AaiResponse<Pnf> aaiResponse = new AaiResponse<>(pnf, "aaaa", 200);
72         Mockito.doReturn(aaiResponse).when(aaiClientInterface).getSpecificPnf(Mockito.anyString());
73         AaiResponse<Pnf> specificPnf = aaiService.getSpecificPnf("1345667");
74         assertNotNull(specificPnf);
75         pnf = specificPnf.getT();
76         assertNotNull(pnf);
77         assertEquals("11111",pnf.getPnfId());
78         assertEquals("aaaa",specificPnf.getErrorMessage());
79         assertEquals(200,specificPnf.getHttpCode());
80     }
81
82     @Test
83     public void testPnfByRegion(){
84         AaiGetPnfResponse aaiGetPnfResponse = new AaiGetPnfResponse();
85         AaiResponse<AaiGetPnfResponse> aaiResponse = new AaiResponse<>(aaiGetPnfResponse, "", 200);
86         Mockito.doReturn(aaiResponse).when(aaiClientInterface).getPNFData(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
87         AaiResponse<AaiGetPnfResponse> aaiGetPnfResponseWrapper = aaiService.getPNFData("1345667", "1345667", "1345667", "1345667", "1345667", "1345667", "1345667");
88         assertNotNull(aaiGetPnfResponseWrapper);
89         aaiGetPnfResponse = aaiGetPnfResponseWrapper.getT();
90         assertNotNull(aaiGetPnfResponse);
91     }
92
93     @Test
94     public void testGetAssociatedPnfs(){
95         ServiceRelationships serviceRelationships = createServiceRelationships();
96         AaiResponse<ServiceRelationships> aaiResponse = new AaiResponse<>(serviceRelationships, null, 200);
97         Mockito.doReturn(aaiResponse).when(aaiClientInterface).getServiceInstance(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
98
99         LogicalLinkResponse logicalLinkResponse = createLogicalLinkResponse();
100         AaiResponse<LogicalLinkResponse> aaiResponse1 = new AaiResponse<>(logicalLinkResponse, null, 200);
101         Mockito.doReturn(aaiResponse1).when(aaiClientInterface).getLogicalLink("SANITY6758cce9%3ALAG1992%7CSANITY6785cce9%3ALAG1961");
102
103         List<String> pnfList = aaiService.getServiceInstanceAssociatedPnfs("123", "456", "789");
104         assertNotNull(pnfList);
105         assertEquals(1, pnfList.size());
106         assertEquals("SANITY6785cce9", pnfList.get(0));
107     }
108
109     private ServiceRelationships createServiceRelationships() {
110         ServiceRelationships serviceRelationships = new ServiceRelationships();
111         serviceRelationships.setServiceInstanceName("test service");
112
113         RelationshipData logicalLinksRelationshipData = new RelationshipData();
114         logicalLinksRelationshipData.setRelationshipKey("logical-link.link-name");
115         logicalLinksRelationshipData.setRelationshipValue("SANITY6758cce9:LAG1992|SANITY6785cce9:LAG1961");
116
117         Relationship logicalLinksRelationship = new Relationship();
118         logicalLinksRelationship.setRelatedTo("logical-link");
119         logicalLinksRelationship.setRelationDataList(Arrays.asList(logicalLinksRelationshipData));
120
121         RelationshipList logicalLinksRelationshipsList = new RelationshipList();
122         logicalLinksRelationshipsList.setRelationship(Arrays.asList(logicalLinksRelationship));
123
124         serviceRelationships.setRelationshipList(logicalLinksRelationshipsList);
125         return serviceRelationships;
126     }
127
128     private LogicalLinkResponse createLogicalLinkResponse() {
129         LogicalLinkResponse logicalLinkResponse = new LogicalLinkResponse();
130         logicalLinkResponse.setLinkName("SANITY6758cce9:LAG1992|SANITY6785cce9:LAG1961");
131
132         RelationshipData lagInterfaceRelationshipData = new RelationshipData();
133         lagInterfaceRelationshipData.setRelationshipKey("pnf.pnf-name");
134         lagInterfaceRelationshipData.setRelationshipValue("SANITY6785cce9");
135
136         Relationship lagInterfaceRelationship = new Relationship();
137         lagInterfaceRelationship.setRelatedTo("lag-interface");
138         lagInterfaceRelationship.setRelationDataList(Arrays.asList(lagInterfaceRelationshipData));
139
140         RelationshipList lagInterfaceRelationshipsList = new RelationshipList();
141         lagInterfaceRelationshipsList.setRelationship(Arrays.asList(lagInterfaceRelationship));
142
143         logicalLinkResponse.setRelationshipList(lagInterfaceRelationshipsList);
144
145         return logicalLinkResponse;
146     }
147
148     @DataProvider
149     public static Object[][] getTenantsData() {
150         return new Object[][] {
151                 {"customer1", "serviceType1", "tenant1", "customer1", "serviceType1", "tenant1", "id-1", true},
152                 {"customer1", "serviceType1", "TeNant1", "customer1", "serviceType1", "tenant1", "id-1", true},
153                 {"customer1", "serviceType1", "TENANT1", "customer1", "serviceType1", "tenant1", "id-1", true},
154                 {"customer1", "serviceType1", "tenant2", "customer1", "serviceType1", "tenant1", "tenant2", false},
155                 {"customer1", "serviceType1", null, "customer1", "serviceType1", "tenant1", "tenant2", true},
156                 {"customer2", "serviceType1", "tenant1", "customer1", "serviceType1", "tenant1", "id-1", false},
157                 {"customer1", "serviceType2", "tenant1", "customer1", "serviceType1", "tenant1", "id-1", false},
158                 {"customer2", "serviceType1", null, "customer1", "serviceType1", "tenant1", "id-1", false},
159                 {"customer1", "serviceType2", null, "customer1", "serviceType1", "tenant1", "id-1", false},
160         };
161     }
162
163     @Test(dataProvider = "getTenantsData")
164     public void testGetTenants(String userGlobalCustomerId, String userServiceType, String userTenantName, String serviceGlobalCustomerId,
165                                String serviceServiceType, String serviceTenantName, String serviceTenantId, boolean expectedIsPermitted) {
166         GetTenantsResponse[] getTenantsResponses = new GetTenantsResponse[] {new GetTenantsResponse(null, null, serviceTenantName, serviceTenantId, expectedIsPermitted)};
167         AaiResponse<GetTenantsResponse[]> aaiResponse = new AaiResponse<>(getTenantsResponses, null, 200);
168         Mockito.doReturn(aaiResponse).when(aaiClientInterface).getTenants(serviceGlobalCustomerId, serviceServiceType);
169         Role role = new Role(null, userGlobalCustomerId, userServiceType, userTenantName);
170         RoleValidator roleValidator = RoleValidator.by(Collections.singletonList(role));
171         AaiResponse<GetTenantsResponse[]> actualTenants = aaiService.getTenants(serviceGlobalCustomerId, serviceServiceType, roleValidator);
172
173         assertThat(actualTenants.getT(), arrayWithSize(1));
174         assertThat(actualTenants.getT()[0].tenantName, equalTo(serviceTenantName));
175         //assertThat(actualTenants.getT()[0].isPermitted, equalTo(expectedIsPermitted));
176     }
177 }