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.event
21 import com.fasterxml.jackson.databind.ObjectMapper
22 import com.fasterxml.jackson.databind.node.ObjectNode
23 import org.onap.cps.event.model.Content
24 import org.onap.cps.event.model.CpsDataUpdatedEvent
25 import org.onap.cps.event.model.Data
26 import spock.lang.Specification
29 * Test class for CpsDataUpdatedEvent.
31 class CpsDataUpdatedEventSpec extends Specification {
33 def objectMapper = new ObjectMapper()
35 final DATASPACE_NAME = 'my-dataspace'
36 final BOOKSTORE_SCHEMA_SET = 'bookstore-schemaset'
37 final ANCHOR_NAME = 'chapters'
38 final EVENT_TIMESTAMP = '2020-12-01T00:00:00.000+0000'
39 final EVENT_ID = '77b8f114-4562-4069-8234-6d059ff742ac'
40 final EVENT_SOURCE = new URI('urn:cps:org.onap.cps')
41 final EVENT_TYPE = 'org.onap.cps.data-updated-event'
42 final EVENT_SCHEMA = new URI('urn:cps:org.onap.cps:data-updated-event-schema:v1')
46 'bookstore-name': 'Chapters',
51 ['authors' : ['Iain M. Banks'],
55 'title' : 'Feersum Endjinn'
63 def 'Conversion from JSON String to CpsDataUpdatedEvent POJO.'() {
64 when: 'event JSON String is converted to CpsDataUpdatedEvent'
65 def notificationMessage = getEventAsJsonStringFromFile()
66 def cpsDataUpdatedEvent = objectMapper.readValue(notificationMessage, CpsDataUpdatedEvent.class)
67 then: 'CpsDataUpdatedEvent POJO has the excepted values'
68 cpsDataUpdatedEvent.id == EVENT_ID
69 cpsDataUpdatedEvent.source == EVENT_SOURCE
70 cpsDataUpdatedEvent.schema == EVENT_SCHEMA
71 cpsDataUpdatedEvent.type == EVENT_TYPE
72 def content = cpsDataUpdatedEvent.content
73 content.observedTimestamp == EVENT_TIMESTAMP
74 content.dataspaceName == DATASPACE_NAME
75 content.schemaSetName == BOOKSTORE_SCHEMA_SET
76 content.anchorName == ANCHOR_NAME
77 content.data.getAdditionalProperties() == DATA
80 def 'Conversion CpsDataUpdatedEvent POJO to JSON String.'() {
81 given: 'Event content with the Data'
83 data.withAdditionalProperty('test:bookstore', DATA.'test:bookstore')
84 def content = new Content()
85 content.withAnchorName(ANCHOR_NAME)
86 .withDataspaceName(DATASPACE_NAME)
87 .withSchemaSetName(BOOKSTORE_SCHEMA_SET)
88 .withObservedTimestamp(EVENT_TIMESTAMP)
90 and: 'CpsDataUpdatedEvent with the content'
91 def cpsDataUpdateEvent = new CpsDataUpdatedEvent()
93 .withSchema(EVENT_SCHEMA)
95 .withSource(EVENT_SOURCE)
98 when: 'CpsDataUpdatedEvent is converted to JSON string'
99 def actualMessage = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(cpsDataUpdateEvent)
100 then: 'the created JSON String is same as the expected JSON String'
101 def expectedMessage = getEventAsJsonStringFromFile()
102 assert actualMessage == expectedMessage
105 def getEventAsJsonStringFromFile() {
106 return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(
107 objectMapper.readValue(
108 this.class.getResource('/bookstore-chapters.json').getText('UTF-8'),