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())
39 def mockCpsDataService = Mock(CpsDataService)
40 def objectUnderTest = new SubscriptionPersistenceImpl(jsonObjectMapper, mockCpsDataService)
42 def predicates = new YangModelSubscriptionEvent.Predicates(datastore: 'some-datastore',
43 targetCmHandles: [new YangModelSubscriptionEvent.TargetCmHandle('cmhandle1'),
44 new YangModelSubscriptionEvent.TargetCmHandle('cmhandle2')])
45 def yangModelSubscriptionEvent = new YangModelSubscriptionEvent(clientId: 'some-client-id',
46 subscriptionName: 'some-subscription-name', tagged: true, topic: 'some-topic', predicates: predicates)
48 def 'save a subscription event' () {
49 given: 'a data node that does not exist in db'
50 def blankDataNode = new DataNodeBuilder().withDataspace('NCMP-Admin')
51 .withAnchor('AVC-Subscriptions').withXpath('/subscription-registry').build()
52 and: 'cps data service return non existing data node'
53 mockCpsDataService.getDataNodes(*_) >> [blankDataNode]
54 when: 'the yangModelSubscriptionEvent is saved into db'
55 objectUnderTest.saveSubscriptionEvent(yangModelSubscriptionEvent)
56 then: 'the cpsDataService save operation is called with the correct data'
57 1 * mockCpsDataService.saveListElements(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_ANCHOR_NAME,
58 SUBSCRIPTION_REGISTRY_PARENT,
59 '{"subscription":[{' +
60 '"topic":"some-topic",' +
61 '"predicates":{"datastore":"some-datastore","targetCmHandles":[{"cmHandleId":"cmhandle1","status":"PENDING"},{"cmHandleId":"cmhandle2","status":"PENDING"}]},' +
62 '"clientID":"some-client-id","subscriptionName":"some-subscription-name","isTagged":true}]}',
66 def 'update a subscription event' () {
67 given: 'a data node exist in db'
68 def childDataNode = new DataNodeBuilder().withDataspace('NCMP-Admin')
69 .withAnchor('AVC-Subscriptions').withXpath('/subscription-registry/subscription').build()
70 def engagedDataNode = new DataNodeBuilder().withDataspace('NCMP-Admin')
71 .withAnchor('AVC-Subscriptions').withXpath('/subscription-registry')
72 .withChildDataNodes([childDataNode]).build()
73 and: 'cps data service return existing data node'
74 mockCpsDataService.getDataNodes(*_) >> [engagedDataNode]
75 when: 'the yangModelSubscriptionEvent is saved into db'
76 objectUnderTest.saveSubscriptionEvent(yangModelSubscriptionEvent)
77 then: 'the cpsDataService update operation is called with the correct data'
78 1 * mockCpsDataService.updateDataNodeAndDescendants(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_ANCHOR_NAME,
79 SUBSCRIPTION_REGISTRY_PARENT,
80 '{"subscription":[{' +
81 '"topic":"some-topic",' +
82 '"predicates":{"datastore":"some-datastore","targetCmHandles":[{"cmHandleId":"cmhandle1","status":"PENDING"},{"cmHandleId":"cmhandle2","status":"PENDING"}]},' +
83 '"clientID":"some-client-id","subscriptionName":"some-subscription-name","isTagged":true}]}',