2 * ============LICENSE_START=======================================================
3 * Copyright (c) 2021 Bell Canada.
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.
16 * ============LICENSE_END=========================================================
19 package org.onap.cps.temporal.controller.event.listener.kafka
21 import org.mapstruct.factory.Mappers
22 import org.onap.cps.event.model.CpsDataUpdatedEvent
23 import org.onap.cps.temporal.controller.event.listener.exception.InvalidEventEnvelopException
24 import org.onap.cps.temporal.controller.event.model.CpsDataUpdatedEventMapper
25 import org.onap.cps.temporal.service.NetworkDataService
26 import spock.lang.Specification
28 import static org.onap.cps.temporal.controller.event.listener.exception.InvalidEventEnvelopException.InvalidField.ErrorType.MISSING
29 import static org.onap.cps.temporal.controller.event.listener.exception.InvalidEventEnvelopException.InvalidField.ErrorType.UNEXPECTED
32 * Test specification for data updated event listener.
34 class DataUpdatedEventListenerSpec extends Specification {
37 def anEventType = 'my-event-type'
38 def anEventSource = new URI('my-event-source')
39 def aTimestamp = EventFixtures.currentIsoTimestamp()
40 def aDataspace = 'my-dataspace'
41 def aSchemaSet = 'my-schema-set'
42 def anAnchor = 'my-anchor'
43 def aDataName = 'my-data-name'
44 def aDataValue = 'my-data-value'
46 // Define service mock
47 def mockService = Mock(NetworkDataService)
50 def mapper = Mappers.getMapper(CpsDataUpdatedEventMapper.class)
52 // Define listener under test
53 def objectUnderTest = new DataUpdatedEventListener(mockService, mapper)
55 def 'Event message consumption'() {
56 when: 'an event is received'
58 EventFixtures.buildEvent(
59 timestamp: aTimestamp, dataspace: aDataspace, schemaSet: aSchemaSet, anchor: anAnchor,
60 dataName: aDataName, dataValue: aDataValue)
61 objectUnderTest.consume(event)
62 then: 'network data service is requested to persisted the data change'
63 1 * mockService.addNetworkData(
65 it.getObservedTimestamp() == EventFixtures.toOffsetDateTime(aTimestamp)
66 && it.getDataspace() == aDataspace
67 && it.getSchemaSet() == aSchemaSet
68 && it.getAnchor() == anAnchor
69 && it.getPayload() == String.format('{"%s":"%s"}', aDataName, aDataValue)
70 && it.getCreatedTimestamp() == null
75 def 'Event message consumption fails because of missing envelop'() {
76 when: 'an event without envelop information is received'
77 def invalidEvent = new CpsDataUpdatedEvent().withSchema(null)
78 objectUnderTest.consume(invalidEvent)
79 then: 'an exception is thrown with 4 invalid fields'
80 def e = thrown(InvalidEventEnvelopException)
81 e.getCpsDataUpdatedEvent() == invalidEvent
82 e.getInvalidFields().size() == 4
83 e.getInvalidFields().contains(
84 new InvalidEventEnvelopException.InvalidField(
85 MISSING,"schema", null,
86 CpsDataUpdatedEvent.Schema.URN_CPS_ORG_ONAP_CPS_DATA_UPDATED_EVENT_SCHEMA_1_1_0_SNAPSHOT
88 e.getInvalidFields().contains(
89 new InvalidEventEnvelopException.InvalidField(
90 MISSING, "id", null, null))
91 e.getInvalidFields().contains(
92 new InvalidEventEnvelopException.InvalidField(
93 UNEXPECTED, "source", null, EventFixtures.defaultEventSource.toString()))
94 e.getInvalidFields().contains(
95 new InvalidEventEnvelopException.InvalidField(
96 UNEXPECTED, "type", null, EventFixtures.defaultEventType))
97 e.getMessage().contains(e.getInvalidFields().toString())
100 def 'Event message consumption fails because of invalid envelop'() {
101 when: 'an event with an invalid envelop is received'
103 new CpsDataUpdatedEvent()
104 .withId('my-id').withSource(anEventSource).withType(anEventType)
105 objectUnderTest.consume(invalidEvent)
106 then: 'an exception is thrown with 2 invalid fields'
107 def e = thrown(InvalidEventEnvelopException)
108 e.getCpsDataUpdatedEvent() == invalidEvent
109 e.getInvalidFields().size() == 2
110 e.getInvalidFields().contains(
111 new InvalidEventEnvelopException.InvalidField(
112 UNEXPECTED, "type", anEventType, EventFixtures.defaultEventType))
113 e.getInvalidFields().contains(
114 new InvalidEventEnvelopException.InvalidField(
115 UNEXPECTED, "source", anEventSource.toString(),
116 EventFixtures.defaultEventSource.toString()))