191472ceea6bedaa768884a24881bfe83f057275
[cps.git] / cps-service / src / test / groovy / org / onap / cps / utils / CpsValidatorSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 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.utils
22
23 import org.onap.cps.spi.exceptions.DataValidationException
24 import spock.lang.Specification
25
26 class CpsValidatorSpec extends Specification {
27
28
29     def 'Validating a valid string.'() {
30         when: 'the string is validated using a valid name'
31             CpsValidator.validateNameCharacters('name-with-no-spaces')
32         then: 'no exception is thrown'
33             noExceptionThrown()
34     }
35
36     def 'Validating an invalid string.'() {
37         when: 'the string is validated using an invalid name'
38             CpsValidator.validateNameCharacters(name)
39         then: 'a data validation exception is thrown'
40             def exceptionThrown = thrown(DataValidationException)
41         and: 'the error was encountered at the following index in #scenario'
42             assert exceptionThrown.getDetails().contains(expectedErrorMessage)
43         where: 'the following names are used'
44             scenario     | name               || expectedErrorMessage
45             'position 5' | 'name with spaces' || 'name with spaces invalid token encountered at position 5'
46             'position 9' | 'nameWith Space'   || 'nameWith Space invalid token encountered at position 9'
47     }
48 }