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
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  21 package org.onap.bbs.event.processor.tasks;
 
  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;
 
  29 import com.google.gson.GsonBuilder;
 
  30 import com.google.gson.TypeAdapterFactory;
 
  32 import java.time.Duration;
 
  33 import java.util.Arrays;
 
  34 import java.util.Collections;
 
  35 import java.util.ServiceLoader;
 
  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;
 
  56 import reactor.core.publisher.Mono;
 
  57 import reactor.test.StepVerifier;
 
  59 class AaiClientTaskImplTest {
 
  61     private AaiReactiveClient reactiveClient;
 
  63     private AaiClientTask task;
 
  67         GsonBuilder gsonBuilder = new GsonBuilder();
 
  68         ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
 
  69         reactiveClient = Mockito.mock(AaiReactiveClient.class);
 
  70         task = new AaiClientTaskImpl(reactiveClient);
 
  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");
 
  78         verify(reactiveClient).getPnfObjectDataFor("some-url");
 
  79         assertNull(pnf.block(Duration.ofSeconds(5)));
 
  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");
 
  88         verify(reactiveClient).getServiceInstanceObjectDataFor("some-url");
 
  89         assertNull(serviceInstance.block(Duration.ofSeconds(5)));
 
  93     void passingPnfObject_taskSucceeds() throws AaiTaskException {
 
  95         String pnfName = "pnf-1";
 
  96         String attachmentPoint = "olt1-1-1";
 
  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())
 
 117                         .relatedToProperties(Collections.singletonList(
 
 118                                 ImmutablePropertyAaiObject.builder()
 
 119                                         .propertyKey("service-instance.service-instance-name")
 
 120                                         .propertyValue("bbs-instance").build())
 
 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()))
 
 133         RelationshipListAaiObject relationshipListAaiObject = ImmutableRelationshipListAaiObject.builder()
 
 134                 .relationshipEntries(Arrays.asList(firstRelationshipEntry, secondRelationshipEntry))
 
 137         // Finally construct PNF object data
 
 138         PnfAaiObject pnfAaiObject = ImmutablePnfAaiObject.builder()
 
 140                 .isInMaintenance(true)
 
 141                 .relationshipListAaiObject(relationshipListAaiObject)
 
 144         when(reactiveClient.getPnfObjectDataFor(any(String.class))).thenReturn(Mono.just(pnfAaiObject));
 
 145         Mono<PnfAaiObject> pnf = task.executePnfRetrieval("Normal PNF retrieval task", "some-url");
 
 147         verify(reactiveClient).getPnfObjectDataFor("some-url");
 
 148         assertNotNull(pnf.block(Duration.ofSeconds(5)));
 
 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()
 
 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");
 
 168     void passingServiceInstanceObject_taskSucceeds() throws AaiTaskException {
 
 170         String serviceInstanceId = "84003b26-6b76-4c75-b805-7b14ab4ffaef";
 
 171         String orchestrationStatus = "active";
 
 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()))
 
 186         RelationshipListAaiObject relationshipListAaiObject = ImmutableRelationshipListAaiObject.builder()
 
 187                 .relationshipEntries(Collections.singletonList(relationshipEntry))
 
 190         MetadataListAaiObject.MetadataEntryAaiObject metadataEntry =
 
 191                 ImmutableMetadataEntryAaiObject.builder()
 
 196         MetadataListAaiObject metadataListAaiObject = ImmutableMetadataListAaiObject.builder()
 
 197                 .metadataEntries(Collections.singletonList(metadataEntry))
 
 200         // Finally construct Service Instance object data
 
 201         ServiceInstanceAaiObject serviceInstanceAaiObject = ImmutableServiceInstanceAaiObject.builder()
 
 202                 .serviceInstanceId(serviceInstanceId)
 
 203                 .orchestrationStatus(orchestrationStatus)
 
 204                 .relationshipListAaiObject(relationshipListAaiObject)
 
 205                 .metadataListAaiObject(metadataListAaiObject)
 
 208         when(reactiveClient.getServiceInstanceObjectDataFor(any(String.class)))
 
 209                 .thenReturn(Mono.just(serviceInstanceAaiObject));
 
 210         Mono<ServiceInstanceAaiObject> serviceInstance =
 
 211                 task.executeServiceInstanceRetrieval("Normal Service Instance retrieval task",
 
 214         verify(reactiveClient).getServiceInstanceObjectDataFor("some-url");
 
 215         assertNotNull(serviceInstance.block(Duration.ofSeconds(5)));
 
 217         StepVerifier.create(serviceInstance)
 
 218                 .expectSubscription()
 
 219                 .consumeNextWith(instance -> {
 
 220                     Assertions.assertEquals(serviceInstanceId, instance.getServiceInstanceId(),
 
 221                             "Service Instance ID in response does not match");
 
 223                     MetadataListAaiObject extractedMetadataListObject =
 
 224                             instance.getMetadataListAaiObject().orElseThrow(AaiClientTaskTestException::new);
 
 226                     MetadataListAaiObject.MetadataEntryAaiObject extractedMetadataEntry =
 
 227                             extractedMetadataListObject.getMetadataEntries()
 
 229                                     .filter(m -> m.getMetaname().equals("cvlan"))
 
 230                                     .findFirst().orElseThrow(AaiClientTaskTestException::new);
 
 232                     Assertions.assertEquals("1005", extractedMetadataEntry.getMetavalue(),
 
 233                             "CVLAN in response does not match");
 
 238     private static class AaiClientTaskTestException extends RuntimeException {}