db5f7cb15ac926dce8f5ed21a5bd600ce61a916f
[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.tasks;
22
23 import static org.junit.jupiter.api.Assertions.assertNotNull;
24 import static org.junit.jupiter.api.Assertions.assertNull;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28
29 import com.google.gson.GsonBuilder;
30 import com.google.gson.TypeAdapterFactory;
31
32 import java.time.Duration;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.ServiceLoader;
36
37 import org.junit.jupiter.api.Assertions;
38 import org.junit.jupiter.api.BeforeEach;
39 import org.junit.jupiter.api.Test;
40 import org.mockito.Mockito;
41 import org.onap.bbs.event.processor.exceptions.AaiTaskException;
42 import org.onap.bbs.event.processor.model.ImmutableMetadataEntryAaiObject;
43 import org.onap.bbs.event.processor.model.ImmutableMetadataListAaiObject;
44 import org.onap.bbs.event.processor.model.ImmutablePnfAaiObject;
45 import org.onap.bbs.event.processor.model.ImmutablePropertyAaiObject;
46 import org.onap.bbs.event.processor.model.ImmutableRelationshipDataEntryAaiObject;
47 import org.onap.bbs.event.processor.model.ImmutableRelationshipEntryAaiObject;
48 import org.onap.bbs.event.processor.model.ImmutableRelationshipListAaiObject;
49 import org.onap.bbs.event.processor.model.ImmutableServiceInstanceAaiObject;
50 import org.onap.bbs.event.processor.model.MetadataListAaiObject;
51 import org.onap.bbs.event.processor.model.PnfAaiObject;
52 import org.onap.bbs.event.processor.model.RelationshipListAaiObject;
53 import org.onap.bbs.event.processor.model.ServiceInstanceAaiObject;
54 import org.onap.bbs.event.processor.utilities.AaiReactiveClient;
55
56 import reactor.core.publisher.Mono;
57 import reactor.test.StepVerifier;
58
59 class AaiClientTaskImplTest {
60
61     private AaiReactiveClient reactiveClient;
62
63     private AaiClientTask task;
64
65     @BeforeEach
66     void init() {
67         GsonBuilder gsonBuilder = new GsonBuilder();
68         ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
69         reactiveClient = Mockito.mock(AaiReactiveClient.class);
70         task = new AaiClientTaskImpl(reactiveClient);
71     }
72
73     @Test
74     void passingEmptyPnfObject_NothingHappens() throws AaiTaskException {
75         when(reactiveClient.getPnfObjectDataFor(any(String.class))).thenReturn(Mono.empty());
76         Mono<PnfAaiObject> pnf = task.executePnfRetrieval("Empty PNF task", "some-url");
77
78         verify(reactiveClient).getPnfObjectDataFor("some-url");
79         assertNull(pnf.block(Duration.ofSeconds(5)));
80     }
81
82     @Test
83     void passingEmptyServiceInstanceObject_NothingHappens() throws AaiTaskException {
84         when(reactiveClient.getServiceInstanceObjectDataFor(any(String.class))).thenReturn(Mono.empty());
85         Mono<ServiceInstanceAaiObject> serviceInstance =
86                 task.executeServiceInstanceRetrieval("Empty Service Instance task", "some-url");
87
88         verify(reactiveClient).getServiceInstanceObjectDataFor("some-url");
89         assertNull(serviceInstance.block(Duration.ofSeconds(5)));
90     }
91
92     @Test
93     void passingPnfObject_taskSucceeds() throws AaiTaskException {
94
95         String pnfName = "pnf-1";
96         String attachmentPoint = "olt1-1-1";
97
98         // Build Relationship Data
99         RelationshipListAaiObject.RelationshipEntryAaiObject firstRelationshipEntry =
100                 ImmutableRelationshipEntryAaiObject.builder()
101                         .relatedTo("service-instance")
102                         .relatedLink("/aai/v14/business/customers/customer/Demonstration/service-subscriptions"
103                                 + "/service-subscription/BBS/service-instances"
104                                 + "/service-instance/84003b26-6b76-4c75-b805-7b14ab4ffaef")
105                         .relationshipLabel("org.onap.relationships.inventory.ComposedOf")
106                         .relationshipData(Arrays.asList(
107                                 ImmutableRelationshipDataEntryAaiObject.builder()
108                                         .relationshipKey("customer.global-customer-id")
109                                         .relationshipValue("Demonstration").build(),
110                                 ImmutableRelationshipDataEntryAaiObject.builder()
111                                         .relationshipKey("service-subscription.service-type")
112                                         .relationshipValue("BBS").build(),
113                                 ImmutableRelationshipDataEntryAaiObject.builder()
114                                         .relationshipKey("service-instance.service-instance-id")
115                                         .relationshipValue("84003b26-6b76-4c75-b805-7b14ab4ffaef").build())
116                         )
117                         .relatedToProperties(Collections.singletonList(
118                                 ImmutablePropertyAaiObject.builder()
119                                         .propertyKey("service-instance.service-instance-name")
120                                         .propertyValue("bbs-instance").build())
121                         )
122                         .build();
123
124         RelationshipListAaiObject.RelationshipEntryAaiObject secondRelationshipEntry =
125                 ImmutableRelationshipEntryAaiObject.builder()
126                         .relatedTo("logical-link")
127                         .relatedLink("/network/logical-links/logical-link/" + attachmentPoint)
128                         .relationshipData(Collections.singletonList(ImmutableRelationshipDataEntryAaiObject.builder()
129                                 .relationshipKey("logical-link.link-name")
130                                 .relationshipValue(attachmentPoint).build()))
131                         .build();
132
133         RelationshipListAaiObject relationshipListAaiObject = ImmutableRelationshipListAaiObject.builder()
134                 .relationshipEntries(Arrays.asList(firstRelationshipEntry, secondRelationshipEntry))
135                 .build();
136
137         // Finally construct PNF object data
138         PnfAaiObject pnfAaiObject = ImmutablePnfAaiObject.builder()
139                 .pnfName(pnfName)
140                 .isInMaintenance(true)
141                 .relationshipListAaiObject(relationshipListAaiObject)
142                 .build();
143
144         when(reactiveClient.getPnfObjectDataFor(any(String.class))).thenReturn(Mono.just(pnfAaiObject));
145         Mono<PnfAaiObject> pnf = task.executePnfRetrieval("Normal PNF retrieval task", "some-url");
146
147         verify(reactiveClient).getPnfObjectDataFor("some-url");
148         assertNotNull(pnf.block(Duration.ofSeconds(5)));
149
150         StepVerifier.create(pnf)
151                 .expectSubscription()
152                 .consumeNextWith(aPnf -> {
153                     Assertions.assertEquals(pnfName, aPnf.getPnfName(), "PNF Name in response does not match");
154                     String extractedAttachmentPoint = aPnf.getRelationshipListAaiObject().getRelationshipEntries()
155                             .stream()
156                             .filter(e -> e.getRelatedTo().equals("logical-link"))
157                             .flatMap(e -> e.getRelationshipData().stream())
158                             .filter(d -> d.getRelationshipKey().equals("logical-link.link-name"))
159                             .map(RelationshipListAaiObject.RelationshipDataEntryAaiObject::getRelationshipValue)
160                             .findFirst().orElseThrow(AaiClientTaskTestException::new);
161                     Assertions.assertEquals(attachmentPoint, extractedAttachmentPoint,
162                             "Attachment point in response does not match");
163                 })
164                 .verifyComplete();
165     }
166
167     @Test
168     void passingServiceInstanceObject_taskSucceeds() throws AaiTaskException {
169
170         String serviceInstanceId = "84003b26-6b76-4c75-b805-7b14ab4ffaef";
171         String orchestrationStatus = "active";
172
173         // Build Relationship Data
174         RelationshipListAaiObject.RelationshipEntryAaiObject relationshipEntry =
175                 ImmutableRelationshipEntryAaiObject.builder()
176                         .relatedTo("service-instance")
177                         .relatedLink("/aai/v14/business/customers/customer/Demonstration/service-subscriptions"
178                                 + "/service-subscription/BBS-CFS/service-instances"
179                                 + "/service-instance/bb374844-44e4-488f-8381-fb5a0e3e6989")
180                         .relationshipLabel("org.onap.relationships.inventory.ComposedOf")
181                         .relationshipData(Collections.singletonList(ImmutableRelationshipDataEntryAaiObject.builder()
182                                 .relationshipKey("service-instance.service-instance-id")
183                                 .relationshipValue("bb374844-44e4-488f-8381-fb5a0e3e6989").build()))
184                         .build();
185
186         RelationshipListAaiObject relationshipListAaiObject = ImmutableRelationshipListAaiObject.builder()
187                 .relationshipEntries(Collections.singletonList(relationshipEntry))
188                 .build();
189
190         MetadataListAaiObject.MetadataEntryAaiObject metadataEntry =
191                 ImmutableMetadataEntryAaiObject.builder()
192                         .metaname("cvlan")
193                         .metavalue("1005")
194                         .build();
195
196         MetadataListAaiObject metadataListAaiObject = ImmutableMetadataListAaiObject.builder()
197                 .metadataEntries(Collections.singletonList(metadataEntry))
198                 .build();
199
200         // Finally construct Service Instance object data
201         ServiceInstanceAaiObject serviceInstanceAaiObject = ImmutableServiceInstanceAaiObject.builder()
202                 .serviceInstanceId(serviceInstanceId)
203                 .orchestrationStatus(orchestrationStatus)
204                 .relationshipListAaiObject(relationshipListAaiObject)
205                 .metadataListAaiObject(metadataListAaiObject)
206                 .build();
207
208         when(reactiveClient.getServiceInstanceObjectDataFor(any(String.class)))
209                 .thenReturn(Mono.just(serviceInstanceAaiObject));
210         Mono<ServiceInstanceAaiObject> serviceInstance =
211                 task.executeServiceInstanceRetrieval("Normal Service Instance retrieval task",
212                         "some-url");
213
214         verify(reactiveClient).getServiceInstanceObjectDataFor("some-url");
215         assertNotNull(serviceInstance.block(Duration.ofSeconds(5)));
216
217         StepVerifier.create(serviceInstance)
218                 .expectSubscription()
219                 .consumeNextWith(instance -> {
220                     Assertions.assertEquals(serviceInstanceId, instance.getServiceInstanceId(),
221                             "Service Instance ID in response does not match");
222
223                     MetadataListAaiObject extractedMetadataListObject =
224                             instance.getMetadataListAaiObject().orElseThrow(AaiClientTaskTestException::new);
225
226                     MetadataListAaiObject.MetadataEntryAaiObject extractedMetadataEntry =
227                             extractedMetadataListObject.getMetadataEntries()
228                                     .stream()
229                                     .filter(m -> m.getMetaname().equals("cvlan"))
230                                     .findFirst().orElseThrow(AaiClientTaskTestException::new);
231
232                     Assertions.assertEquals("1005", extractedMetadataEntry.getMetavalue(),
233                             "CVLAN in response does not match");
234                 })
235                 .verifyComplete();
236     }
237
238     private static class AaiClientTaskTestException extends RuntimeException {}
239 }