8e3c46bade48328f8dd4a6e1e8f6a337c5fc4c85
[dcaegen2/services.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * BBS-RELOCATION-CPE-AUTHENTICATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2019 NOKIA 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.bbs.event.processor.utilities;
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.givenThat;
26 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
27 import static org.mockito.Mockito.when;
28
29 import com.github.tomakehurst.wiremock.WireMockServer;
30 import com.github.tomakehurst.wiremock.client.WireMock;
31 import com.google.gson.Gson;
32 import com.google.gson.GsonBuilder;
33 import com.google.gson.TypeAdapterFactory;
34
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.ServiceLoader;
39
40 import javax.net.ssl.SSLException;
41
42 import org.junit.jupiter.api.AfterEach;
43 import org.junit.jupiter.api.Assertions;
44 import org.junit.jupiter.api.BeforeAll;
45 import org.junit.jupiter.api.BeforeEach;
46 import org.junit.jupiter.api.Test;
47 import org.junit.jupiter.api.extension.ExtendWith;
48 import org.mockito.Mockito;
49 import org.onap.bbs.event.processor.config.AaiClientConfiguration;
50 import org.onap.bbs.event.processor.config.ApplicationConfiguration;
51 import org.onap.bbs.event.processor.model.ImmutableMetadataEntryAaiObject;
52 import org.onap.bbs.event.processor.model.ImmutableMetadataListAaiObject;
53 import org.onap.bbs.event.processor.model.ImmutablePnfAaiObject;
54 import org.onap.bbs.event.processor.model.ImmutablePropertyAaiObject;
55 import org.onap.bbs.event.processor.model.ImmutableRelationshipDataEntryAaiObject;
56 import org.onap.bbs.event.processor.model.ImmutableRelationshipEntryAaiObject;
57 import org.onap.bbs.event.processor.model.ImmutableRelationshipListAaiObject;
58 import org.onap.bbs.event.processor.model.ImmutableServiceInstanceAaiObject;
59 import org.onap.bbs.event.processor.model.MetadataListAaiObject;
60 import org.onap.bbs.event.processor.model.PnfAaiObject;
61 import org.onap.bbs.event.processor.model.RelationshipListAaiObject;
62 import org.onap.bbs.event.processor.model.ServiceInstanceAaiObject;
63 import org.springframework.test.context.junit.jupiter.SpringExtension;
64
65 import reactor.test.StepVerifier;
66
67 @ExtendWith(SpringExtension.class)
68 class AaiReactiveClientTest {
69
70     private static final int PORT = 9999;
71
72     private static AaiReactiveClient reactiveClient;
73     private static Gson gson;
74     private static WireMockServer wireMockServer;
75
76     @BeforeAll
77     static void init() throws SSLException {
78         GsonBuilder gsonBuilder = new GsonBuilder();
79         ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
80         gson = gsonBuilder.create();
81
82         ApplicationConfiguration configuration = Mockito.mock(ApplicationConfiguration.class);
83         AaiClientConfiguration aaiClientConfiguration = Mockito.mock(AaiClientConfiguration.class);
84         when(configuration.getAaiClientConfiguration()).thenReturn(aaiClientConfiguration);
85         when(aaiClientConfiguration.aaiUserName()).thenReturn("AAI");
86         when(aaiClientConfiguration.aaiUserPassword()).thenReturn("AAI");
87         when(aaiClientConfiguration.aaiHeaders()).thenReturn(new HashMap<>());
88         when(aaiClientConfiguration.enableAaiCertAuth()).thenReturn(false);
89
90         reactiveClient = new AaiReactiveClient(configuration, gson);
91
92         wireMockServer = new WireMockServer(PORT);
93         WireMock.configureFor("localhost", PORT);
94     }
95
96     @BeforeEach
97     void wireMockSetup() {
98         wireMockServer.start();
99     }
100
101     @AfterEach
102     void wireMockTearDown() {
103         wireMockServer.start();
104     }
105
106     @Test
107     void sendingReactiveRequestForPnf_Succeeds() {
108
109         String pnfName = "pnf-1";
110         String attachmentPoint = "olt1-1-1";
111
112         String pnfUrl = String.format("/aai/v14/network/pnfs/pnf/%s?depth=1", pnfName);
113
114         // Build Relationship Data
115         RelationshipListAaiObject.RelationshipEntryAaiObject firstRelationshipEntry =
116                 ImmutableRelationshipEntryAaiObject.builder()
117                         .relatedTo("service-instance")
118                         .relatedLink("/aai/v14/business/customers/customer/Demonstration/service-subscriptions"
119                                 + "/service-subscription/BBS/service-instances"
120                                 + "/service-instance/84003b26-6b76-4c75-b805-7b14ab4ffaef")
121                         .relationshipLabel("org.onap.relationships.inventory.ComposedOf")
122                         .relationshipData(Arrays.asList(
123                                 ImmutableRelationshipDataEntryAaiObject.builder()
124                                         .relationshipKey("customer.global-customer-id")
125                                         .relationshipValue("Demonstration").build(),
126                                 ImmutableRelationshipDataEntryAaiObject.builder()
127                                         .relationshipKey("service-subscription.service-type")
128                                         .relationshipValue("BBS").build(),
129                                 ImmutableRelationshipDataEntryAaiObject.builder()
130                                         .relationshipKey("service-instance.service-instance-id")
131                                         .relationshipValue("84003b26-6b76-4c75-b805-7b14ab4ffaef").build())
132                         )
133                         .relatedToProperties(Collections.singletonList(
134                                 ImmutablePropertyAaiObject.builder()
135                                         .propertyKey("service-instance.service-instance-name")
136                                         .propertyValue("bbs-instance").build())
137                         )
138                         .build();
139
140         RelationshipListAaiObject.RelationshipEntryAaiObject secondRelationshipEntry =
141                 ImmutableRelationshipEntryAaiObject.builder()
142                         .relatedTo("logical-link")
143                         .relatedLink("/network/logical-links/logical-link/" + attachmentPoint)
144                         .relationshipData(Collections.singletonList(ImmutableRelationshipDataEntryAaiObject.builder()
145                                 .relationshipKey("logical-link.link-name")
146                                 .relationshipValue(attachmentPoint).build()))
147                         .build();
148
149         RelationshipListAaiObject relationshipListAaiObject = ImmutableRelationshipListAaiObject.builder()
150                 .relationshipEntries(Arrays.asList(firstRelationshipEntry, secondRelationshipEntry))
151                 .build();
152
153         // Finally construct PNF object data
154         PnfAaiObject pnfAaiObject = ImmutablePnfAaiObject.builder()
155                 .pnfName(pnfName)
156                 .isInMaintenance(true)
157                 .relationshipListAaiObject(relationshipListAaiObject)
158                 .build();
159
160         givenThat(get(urlEqualTo(pnfUrl))
161                 .willReturn(aResponse().withStatus(200)
162                         .withHeader("Content-Type", "application/json")
163                         .withBody(gson.toJson(pnfAaiObject, ImmutablePnfAaiObject.class))));
164
165         StepVerifier.create(reactiveClient.getPnfObjectDataFor("http://127.0.0.1:" + PORT + pnfUrl))
166                 .expectSubscription()
167                 .consumeNextWith(pnf -> {
168                     Assertions.assertEquals(pnfName, pnf.getPnfName(), "PNF Name in response does not match");
169                     String extractedAttachmentPoint = pnf.getRelationshipListAaiObject().getRelationshipEntries()
170                             .stream()
171                             .filter(e -> e.getRelatedTo().equals("logical-link"))
172                             .flatMap(e -> e.getRelationshipData().stream())
173                             .filter(d -> d.getRelationshipKey().equals("logical-link.link-name"))
174                             .map(RelationshipListAaiObject.RelationshipDataEntryAaiObject::getRelationshipValue)
175                             .findFirst().orElseThrow(AaiReactiveClientTestException::new);
176                     Assertions.assertEquals(attachmentPoint, extractedAttachmentPoint,
177                             "Attachment point in response does not match");
178                 })
179                 .verifyComplete();
180     }
181
182     @Test
183     void sendingReactiveRequestForServiceInstance_Succeeds() {
184
185         String serviceInstanceId = "84003b26-6b76-4c75-b805-7b14ab4ffaef";
186         String orchestrationStatus = "active";
187
188         String serviceInstanceUrl =
189                 String.format("/aai/v14/nodes/service-instances/service-instance/%s?format=resource_and_url",
190                 serviceInstanceId);
191
192         // Build Relationship Data
193         RelationshipListAaiObject.RelationshipEntryAaiObject relationshipEntry =
194                 ImmutableRelationshipEntryAaiObject.builder()
195                         .relatedTo("service-instance")
196                         .relatedLink("/aai/v14/business/customers/customer/Demonstration/service-subscriptions"
197                                 + "/service-subscription/BBS-CFS/service-instances"
198                                 + "/service-instance/bb374844-44e4-488f-8381-fb5a0e3e6989")
199                         .relationshipLabel("org.onap.relationships.inventory.ComposedOf")
200                         .relationshipData(Collections.singletonList(ImmutableRelationshipDataEntryAaiObject.builder()
201                                 .relationshipKey("service-instance.service-instance-id")
202                                 .relationshipValue("bb374844-44e4-488f-8381-fb5a0e3e6989").build()))
203                         .build();
204
205         RelationshipListAaiObject relationshipListAaiObject = ImmutableRelationshipListAaiObject.builder()
206                 .relationshipEntries(Collections.singletonList(relationshipEntry))
207                 .build();
208
209         MetadataListAaiObject.MetadataEntryAaiObject metadataEntry =
210                 ImmutableMetadataEntryAaiObject.builder()
211                         .metaname("cvlan")
212                         .metavalue("1005")
213                         .build();
214
215         MetadataListAaiObject metadataListAaiObject = ImmutableMetadataListAaiObject.builder()
216                 .metadataEntries(Collections.singletonList(metadataEntry))
217                 .build();
218
219         // Finally construct Service Instance object data
220         ServiceInstanceAaiObject serviceInstanceAaiObject = ImmutableServiceInstanceAaiObject.builder()
221                 .serviceInstanceId(serviceInstanceId)
222                 .orchestrationStatus(orchestrationStatus)
223                 .relationshipListAaiObject(relationshipListAaiObject)
224                 .metadataListAaiObject(metadataListAaiObject)
225                 .build();
226
227         givenThat(get(urlEqualTo(serviceInstanceUrl))
228                 .willReturn(aResponse().withStatus(200)
229                         .withHeader("Content-Type", "application/json")
230                         .withBody(gson.toJson(serviceInstanceAaiObject, ImmutableServiceInstanceAaiObject.class))));
231
232         StepVerifier.create(
233                 reactiveClient.getServiceInstanceObjectDataFor("http://127.0.0.1:" + PORT + serviceInstanceUrl)
234         )
235                 .expectSubscription()
236                 .consumeNextWith(serviceInstance -> {
237                     Assertions.assertEquals(serviceInstanceId, serviceInstance.getServiceInstanceId(),
238                             "Service Instance ID in response does not match");
239
240                     MetadataListAaiObject extractedMetadataListObject =
241                             serviceInstance.getMetadataListAaiObject().orElseThrow(AaiReactiveClientTestException::new);
242
243                     MetadataListAaiObject.MetadataEntryAaiObject extractedMetadataEntry =
244                             extractedMetadataListObject.getMetadataEntries()
245                             .stream()
246                             .filter(m -> m.getMetaname().equals("cvlan"))
247                             .findFirst().orElseThrow(AaiReactiveClientTestException::new);
248
249                     Assertions.assertEquals("1005", extractedMetadataEntry.getMetavalue(),
250                             "CVLAN in response does not match");
251                 })
252                 .verifyComplete();
253     }
254
255     private static class AaiReactiveClientTestException extends RuntimeException {}
256
257 }