ad201c6e521c8fab05268d246783e4a7d279cfb3
[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.onap.cps.utils.JsonObjectMapper
32 import org.slf4j.LoggerFactory
33 import org.springframework.beans.factory.annotation.Autowired
34 import org.springframework.boot.test.context.SpringBootTest
35 import spock.lang.Specification
36
37 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
38 class NcmpInEventConsumerSpec extends Specification {
39
40     def mockCmSubscriptionHandler = Mock(CmSubscriptionHandlerImpl)
41     def objectUnderTest = new NcmpInEventConsumer(mockCmSubscriptionHandler)
42     def logger = new ListAppender<ILoggingEvent>()
43
44     @Autowired
45     JsonObjectMapper jsonObjectMapper
46
47     @Autowired
48     ObjectMapper objectMapper
49
50     void setup() {
51         ((Logger) LoggerFactory.getLogger(NcmpInEventConsumer.class)).addAppender(logger)
52         logger.start()
53     }
54
55     void cleanup() {
56         ((Logger) LoggerFactory.getLogger(NcmpInEventConsumer.class)).detachAndStopAllAppenders()
57     }
58
59     def 'Consuming CREATE cm data job subscription request.'() {
60         given: 'a JSON file for create event'
61             def jsonData = TestUtils.getResourceFileContent(
62                 'datajobs/subscription/cmNotificationSubscriptionNcmpInEvent.json')
63             def myEventType = "dataJobCreated"
64             jsonData = jsonData.replace('#myEventType', myEventType)
65         and: 'the event'
66             def event = objectMapper.readValue(jsonData, DataJobSubscriptionOperationInEvent)
67         and: 'the list of data node selectors'
68             def dataNodeSelectorList = getDataNodeSelectorsAsXpaths(event)
69         and: 'the other data job event attributes'
70             def dataSelector = getDataSelector(event)
71         when: 'the event is consumed'
72             objectUnderTest.consumeSubscriptionEvent(event)
73         then: 'event details are logged at level INFO'
74             def loggingEvent = logger.list.last()
75             assert loggingEvent.level == Level.INFO
76             assert loggingEvent.formattedMessage.contains('dataJobId=myDataJobId')
77             assert loggingEvent.formattedMessage.contains("eventType=${myEventType}")
78         and: 'method to handle process subscription create request is called'
79             1 * mockCmSubscriptionHandler.processSubscriptionCreate(dataSelector, "myDataJobId", dataNodeSelectorList)
80     }
81
82     def getDataNodeSelectorsAsXpaths(event) {
83         return JexParser.toXpaths(event.event.dataJob.productionJobDefinition.targetSelector.dataNodeSelector)
84     }
85
86     def getDataSelector(event) {
87         return event.event.dataJob.productionJobDefinition.dataSelector
88     }
89 }