aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/groovy/org/onap/cps/temporal/domain/SearchCriteriaSpec.groovy
blob: d7b6d1f31f69e69abe2cd902a371680b8ccdebc6 (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
132
/*
 * ============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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */
package org.onap.cps.temporal.domain

import org.springframework.data.domain.Sort
import spock.lang.Specification

import java.time.OffsetDateTime

class SearchCriteriaSpec extends Specification {

    def myDataspace = 'my-dataspace'
    def myAnchorName = 'my-anchor'
    def myschemaSetName = 'my-schemaset'


    def 'Search Criteria has default values if not provided.'() {
        def myPayloadFilter = '{"status": "down"}'
        when: 'search criteria is created'
            def searchCriteria = SearchCriteria.builder()
                .dataspaceName(myDataspace)
                .schemaSetName(myschemaSetName)
                .pagination(0, 10)
                .simplePayloadFilter(myPayloadFilter)
                .build()

        then: 'search criteria has default value for sort'
            searchCriteria.getPageable().getSort() == Sort.by(Sort.Direction.DESC, 'observed_timestamp')
        and: 'created before has almost current time as default value'
            OffsetDateTime.now().minusMinutes(5).isBefore(searchCriteria.getCreatedBefore())
        and: 'contains the provided value to builder'
            searchCriteria.getDataspaceName() == myDataspace
            searchCriteria.getSchemaSetName() == myschemaSetName
            searchCriteria.getSimplePayloadFilter() == myPayloadFilter
            searchCriteria.getPageable().getPageNumber() == 0
            searchCriteria.getPageable().getPageSize() == 10

    }

    def 'Search Criteria with the provided values.'() {

        given: 'sort by parameter'
            def sortBy = Sort.by(Sort.Direction.ASC, 'observed_timestamp')
        and: 'data created one day ago'
            def lastDayAsCreatedBefore = OffsetDateTime.now().minusDays(1)
        and: 'observed timestamp'
            def nowAsObservedAfter = OffsetDateTime.now()

        when: 'search criteria is created'
            def searchCriteria = SearchCriteria.builder()
                .dataspaceName(myDataspace)
                .schemaSetName(myschemaSetName)
                .anchorName(myAnchorName)
                .pagination(0, 10)
                .sort(sortBy)
                .observedAfter(nowAsObservedAfter)
                .createdBefore(lastDayAsCreatedBefore)
                .build()

        then: 'search criteria has expected value'
            with(searchCriteria) {
                dataspaceName == myDataspace
                schemaSetName == myschemaSetName
                anchorName == myAnchorName
                observedAfter == nowAsObservedAfter
                createdBefore == lastDayAsCreatedBefore
                pageable.getPageNumber() == 0
                pageable.getPageSize() == 10
                pageable.getSort() == sortBy
            }
    }

    def 'Error handling: missing dataspace.'() {
        when: 'search criteria is created without dataspace'
            SearchCriteria.builder()
                .anchorName(myAnchorName)
                .pagination(0, 10)
                .build()
        then: 'exception is thrown'
            thrown(IllegalStateException)
    }

    def 'Error handling: missing both schemaset and anchor.'() {
        when: 'search criteria is created without schemaset and anchor'
            SearchCriteria.builder()
                .dataspaceName(myDataspace)
                .pagination(0, 10)
                .build()
        then: 'exception is thrown'
            thrown(IllegalStateException)
    }

    def 'Error handling: missing pagination.'() {
        when: 'search criteria is created without pagination'
            SearchCriteria.builder()
                .dataspaceName(myDataspace)
                .anchorName(myAnchorName)
                .build()
        then: 'exception is thrown'
            thrown(IllegalStateException)
    }

    def 'Error Handling: sort must be not null.'() {
        when: 'search criteria is created without sorting information'
            SearchCriteria.builder()
                .dataspaceName(myDataspace)
                .anchorName(myAnchorName)
                .pagination(0, 1)
                .sort(null)
                .build()
        then: 'exception is thrown'
            thrown(IllegalArgumentException)
    }

}