2  * ============LICENSE_START=======================================================
 
   3  * Copyright (c) 2024 Nordix Foundation.
 
   4  *  ================================================================================
 
   5  *  Licensed under the Apache License, Version 2.0 (the "License");
 
   6  *  you may not use this file except in compliance with the License.
 
   7  *  You may obtain a copy of the License at
 
   9  *        http://www.apache.org/licenses/LICENSE-2.0
 
  11  *  Unless required by applicable law or agreed to in writing, software
 
  12  *  distributed under the License is distributed on an 'AS IS' BASIS,
 
  13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  14  *  See the License for the specific language governing permissions and
 
  15  *  limitations under the License.
 
  17  *  SPDX-License-Identifier: Apache-2.0
 
  18  *  ============LICENSE_END=========================================================
 
  21 package org.onap.cps.ncmp.impl.cmnotificationsubscription.ncmp
 
  23 import com.fasterxml.jackson.databind.ObjectMapper
 
  24 import org.onap.cps.ncmp.impl.cmnotificationsubscription.EventsFacade
 
  25 import org.onap.cps.ncmp.impl.cmnotificationsubscription.MappersFacade
 
  26 import org.onap.cps.ncmp.impl.cmnotificationsubscription.cache.DmiCacheHandler
 
  27 import org.onap.cps.ncmp.impl.cmnotificationsubscription.models.DmiCmSubscriptionDetails
 
  28 import org.onap.cps.ncmp.impl.cmnotificationsubscription.models.DmiCmSubscriptionPredicate
 
  29 import org.onap.cps.ncmp.impl.cmnotificationsubscription.utils.CmSubscriptionPersistenceService
 
  30 import org.onap.cps.ncmp.impl.cmnotificationsubscription_1_0_0.client_to_ncmp.NcmpInEvent
 
  31 import org.onap.cps.ncmp.impl.cmnotificationsubscription_1_0_0.ncmp_to_client.NcmpOutEvent
 
  32 import org.onap.cps.ncmp.impl.cmnotificationsubscription_1_0_0.ncmp_to_dmi.DmiInEvent
 
  33 import org.onap.cps.ncmp.utils.TestUtils
 
  34 import org.onap.cps.utils.JsonObjectMapper
 
  35 import spock.lang.Specification
 
  37 import static org.onap.cps.ncmp.api.data.models.DatastoreType.PASSTHROUGH_OPERATIONAL
 
  38 import static org.onap.cps.ncmp.impl.cmnotificationsubscription.models.CmSubscriptionStatus.ACCEPTED
 
  39 import static org.onap.cps.ncmp.impl.cmnotificationsubscription.models.CmSubscriptionStatus.PENDING
 
  41 class CmSubscriptionHandlerImplSpec extends Specification {
 
  43     def jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
 
  44     def mockCmSubscriptionPersistenceService = Mock(CmSubscriptionPersistenceService);
 
  45     def mockCmSubscriptionComparator = Mock(CmSubscriptionComparator);
 
  46     def mockMappersFacade = Mock(MappersFacade);
 
  47     def mockEventsFacade = Mock(EventsFacade);
 
  48     def mockDmiCacheHandler = Mock(DmiCacheHandler);
 
  50     def objectUnderTest = new CmSubscriptionHandlerImpl(mockCmSubscriptionPersistenceService,
 
  51         mockCmSubscriptionComparator, mockMappersFacade,
 
  52         mockEventsFacade, mockDmiCacheHandler)
 
  54     def testDmiSubscriptionsPerDmi = ["dmi-1": new DmiCmSubscriptionDetails([], PENDING)]
 
  56     def 'Consume valid and unique CmNotificationSubscriptionNcmpInEvent create message'() {
 
  57         given: 'a cmNotificationSubscriptionNcmp in event with unique subscription id'
 
  58             def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
 
  59             def testEventConsumed = jsonObjectMapper.convertJsonString(jsonData, NcmpInEvent.class)
 
  60             def testListOfDeltaPredicates = [new DmiCmSubscriptionPredicate(['ch1'].toSet(), PASSTHROUGH_OPERATIONAL, ['/a/b'].toSet())]
 
  61             mockCmSubscriptionPersistenceService.isUniqueSubscriptionId("test-id") >> true
 
  62         and: 'relevant details is extracted from the event'
 
  63             def subscriptionId = testEventConsumed.getData().getSubscriptionId()
 
  64             def predicates = testEventConsumed.getData().getPredicates()
 
  65         and: 'the cache handler returns for relevant subscription id'
 
  66             1 * mockDmiCacheHandler.get("test-id") >> testDmiSubscriptionsPerDmi
 
  67         and: 'the delta predicates is returned'
 
  68             1 * mockCmSubscriptionComparator.getNewDmiSubscriptionPredicates(_) >> testListOfDeltaPredicates
 
  69         and: 'the DMI in event mapper returns cm notification subscription event'
 
  70             def testDmiInEvent = new DmiInEvent()
 
  72                 .toDmiInEvent(testListOfDeltaPredicates) >> testDmiInEvent
 
  73         when: 'the valid and unique event is consumed'
 
  74             objectUnderTest.processSubscriptionCreateRequest(subscriptionId, predicates)
 
  75         then: 'the subscription cache handler is called once'
 
  76             1 * mockDmiCacheHandler.add('test-id', _)
 
  77         and: 'the events handler method to publish DMI event is called correct number of times with the correct parameters'
 
  78             testDmiSubscriptionsPerDmi.size() * mockEventsFacade.publishDmiInEvent(
 
  79                 "test-id", "dmi-1", "subscriptionCreateRequest", testDmiInEvent)
 
  80         and: 'we schedule to send the response after configured time from the cache'
 
  81             1 * mockEventsFacade.publishNcmpOutEvent('test-id', 'subscriptionCreateResponse', null, true)
 
  84     def 'Consume valid and Overlapping Cm Notification Subscription NcmpIn Event'() {
 
  85         given: 'a cmNotificationSubscriptionNcmp in event with unique subscription id'
 
  86             def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
 
  87             def testEventConsumed = jsonObjectMapper.convertJsonString(jsonData, NcmpInEvent.class)
 
  88             def noDeltaPredicates = []
 
  89             mockCmSubscriptionPersistenceService.isUniqueSubscriptionId("test-id") >> true
 
  90         and: 'the cache handler returns for relevant subscription id'
 
  91             1 * mockDmiCacheHandler.get('test-id') >> testDmiSubscriptionsPerDmi
 
  92         and: 'the delta predicates is returned'
 
  93             1 * mockCmSubscriptionComparator.getNewDmiSubscriptionPredicates(_) >> noDeltaPredicates
 
  94         when: 'the valid and unique event is consumed'
 
  95             objectUnderTest.processSubscriptionCreateRequest('test-id', noDeltaPredicates)
 
  96         then: 'the subscription cache handler is called once'
 
  97             1 * mockDmiCacheHandler.add('test-id', _)
 
  98         and: 'the subscription details are updated in the cache'
 
  99             1 * mockDmiCacheHandler.updateDmiSubscriptionStatusPerDmi('test-id', _, ACCEPTED)
 
 100         and: 'we schedule to send the response after configured time from the cache'
 
 101             1 * mockEventsFacade.publishNcmpOutEvent('test-id', 'subscriptionCreateResponse', null, true)
 
 104     def 'Consume valid and but non-unique CmNotificationSubscription create message'() {
 
 105         given: 'a cmNotificationSubscriptionNcmp in event'
 
 106             def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
 
 107             def testEventConsumed = jsonObjectMapper.convertJsonString(jsonData, NcmpInEvent.class)
 
 108             mockCmSubscriptionPersistenceService.isUniqueSubscriptionId('test-id') >> false
 
 109         and: 'relevant details is extracted from the event'
 
 110             def subscriptionId = testEventConsumed.getData().getSubscriptionId()
 
 111             def predicates = testEventConsumed.getData().getPredicates()
 
 112         and: 'the NCMP out in event mapper returns an event for rejected request'
 
 113             def testNcmpOutEvent = new NcmpOutEvent()
 
 114             1 * mockMappersFacade.toNcmpOutEventForRejectedRequest(
 
 115                 "test-id", _) >> testNcmpOutEvent
 
 116         when: 'the valid but non-unique event is consumed'
 
 117             objectUnderTest.processSubscriptionCreateRequest(subscriptionId, predicates)
 
 118         then: 'the events handler method to publish DMI event is never called'
 
 119             0 * mockEventsFacade.publishDmiInEvent(_, _, _, _)
 
 120         and: 'the events handler method to publish NCMP out event is called once'
 
 121             1 * mockEventsFacade.publishNcmpOutEvent('test-id', 'subscriptionCreateResponse', testNcmpOutEvent, false)
 
 124     def 'Consume valid CmNotificationSubscriptionNcmpInEvent delete message'() {
 
 125         given: 'a cmNotificationSubscriptionNcmp in event for delete'
 
 126             def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
 
 127             def testEventConsumed = jsonObjectMapper.convertJsonString(jsonData, NcmpInEvent.class)
 
 128         and: 'relevant details is extracted from the event'
 
 129             def subscriptionId = testEventConsumed.getData().getSubscriptionId()
 
 130             def predicates = testEventConsumed.getData().getPredicates()
 
 131         and: 'the cache handler returns for relevant subscription id'
 
 132             1 * mockDmiCacheHandler.get('test-id') >> testDmiSubscriptionsPerDmi
 
 133         when: 'the valid and unique event is consumed'
 
 134             objectUnderTest.processSubscriptionDeleteRequest(subscriptionId, predicates)
 
 135         then: 'the subscription cache handler is called once'
 
 136             1 * mockDmiCacheHandler.add('test-id', predicates)
 
 137         and: 'the mapper handler to get DMI in event is called once'
 
 138             1 * mockMappersFacade.toDmiInEvent(_)
 
 139         and: 'the events handler method to publish DMI event is called correct number of times with the correct parameters'
 
 140             testDmiSubscriptionsPerDmi.size() * mockEventsFacade.publishDmiInEvent(
 
 141                 'test-id', 'dmi-1', 'subscriptionDeleteRequest', _)
 
 142         and: 'we schedule to send the response after configured time from the cache'
 
 143             1 * mockEventsFacade.publishNcmpOutEvent('test-id', 'subscriptionDeleteResponse', null, true)