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.cache
 
  23 import com.fasterxml.jackson.databind.ObjectMapper
 
  24 import io.cloudevents.CloudEvent
 
  25 import io.cloudevents.core.builder.CloudEventBuilder
 
  26 import org.apache.kafka.clients.consumer.ConsumerRecord
 
  27 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
 
  28 import org.onap.cps.ncmp.impl.cmnotificationsubscription.models.CmSubscriptionStatus
 
  29 import org.onap.cps.ncmp.impl.cmnotificationsubscription.models.DmiCmSubscriptionDetails
 
  30 import org.onap.cps.ncmp.impl.cmnotificationsubscription.utils.CmSubscriptionPersistenceService
 
  31 import org.onap.cps.ncmp.impl.cmnotificationsubscription_1_0_0.client_to_ncmp.NcmpInEvent
 
  32 import org.onap.cps.ncmp.impl.inventory.InventoryPersistence
 
  33 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle
 
  34 import org.onap.cps.ncmp.utils.TestUtils
 
  35 import org.onap.cps.utils.JsonObjectMapper
 
  36 import org.spockframework.spring.SpringBean
 
  37 import org.springframework.beans.factory.annotation.Autowired
 
  38 import org.springframework.boot.test.context.SpringBootTest
 
  40 import static org.onap.cps.ncmp.utils.events.CloudEventMapper.toTargetEvent
 
  42 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
 
  43 class DmiCacheHandlerSpec extends MessagingBaseSpec {
 
  46     JsonObjectMapper jsonObjectMapper
 
  48     ObjectMapper objectMapper
 
  50     InventoryPersistence mockInventoryPersistence = Mock(InventoryPersistence)
 
  52     CmSubscriptionPersistenceService mockCmSubscriptionPersistenceService = Mock(CmSubscriptionPersistenceService)
 
  55     def objectUnderTest = new DmiCacheHandler(mockCmSubscriptionPersistenceService, testCache, mockInventoryPersistence)
 
  57     NcmpInEvent ncmpInEvent
 
  58     def yangModelCmHandle1 = new YangModelCmHandle(id:'ch1',dmiServiceName:'dmi-1')
 
  59     def yangModelCmHandle2 = new YangModelCmHandle(id:'ch2',dmiServiceName:'dmi-2')
 
  60     def yangModelCmHandle3 = new YangModelCmHandle(id:'ch3',dmiServiceName:'dmi-1')
 
  61     def yangModelCmHandle4 = new YangModelCmHandle(id:'ch4',dmiServiceName:'dmi-2')
 
  65         initialiseMockInventoryPersistenceResponses()
 
  68     def 'Load CM subscription event to cache'() {
 
  69         given: 'a valid subscription event with Id'
 
  70             def subscriptionId = ncmpInEvent.getData().getSubscriptionId()
 
  71         and: 'list of predicates'
 
  72             def predicates = ncmpInEvent.getData().getPredicates()
 
  73         when: 'a valid event object loaded in cache'
 
  74             objectUnderTest.add(subscriptionId, predicates)
 
  75         then: 'the cache contains the correct entry with #subscriptionId subscription ID'
 
  76             assert testCache.containsKey(subscriptionId)
 
  79     def 'Get cache entry via subscription id'() {
 
  80         given: 'the cache contains value for some-id'
 
  81             testCache.put('some-id',[:])
 
  82         when: 'the get method is called'
 
  83             def result = objectUnderTest.get('some-id')
 
  84         then: 'correct value is returned as expected'
 
  88     def 'Remove accepted and rejected entries from cache via subscription id'() {
 
  89         given: 'a map as the value for cache entry for some-id'
 
  92                 new DmiCmSubscriptionDetails([],CmSubscriptionStatus.ACCEPTED))
 
  94                 new DmiCmSubscriptionDetails([],CmSubscriptionStatus.REJECTED))
 
  96                 new DmiCmSubscriptionDetails([],CmSubscriptionStatus.PENDING))
 
  97             testCache.put("test-id", testMap)
 
  98             assert testCache.get("test-id").size() == 3
 
  99         when: 'the method to remove accepted and rejected entries for test-id is called'
 
 100             objectUnderTest.removeAcceptedAndRejectedDmiSubscriptionEntries("test-id")
 
 101         then: 'all entries with status accepted/rejected are no longer present for test-id'
 
 102             testCache.get("test-id").each { key, testResultMap ->
 
 103                 assert testResultMap.cmSubscriptionStatus != CmSubscriptionStatus.ACCEPTED
 
 104                     || testResultMap.cmSubscriptionStatus != CmSubscriptionStatus.REJECTED
 
 106         and: 'the size of the map for cache entry test-id is as expected'
 
 107             assert testCache.get("test-id").size() == 1
 
 110     def 'Create map for DMI cm notification subscription per DMI service name'() {
 
 111         given: 'list of predicates from the create subscription event'
 
 112             def predicates = ncmpInEvent.getData().getPredicates()
 
 113         when: 'method to create map of DMI cm notification subscription per DMI service name is called'
 
 114             def result = objectUnderTest.createDmiSubscriptionsPerDmi(predicates)
 
 115         then: 'the result size of resulting map is correct to the number of DMIs'
 
 116             assert result.size() == 2
 
 117         and: 'the cache objects per DMI exists'
 
 118             def resultMapForDmi1 = result.get('dmi-1')
 
 119             def resultMapForDmi2 = result.get('dmi-2')
 
 120             assert resultMapForDmi1 != null
 
 121             assert resultMapForDmi2 != null
 
 122         and: 'the size of predicates in each object is correct'
 
 123             assert resultMapForDmi1.dmiCmSubscriptionPredicates.size() == 2
 
 124             assert resultMapForDmi2.dmiCmSubscriptionPredicates.size() == 2
 
 125         and: 'the subscription status in each object is correct'
 
 126             assert resultMapForDmi1.cmSubscriptionStatus.toString() == 'PENDING'
 
 127             assert resultMapForDmi2.cmSubscriptionStatus.toString() == 'PENDING'
 
 128         and: 'the target cmHandles for each predicate is correct'
 
 129             assert resultMapForDmi1.dmiCmSubscriptionPredicates[0].targetCmHandleIds == ['ch1'].toSet()
 
 130             assert resultMapForDmi1.dmiCmSubscriptionPredicates[1].targetCmHandleIds == ['ch3'].toSet()
 
 132             assert resultMapForDmi2.dmiCmSubscriptionPredicates[0].targetCmHandleIds == ['ch2'].toSet()
 
 133             assert resultMapForDmi2.dmiCmSubscriptionPredicates[1].targetCmHandleIds == ['ch4'].toSet()
 
 134         and: 'the list of xpath for each is correct'
 
 135             assert resultMapForDmi1.dmiCmSubscriptionPredicates[0].xpaths
 
 136                 && resultMapForDmi2.dmiCmSubscriptionPredicates[0].xpaths == ['/x1/y1', 'x2/y2'].toSet()
 
 138             assert resultMapForDmi1.dmiCmSubscriptionPredicates[1].xpaths
 
 139                 && resultMapForDmi2.dmiCmSubscriptionPredicates[1].xpaths == ['/x3/y3', 'x4/y4'].toSet()
 
 142     def 'Get map for cm handle IDs by DMI service name'() {
 
 143         given: 'the predicate from the test request CM subscription event'
 
 144             def targetFilter = ncmpInEvent.getData().getPredicates().get(0).getTargetFilter()
 
 145         when: 'the method to group all target CM handles by DMI service name is called'
 
 146             def mapOfCMHandleIDsByDmi = objectUnderTest.groupTargetCmHandleIdsByDmi(targetFilter)
 
 147         then: 'the size of the resulting map is correct'
 
 148             assert mapOfCMHandleIDsByDmi.size() == 2
 
 149         and: 'the values in the map is as expected'
 
 150             assert mapOfCMHandleIDsByDmi.get('dmi-1') == ['ch1'].toSet()
 
 151             assert mapOfCMHandleIDsByDmi.get('dmi-2') == ['ch2'].toSet()
 
 154     def 'Update subscription status in cache per DMI service name'() {
 
 155         given: 'populated cache'
 
 156             def predicates = ncmpInEvent.getData().getPredicates()
 
 157             def subscriptionId = ncmpInEvent.getData().getSubscriptionId()
 
 158             objectUnderTest.add(subscriptionId, predicates)
 
 159         when: 'subscription status per dmi is updated in cache'
 
 160             objectUnderTest.updateDmiSubscriptionStatusPerDmi(subscriptionId,'dmi-1', CmSubscriptionStatus.ACCEPTED)
 
 161         then: 'verify status has been updated in cache'
 
 162             def predicate = testCache.get(subscriptionId)
 
 163             assert predicate.get('dmi-1').cmSubscriptionStatus == CmSubscriptionStatus.ACCEPTED
 
 166     def 'Persist Cache into database per dmi'() {
 
 167         given: 'populated cache'
 
 168             def predicates = ncmpInEvent.getData().getPredicates()
 
 169             def subscriptionId = ncmpInEvent.getData().getSubscriptionId()
 
 170             objectUnderTest.add(subscriptionId, predicates)
 
 171         when: 'subscription is persisted in database'
 
 172             objectUnderTest.persistIntoDatabasePerDmi(subscriptionId,'dmi-1')
 
 173         then: 'persistence service is called the correct number of times per dmi'
 
 174             4 * mockCmSubscriptionPersistenceService.addCmSubscription(_,_,_,subscriptionId)
 
 177     def 'Remove subscription from database per dmi'() {
 
 178         given: 'populated cache'
 
 179             def predicates = ncmpInEvent.getData().getPredicates()
 
 180             def subscriptionId = ncmpInEvent.getData().getSubscriptionId()
 
 181             objectUnderTest.add(subscriptionId, predicates)
 
 182         when: 'subscription is persisted in database'
 
 183             objectUnderTest.removeFromDatabasePerDmi(subscriptionId,'dmi-1')
 
 184         then: 'persistence service is called the correct number of times per dmi'
 
 185             4 * mockCmSubscriptionPersistenceService.removeCmSubscription(_,_,_,subscriptionId)
 
 188     def setUpTestEvent(){
 
 189         def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
 
 190         def testEventSent = jsonObjectMapper.convertJsonString(jsonData, NcmpInEvent.class)
 
 191         def testCloudEventSent = CloudEventBuilder.v1()
 
 192             .withData(objectMapper.writeValueAsBytes(testEventSent))
 
 193             .withId('subscriptionCreated')
 
 194             .withType('subscriptionCreated')
 
 195             .withSource(URI.create('some-resource'))
 
 196             .withExtension('correlationid', 'test-cmhandle1').build()
 
 197         def consumerRecord = new ConsumerRecord<String, CloudEvent>('topic-name', 0, 0, 'event-key', testCloudEventSent)
 
 198         def cloudEvent = consumerRecord.value()
 
 200         ncmpInEvent = toTargetEvent(cloudEvent, NcmpInEvent.class);
 
 203     def initialiseMockInventoryPersistenceResponses(){
 
 204         mockInventoryPersistence.getYangModelCmHandles(['ch1','ch2'])
 
 205             >> [yangModelCmHandle1, yangModelCmHandle2]
 
 207         mockInventoryPersistence.getYangModelCmHandles(['ch3','ch4'])
 
 208             >> [yangModelCmHandle3, yangModelCmHandle4]