aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/groovy/org/onap/cps/temporal/controller/event/model/CpsDataUpdatedEventMapperSpec.groovy
blob: a51c4fe0a288b80bab46f47aef174861373a8d6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * ============LICENSE_START=======================================================
 * Copyright (c) 2021 Bell Canada.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.cps.temporal.controller.event.model

import com.fasterxml.jackson.core.JsonProcessingException
import org.mapstruct.factory.Mappers
import org.onap.cps.event.model.Content
import org.onap.cps.event.model.CpsDataUpdatedEvent
import org.onap.cps.event.model.Data
import org.onap.cps.temporal.domain.NetworkData
import spock.lang.Specification

import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter

/**
 * Test specification for data updated event mapper.
 */
class CpsDataUpdatedEventMapperSpec extends Specification {

    def objectUnderTest = Mappers.getMapper(CpsDataUpdatedEventMapper.class);

    def 'Mapping a null event'() {
        given: 'a null event'
            def event = null
        when: 'the event is mapped to an entity'
            NetworkData result = objectUnderTest.eventToEntity(event)
        then: 'the result entity is null'
            result == null
    }

    def 'Mapping an event whose properties are null'() {
        given: 'an event whose properties are null'
            def event = new CpsDataUpdatedEvent()
        when: 'the event is mapped to an entity'
            NetworkData result = objectUnderTest.eventToEntity(event)
        then: 'the result entity is not null'
            result != null
        and: 'all result entity properties are null'
            assertEntityPropertiesAreNull(result)
    }

    def 'Mapping an event whose content properties are null'() {
        given: 'an event whose content properties are null'
            def event = new CpsDataUpdatedEvent().withContent(new Content())
        when: 'the event is mapped to an entity'
            NetworkData result = objectUnderTest.eventToEntity(event)
        then: 'the result entity is not null'
            result != null
        and: 'all result entity properties are null'
            assertEntityPropertiesAreNull(result)
    }

    def 'Mapping an event whose content data is empty'() {
        given: 'an event whose content data is empty'
            def event = new CpsDataUpdatedEvent().withContent(new Content().withData(new Data()))
        when: 'the event is mapped to an entity'
            NetworkData result = objectUnderTest.eventToEntity(event)
        then: 'the result entity is not null'
            result != null
        and: 'the result entity payload is an empty json '
            result.getPayload() == "{}"
    }

    def 'Mapping an event whose content data is invalid'() {
        given: 'an event whose content data is invalid'
            def event =
                    new CpsDataUpdatedEvent().withContent(new Content().withData(
                            new Data().withAdditionalProperty(null, null)))
        when: 'the event is mapped to an entity'
            NetworkData result = objectUnderTest.eventToEntity(event)
        then: 'an runtime exception is thrown'
            def e = thrown(RuntimeException)
            e.getCause() instanceof JsonProcessingException
    }

    def 'Mapping a valid complete event'() {
        given: 'a valid complete event'
            def isoTimestampFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
            def aDataName = 'a-data-name'
            def aDataValue = 'a-data-value'
            def event =
                    new CpsDataUpdatedEvent()
                            .withContent(
                                    new Content()
                                            .withObservedTimestamp(isoTimestampFormatter.format(OffsetDateTime.now()))
                                            .withDataspaceName('a-dataspace')
                                            .withSchemaSetName('a-schema-set')
                                            .withAnchorName('an-anchor')
                                            .withData(new Data().withAdditionalProperty(aDataName, aDataValue)))
        when: 'the event is mapped to an entity'
            NetworkData result = objectUnderTest.eventToEntity(event)
        then: 'the result entity is not null'
            result != null
        and: 'all result entity properties are the ones from the event'
            result.getObservedTimestamp() ==
                OffsetDateTime.parse(event.getContent().getObservedTimestamp(), isoTimestampFormatter)
            result.getDataspace() == event.getContent().getDataspaceName()
            result.getSchemaSet() == event.getContent().getSchemaSetName()
            result.getAnchor() == event.getContent().getAnchorName()
            result.getPayload().contains(aDataValue)
            result.getPayload().contains(aDataValue)
            result.getCreatedTimestamp() == null
    }

    private void assertEntityPropertiesAreNull(NetworkData networkData) {
        assert networkData.getObservedTimestamp() == null
        assert networkData.getDataspace() == null
        assert networkData.getSchemaSet() == null
        assert networkData.getAnchor() == null
        assert networkData.getPayload() == null
        assert networkData.getCreatedTimestamp() == null
    }

}