6230fb50051baad7bab70075b4c13ff75d9d8ab2
[cps.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (c) 2024-2025 OpenInfra Foundation Europe. All rights reserved.
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.impl.datajobs.subscription.ncmp
22
23 import ch.qos.logback.classic.Level
24 import ch.qos.logback.classic.Logger
25 import ch.qos.logback.classic.spi.ILoggingEvent
26 import ch.qos.logback.core.read.ListAppender
27 import com.fasterxml.jackson.databind.ObjectMapper
28 import org.onap.cps.ncmp.impl.datajobs.subscription.client_to_ncmp.DataJobSubscriptionOperationInEvent
29 import org.onap.cps.ncmp.impl.utils.JexParser
30 import org.onap.cps.ncmp.utils.TestUtils
31 import org.slf4j.LoggerFactory
32 import spock.lang.Specification
33
34 class NcmpInEventConsumerSpec extends Specification {
35
36     def logger = new ListAppender<ILoggingEvent>()
37     def objectMapper = new ObjectMapper()
38
39     def mockCmSubscriptionHandler = Mock(CmSubscriptionHandlerImpl)
40     def objectUnderTest = new NcmpInEventConsumer(mockCmSubscriptionHandler)
41
42     void setup() {
43         ((Logger) LoggerFactory.getLogger(NcmpInEventConsumer.class)).addAppender(logger)
44         logger.start()
45     }
46
47     void cleanup() {
48         ((Logger) LoggerFactory.getLogger(NcmpInEventConsumer.class)).detachAndStopAllAppenders()
49     }
50
51     def 'Consuming CREATE cm data job subscription request.'() {
52         given: 'a JSON file for create event'
53             def jsonData = TestUtils.getResourceFileContent(
54                 'datajobs/subscription/cmNotificationSubscriptionNcmpInEvent.json')
55             def myEventType = "dataJobCreated"
56             jsonData = jsonData.replace('#myEventType', myEventType)
57         and: 'the event'
58             def event = objectMapper.readValue(jsonData, DataJobSubscriptionOperationInEvent)
59         and: 'the list of data node selectors'
60             def dataNodeSelectorList = getDataNodeSelectorsAsXpaths(event)
61         and: 'the other data job event attributes'
62             def dataSelector = getDataSelector(event)
63         when: 'the event is consumed'
64             objectUnderTest.consumeSubscriptionEvent(event)
65         then: 'event details are logged at level INFO'
66             def loggingEvent = logger.list.last()
67             assert loggingEvent.level == Level.INFO
68             assert loggingEvent.formattedMessage.contains('dataJobId=myDataJobId')
69             assert loggingEvent.formattedMessage.contains("eventType=${myEventType}")
70         and: 'method to handle process subscription create request is called'
71             1 * mockCmSubscriptionHandler.processSubscriptionCreate(dataSelector, "myDataJobId", dataNodeSelectorList)
72     }
73
74     def getDataNodeSelectorsAsXpaths(event) {
75         return JexParser.toXpaths(event.event.dataJob.productionJobDefinition.targetSelector.dataNodeSelector)
76     }
77
78     def getDataSelector(event) {
79         return event.event.dataJob.productionJobDefinition.dataSelector
80     }
81 }