2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 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.api.impl.subscriptions
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.onap.cps.api.CpsDataService
25 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent
26 import org.onap.cps.spi.model.DataNodeBuilder
27 import org.onap.cps.utils.JsonObjectMapper
28 import spock.lang.Specification
30 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NO_TIMESTAMP
32 class SubscriptionPersistenceSpec extends Specification {
34 private static final String SUBSCRIPTION_DATASPACE_NAME = "NCMP-Admin";
35 private static final String SUBSCRIPTION_ANCHOR_NAME = "AVC-Subscriptions";
36 private static final String SUBSCRIPTION_REGISTRY_PARENT = "/subscription-registry";
38 def jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
40 def mockCpsDataService = Mock(CpsDataService)
42 def objectUnderTest = new SubscriptionPersistenceImpl(jsonObjectMapper, mockCpsDataService)
44 def 'save a subscription event' () {
45 given: 'a yang model subscription event'
46 def predicates = new YangModelSubscriptionEvent.Predicates(datastore: 'some-datastore',
47 targetCmHandles: [new YangModelSubscriptionEvent.TargetCmHandle('cmhandle1'),
48 new YangModelSubscriptionEvent.TargetCmHandle('cmhandle2')])
49 def yangModelSubscriptionEvent = new YangModelSubscriptionEvent(clientId: 'some-client-id',
50 subscriptionName: 'some-subscription-name', tagged: true, topic: 'some-topic', predicates: predicates)
51 and: 'a data node that does not exist in db'
52 def dataNodeNonExist = new DataNodeBuilder().withDataspace('NCMP-Admin')
53 .withAnchor('AVC-Subscriptions').withXpath('/subscription-registry').build()
54 and: 'cps data service return non existing data node'
55 mockCpsDataService.getDataNodes(*_) >> [dataNodeNonExist]
56 when: 'the yangModelSubscriptionEvent is saved into db'
57 objectUnderTest.saveSubscriptionEvent(yangModelSubscriptionEvent)
58 then: 'the cpsDataService save operation is called with the correct data'
59 1 * mockCpsDataService.saveListElements(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_ANCHOR_NAME,
60 SUBSCRIPTION_REGISTRY_PARENT,
61 '{"subscription":[{' +
62 '"topic":"some-topic",' +
63 '"predicates":{"datastore":"some-datastore","targetCmHandles":[{"cmHandleId":"cmhandle1","status":"PENDING"},{"cmHandleId":"cmhandle2","status":"PENDING"}]},' +
64 '"clientID":"some-client-id","subscriptionName":"some-subscription-name","isTagged":true}]}',
68 def 'update a subscription event' () {
69 given: 'a yang model subscription event'
70 def predicates = new YangModelSubscriptionEvent.Predicates(datastore: 'some-datastore',
71 targetCmHandles: [new YangModelSubscriptionEvent.TargetCmHandle('cmhandle1'),
72 new YangModelSubscriptionEvent.TargetCmHandle('cmhandle2')])
73 def yangModelSubscriptionEvent = new YangModelSubscriptionEvent(clientId: 'some-client-id',
74 subscriptionName: 'some-subscription-name', tagged: true, topic: 'some-topic', predicates: predicates)
75 and: 'a data node exist in db'
76 def childDataNode = new DataNodeBuilder().withDataspace('NCMP-Admin')
77 .withAnchor('AVC-Subscriptions').withXpath('/subscription-registry/subscription').build()
78 def dataNodeExist = new DataNodeBuilder().withDataspace('NCMP-Admin')
79 .withAnchor('AVC-Subscriptions').withXpath('/subscription-registry')
80 .withChildDataNodes([childDataNode]).build()
81 and: 'cps data service return existing data node'
82 mockCpsDataService.getDataNodes(*_) >> [dataNodeExist]
83 when: 'the yangModelSubscriptionEvent is saved into db'
84 objectUnderTest.saveSubscriptionEvent(yangModelSubscriptionEvent)
85 then: 'the cpsDataService update operation is called with the correct data'
86 1 * mockCpsDataService.updateDataNodeAndDescendants(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_ANCHOR_NAME,
87 SUBSCRIPTION_REGISTRY_PARENT,
88 '{"subscription":[{' +
89 '"topic":"some-topic",' +
90 '"predicates":{"datastore":"some-datastore","targetCmHandles":[{"cmHandleId":"cmhandle1","status":"PENDING"},{"cmHandleId":"cmhandle2","status":"PENDING"}]},' +
91 '"clientID":"some-client-id","subscriptionName":"some-subscription-name","isTagged":true}]}',