Merge "CM SUBSCRIPTION: add new subscription for non existing xpath"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / DataJobServiceImplSpec.groovy
1 /*
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
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.api.impl
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 org.slf4j.LoggerFactory
28 import org.onap.cps.ncmp.api.models.datajob.DataJobReadRequest
29 import org.onap.cps.ncmp.api.models.datajob.DataJobWriteRequest
30 import org.onap.cps.ncmp.api.models.datajob.DataJobMetadata
31 import org.onap.cps.ncmp.api.models.datajob.ReadOperation
32 import org.onap.cps.ncmp.api.models.datajob.WriteOperation
33 import spock.lang.Specification
34
35 class DataJobServiceImplSpec extends Specification{
36
37     def objectUnderTest = new DataJobServiceImpl()
38
39     def logger = Spy(ListAppender<ILoggingEvent>)
40
41     def setup() {
42         setupLogger()
43     }
44
45     def cleanup() {
46         ((Logger) LoggerFactory.getLogger(DataJobServiceImpl.class)).detachAndStopAllAppenders()
47     }
48
49     def '#operation data job request.'() {
50         given: 'data job metadata'
51             def dataJobMetadata = new DataJobMetadata('client-topic', 'application/vnd.3gpp.object-tree-hierarchical+json', 'application/3gpp-json-patch+json')
52         when: 'read/write data job request is processed'
53             if (operation == 'read') {
54                 objectUnderTest.readDataJob('some-job-id', dataJobMetadata, new DataJobReadRequest([getWriteOrReadOperationRequest(operation)]))
55             } else {
56                 objectUnderTest.writeDataJob('some-job-id', dataJobMetadata, new DataJobWriteRequest([getWriteOrReadOperationRequest(operation)]))
57             }
58         then: 'the data job id is correctly logged'
59             def loggingEvent = logger.list[0]
60             assert loggingEvent.level == Level.INFO
61             assert loggingEvent.formattedMessage.contains('data job id for ' + operation + ' operation is: some-job-id')
62         where: 'the following data job operations are used'
63             operation << ['read', 'write']
64     }
65
66     def getWriteOrReadOperationRequest(operation) {
67         if (operation == 'write') {
68             return new WriteOperation('some/write/path', 'add', 'some-operation-id', 'some-value')
69         }
70         return new ReadOperation('some/read/path', 'read', 'some-operation-id', ['some-attrib-1'], ['some-field-1'], 'some-filter', 'some-scope-type', 1)
71     }
72
73     def setupLogger() {
74         def setupLogger = ((Logger) LoggerFactory.getLogger(DataJobServiceImpl.class))
75         setupLogger.setLevel(Level.DEBUG)
76         setupLogger.addAppender(logger)
77         logger.start()
78     }
79 }