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