aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/groovy/org/onap/cps/temporal/controller/event/listener/kafka/EventFixtures.groovy
blob: 44a28de3a3b5e92bf0df8b794ebe5589fff08915 (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
/*
 * ============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.listener.kafka

import org.onap.cps.event.model.Content
import org.onap.cps.event.model.CpsDataUpdatedEvent
import org.onap.cps.event.model.Data

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

/**
 * This class contains utility fixtures methods for building and manipulating event data.
 */
class EventFixtures {

    static DateTimeFormatter isoTimestampFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
    static String defaultEventType = 'org.onap.cps.data-updated-event'
    static URI defaultEventSource = new URI('urn:cps:org.onap.cps')

    static CpsDataUpdatedEvent buildEvent(final Map map) {
        CpsDataUpdatedEvent event =
                new CpsDataUpdatedEvent()
                        .withId(
                                map.id != null ? map.id.toString() : UUID.randomUUID().toString())
                        .withType(
                                map.eventType != null ? map.eventType.toString() : defaultEventType)
                        .withSource(
                                map.eventSource != null ? new URI(map.eventSource.toString()) : defaultEventSource)
                        .withContent(
                                new Content()
                                        .withObservedTimestamp(
                                                map.timestamp != null ? map.timestamp.toString() : currentTimestamp())
                                        .withDataspaceName(
                                                map.dataspace != null ? map.dataspace.toString() : 'a-dataspace')
                                        .withSchemaSetName(
                                                map.schemaSet != null ? map.schemaSet.toString() : 'a-schema-set')
                                        .withAnchorName(
                                                map.anchor != null ? map.anchor.toString() : 'an-anchor')
                                        .withData(
                                                new Data().withAdditionalProperty(
                                                        map.dataName != null ? map.dataName.toString() : 'a-data-name',
                                                        map.dataValue != null ? map.dataValue : 'a-data-value')))

        return event
    }

    static String currentIsoTimestamp() {
        return isoTimestampFormatter.format(OffsetDateTime.now())
    }

    static OffsetDateTime toOffsetDateTime(String timestamp) {
        return OffsetDateTime.parse(timestamp, isoTimestampFormatter)
    }

}