[OOM cert-service-client] Add validation of email, ip and domain name
[oom/platform/cert-service.git] / certServiceClient / src / test / java / org / onap / oom / certservice / client / configuration / validation / csr / CsrEnvsValueValidatorsTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * oom-certservice-client
4  * ================================================================================
5  * Copyright (C) 2020 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.oom.certservice.client.configuration.validation.csr;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.onap.oom.certservice.client.configuration.validation.client.ClientEnvsValueValidators.isPathValid;
25 import static org.onap.oom.certservice.client.configuration.validation.csr.CsrEnvsValueValidators.isCountryValid;
26 import static org.onap.oom.certservice.client.configuration.validation.csr.CsrEnvsValueValidators.isDomainNameValid;
27 import static org.onap.oom.certservice.client.configuration.validation.csr.CsrEnvsValueValidators.isEmailAddressValid;
28 import static org.onap.oom.certservice.client.configuration.validation.csr.CsrEnvsValueValidators.isIpAddressValid;
29 import static org.onap.oom.certservice.client.configuration.validation.csr.CsrEnvsValueValidators.isSpecialCharPresent;
30 import static org.onap.oom.certservice.client.configuration.validation.csr.CsrEnvsValueValidators.isUriValid;
31
32 import org.junit.jupiter.params.ParameterizedTest;
33 import org.junit.jupiter.params.provider.ValueSource;
34
35 class CsrEnvsValueValidatorsTest {
36
37     @ParameterizedTest
38     @ValueSource(strings = {"/var/log", "/", "/var/log/", "/second_var", "/second-var"})
39     void shouldAcceptValidPath(String path) {
40         assertThat(isPathValid(path)).isTrue();
41     }
42
43     @ParameterizedTest
44     @ValueSource(strings = {"/var/log?", "", "var_", "var", "//", "/var//log"})
45     void shouldRejectInvalidPath(String path) {
46         assertThat(isPathValid(path)).isFalse();
47     }
48
49     @ParameterizedTest
50     @ValueSource(strings = {"PL", "DE", "PN", "US", "IO", "CA", "KH", "CO", "DK", "EC", "CZ", "CN", "BR", "BD", "BE"})
51     void shouldAcceptValidCountryCode(String countryCode) {
52         assertThat(isCountryValid(countryCode)).isTrue();
53     }
54
55     @ParameterizedTest
56     @ValueSource(strings = {"", "QQ", "AFG", "D", "&*", "!", "ONAP", "p", "pl", "us", "afg"})
57     void shouldRejectInvalidCountryCode(String countryCode) {
58         assertThat(isCountryValid(countryCode)).isFalse();
59     }
60
61     @ParameterizedTest
62     @ValueSource(strings = {"sample@example.com", "onap@domain.pl", "alex.supertramp@onap.com",
63         "al.super^tramp@onap.org"})
64     void shouldAcceptValidEmailAddr(String emailAddr) {
65         assertThat(isEmailAddressValid(emailAddr)).isTrue();
66     }
67
68     @ParameterizedTest
69     @ValueSource(strings = {"<sample@example.com>", "onap@domain", "(mailto)user@onap.com", "mailto:axe@axe.de",
70         "incoreectdomaim@onap.ux"})
71     void shouldRejectInvalidEmailAddr(String address) {
72         assertThat(isEmailAddressValid(address)).isFalse();
73     }
74
75     @ParameterizedTest
76     @ValueSource(strings = {"192.168.0.1", "10.183.34.201", "ff:ff:ff:ff::", "ff:ff:ff:ff:ff:ff:ff:ff"})
77     void shouldAcceptValidIpAddress(String address) {
78         assertThat(isIpAddressValid(address)).isTrue();
79     }
80
81     @ParameterizedTest
82     @ValueSource(strings = {"192.168.0.", "ff:ff:ee:a1:", "fg:ff:ff:ff::", "http://10.183.34.201",
83         "10.183.34.201:8080"})
84     void shouldRejectInvalidIpAddress(String address) {
85         assertThat(isIpAddressValid(address)).isFalse();
86     }
87
88     @ParameterizedTest
89     @ValueSource(strings = {"sample.com", "Sample.com", "onap.org", "SRI-NIC.ARPA", "ves-collector", "sample"})
90     void shouldAcceptValidDomainName(String domain) {
91         assertThat(isDomainNameValid(domain)).isTrue();
92     }
93
94     @ParameterizedTest
95     @ValueSource(strings = {" ", "", "sample@onap.org", "192.168.0.1", "http://sample.com"})
96     void shouldRejectInvalidDomainNames(String domain) {
97         assertThat(isDomainNameValid(domain)).isFalse();
98     }
99
100     @ParameterizedTest
101     @ValueSource(strings = {"http://sample.com/path", "ftp://sample.com/path/file.txt", "https://ves.pl"})
102     void shouldBeTrueForValidUris(String text) {
103         assertThat(isUriValid(text)).isTrue();
104     }
105     @ParameterizedTest
106     @ValueSource(strings = {"3http://sample.com", "192.168.0.1", "www.example.com"})
107     void shouldBeFalseForInvalidUris(String text) {
108         assertThat(isUriValid(text)).isFalse();
109     }
110
111     @ParameterizedTest
112     @ValueSource(strings = {"/text~", "/text#", "/text@", "/text*","/text$", "/text+", "/text%", "/text!", "/text(",
113         "/text)", "/text?", "/text|", "/text_", "/text^"})
114     void shouldBeTrueForStringsWithSpecialChars(String text) {
115         assertThat(isSpecialCharPresent(text)).isTrue();
116     }
117     @ParameterizedTest
118     @ValueSource(strings = {"text", ""})
119     void shouldBeFalseForStringsWithoutSpecialChars(String text) {
120         assertThat(isSpecialCharPresent(text)).isFalse();
121     }
122 }