ea15740c9f09537192d63a74f5efd5d92ad3dcd4
[oom/platform/cert-service.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
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.aaf.certservice.certification.configuration.validation;
22
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.onap.aaf.certservice.CertServiceApplication;
27 import org.onap.aaf.certservice.certification.configuration.model.Authentication;
28 import org.onap.aaf.certservice.certification.configuration.model.CaMode;
29 import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.test.context.ContextConfiguration;
32 import org.springframework.test.context.junit.jupiter.SpringExtension;
33
34 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
35 import static org.junit.jupiter.api.Assertions.assertThrows;
36
37 @ExtendWith(SpringExtension.class)
38 @ContextConfiguration(classes = CertServiceApplication.class)
39 class Cmpv2ServerConfigurationValidatorTest {
40
41     @Autowired
42     private Cmpv2ServerConfigurationValidator validator;
43
44     private Authentication authentication;
45     private Cmpv2Server server;
46
47     @BeforeEach
48     private void init() {
49         setAuthentication();
50         setServerConfiguration();
51     }
52
53     @Test
54     public void givenValidServerDetailsWhenValidatingShouldNotThrowAnyException() {
55         //then
56         assertDoesNotThrow(() -> validator.validate(server));
57     }
58
59     @Test
60     public void givenWrongProtocolInURLServerDetailsWhenValidatingShouldThrowException() {
61         //given
62         server.setUrl("https://test.test.test:60000/");
63
64         //then
65         assertThrows(IllegalArgumentException.class, () -> {validator.validate(server);});
66     }
67
68     @Test
69     public void givenWrongPortInURLServerDetailsWhenValidatingShouldThrowException() {
70         //given
71         server.setUrl("http://test.test.test:70000/");
72
73         //then
74         assertThrows(IllegalArgumentException.class, () -> validator.validate(server));
75     }
76
77     @Test
78     public void givenWrongCANameLengthInURLServerDetailsWhenValidatingShouldThrowException() {
79         //given
80         server.setCaName("");
81
82         //then
83         assertThrows(IllegalArgumentException.class, () -> validator.validate(server));
84     }
85
86     @Test
87     public void givenWrongIssuerDNLengthInURLServerDetailsWhenValidatingShouldThrowException() {
88         //given
89         server.setIssuerDN("123");
90
91         //then
92         assertThrows(IllegalArgumentException.class, () -> validator.validate(server));
93     }
94
95     @Test
96     public void givenWrongRVLengthInURLServerDetailsWhenValidatingShouldThrowException() {
97         //given
98         authentication.setRv("");
99
100         //then
101         assertThrows(IllegalArgumentException.class, () -> validator.validate(server));
102     }
103
104     @Test
105     public void givenWrongIAKLengthInURLServerDetailsWhenValidatingShouldThrowException() {
106         //given
107         authentication.setIak("");
108
109         //then
110         assertThrows(IllegalArgumentException.class, () -> validator.validate(server));
111     }
112
113     private void setServerConfiguration() {
114         server = new Cmpv2Server();
115         server.setCaMode(CaMode.CLIENT);
116         server.setCaName("TEST");
117         server.setIssuerDN("CN=ManagementCA");
118         server.setUrl("http://127.0.0.1/ejbca/publicweb/cmp/cmp");
119         server.setAuthentication(authentication);
120     }
121
122     private void setAuthentication() {
123         authentication = new Authentication();
124         authentication.setRv("testRV");
125         authentication.setIak("testIAK");
126     }
127 }