Support pagination in query across all anchors(ep4)
[cps.git] / cps-ri / src / test / groovy / org / onap / cps / spi / impl / utils / CpsValidatorSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2023 Nordix Foundation
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.spi.impl.utils
22
23 import org.onap.cps.spi.PaginationOption
24 import org.onap.cps.spi.exceptions.DataValidationException
25 import spock.lang.Specification
26
27 class CpsValidatorSpec extends Specification {
28
29     def objectUnderTest = new CpsValidatorImpl()
30
31     def 'Validating a valid string.'() {
32         when: 'the string is validated using a valid name'
33             objectUnderTest.validateNameCharacters('name-with-no-spaces')
34         then: 'no exception is thrown'
35             noExceptionThrown()
36     }
37
38     def 'Validating an invalid string.'() {
39         when: 'the string is validated using an invalid name'
40             objectUnderTest.validateNameCharacters(name)
41         then: 'a data validation exception is thrown'
42             def exceptionThrown = thrown(DataValidationException)
43         and: 'the error was encountered at the following index in #scenario'
44             assert exceptionThrown.getDetails().contains(expectedErrorMessage)
45         where: 'the following names are used'
46             scenario     | name               || expectedErrorMessage
47             'position 5' | 'name with spaces' || 'name with spaces invalid token encountered at position 5'
48             'position 9' | 'nameWith Space'   || 'nameWith Space invalid token encountered at position 9'
49     }
50
51     def 'Validating a list of valid names.'() {
52         given: 'a list of valid names'
53             def names = ['valid-name', 'another-valid-name']
54         when: 'a list of strings is validated'
55             objectUnderTest.validateNameCharacters(names)
56         then: 'no exception is thrown'
57             noExceptionThrown()
58     }
59
60     def 'Validating a list of names with invalid names.'() {
61         given: 'a list of names with an invalid name'
62             def names = ['valid-name', 'name with spaces']
63         when: 'a list of strings is validated'
64             objectUnderTest.validateNameCharacters(names)
65         then: 'a data validation exception is thrown'
66             thrown(DataValidationException)
67     }
68
69     def 'Validate Pagination option with invalid page index and size.'() {
70         when: 'the pagination option is validated using invalid options'
71             objectUnderTest.validatePaginationOption(new PaginationOption(-5, -2))
72         then: 'a data validation exception is thrown'
73             def exceptionThrown = thrown(DataValidationException)
74         and: 'the error was encountered at the following index in #scenario'
75             assert exceptionThrown.getDetails().contains("Invalid page index or size")
76     }
77 }