aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/groovy/org/onap/cps/temporal/service/NetworkDataServiceImplSpec.groovy
blob: 2e04ca8703358052cc3be385283ebe686ca3c815 (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
/*
 * ============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.service

import org.onap.cps.temporal.domain.NetworkDataId
import org.onap.cps.temporal.domain.SearchCriteria
import org.spockframework.spring.SpringBean
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.data.domain.PageImpl
import org.springframework.test.context.ContextConfiguration

import javax.validation.ValidationException
import java.time.OffsetDateTime
import org.onap.cps.temporal.domain.NetworkData
import org.onap.cps.temporal.repository.NetworkDataRepository
import spock.lang.Specification

/**
 * Test specification for network data service.
 */
@SpringBootTest
@ContextConfiguration(classes = NetworkDataServiceImpl)
class NetworkDataServiceImplSpec extends Specification {

    @SpringBean
    NetworkDataRepository mockNetworkDataRepository = Mock()

    @Autowired
    NetworkDataService objectUnderTest

    @Value('${app.query.response.max-page-size}')
    int maxPageSize

    def networkData = new NetworkData()

    def 'Add network data successfully.'() {
        given: 'network data repository is persisting network data it is asked to save'
            def persistedNetworkData = new NetworkData()
            persistedNetworkData.setCreatedTimestamp(OffsetDateTime.now())
            mockNetworkDataRepository.save(networkData) >> persistedNetworkData
        when: 'a new network data is added'
            def result = objectUnderTest.addNetworkData(networkData)
        then: 'result network data is the one that has been persisted'
            result == persistedNetworkData
            result.getCreatedTimestamp() != null
            networkData.getCreatedTimestamp() == null
    }

    def 'Add network data fails because already added'() {
        given:
            'network data repository is not able to create data it is asked to persist ' +
                'and reveals it with null created timestamp on network data entity'
            def persistedNetworkData = new NetworkData()
            persistedNetworkData.setCreatedTimestamp(null)
            mockNetworkDataRepository.save(networkData) >> persistedNetworkData
        and: 'existing data can be retrieved'
            def existing = new NetworkData()
            existing.setCreatedTimestamp(OffsetDateTime.now().minusYears(1))
            mockNetworkDataRepository.findById(_ as NetworkDataId) >> Optional.of(existing)
        when: 'a new network data is added'
            objectUnderTest.addNetworkData(networkData)
        then: 'network service exception is thrown'
            thrown(ServiceException)
    }

    def 'Query network data by search criteria.'() {
        given: 'search criteria'
            def searchCriteria = SearchCriteria.builder()
                .dataspaceName('my-dataspaceName')
                .schemaSetName('my-schemaset')
                .pagination(0, 10)
                .build()
        and: 'response from repository'
            def pageFromRepository = new PageImpl<>(Collections.emptyList(), searchCriteria.getPageable(), 10)
            mockNetworkDataRepository.findBySearchCriteria(searchCriteria) >> pageFromRepository

        when: 'search is executed'
            def resultPage = objectUnderTest.searchNetworkData(searchCriteria)

        then: 'data is fetched from repository and returned'
            resultPage == pageFromRepository

    }

    def 'Query network data with more than max page-size'() {
        given: 'search criteria with more than max page size'
            def searchCriteria = SearchCriteria.builder()
                .dataspaceName('my-dataspaceName')
                .schemaSetName('my-schemaset')
                .pagination(0, maxPageSize + 1)
                .build()
        when: 'search is executed'
            objectUnderTest.searchNetworkData(searchCriteria)

        then: 'throws error'
            thrown(ValidationException)

    }

}