[OOM-CERT-SERVICE] Add logic for KUR/CR detection
[oom/platform/cert-service.git] / certService / src / test / java / org / onap / oom / certservice / certification / X509CertificateParserTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Cert Service
4  * ================================================================================
5  * Copyright (C) 2021 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.certification;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26
27 import java.security.cert.CertificateParsingException;
28 import java.security.cert.X509Certificate;
29 import java.util.List;
30 import javax.security.auth.x500.X500Principal;
31 import org.bouncycastle.asn1.x500.X500Name;
32 import org.bouncycastle.asn1.x509.GeneralName;
33 import org.junit.jupiter.api.Test;
34
35 class X509CertificateParserTest {
36
37     private static final String SUBJECT = "CN=onap.org,OU=Linux-Foundation,O=ONAP,L=San-Francisco,ST=California,C=US";
38
39     private final X509CertificateParser parser = new X509CertificateParser();
40
41     @Test
42     void getSubject_shouldReturnCorrectSubject() {
43         //given
44         X509Certificate certificate = mock(X509Certificate.class);
45         when(certificate.getSubjectX500Principal())
46             .thenReturn(new X500Principal(SUBJECT));
47         //when
48         final X500Name subject = parser.getSubject(certificate);
49         //then
50         assertThat(subject).isEqualTo(new X500Name(SUBJECT));
51     }
52
53     @Test
54     void getSans_shouldReturnCorrectSansArray() throws CertificateParsingException {
55         //given
56         X509Certificate certificate = mock(X509Certificate.class);
57         when(certificate.getSubjectAlternativeNames())
58             .thenReturn(List.of(
59                 List.of(GeneralName.dNSName, "test.onap.org"),
60                 List.of(GeneralName.dNSName, "test2.onap.org"),
61                 List.of(GeneralName.iPAddress, "127.0.0.1")
62             ));
63         //when
64         final GeneralName[] sans = parser.getSans(certificate);
65         //then
66         assertThat(sans).containsExactlyInAnyOrder(
67             new GeneralName(GeneralName.dNSName, "test.onap.org"),
68             new GeneralName(GeneralName.dNSName, "test2.onap.org"),
69             new GeneralName(GeneralName.iPAddress, "127.0.0.1")
70         );
71     }
72
73     @Test
74     void getSans_shouldReturnEmptyArrayWhenNoSans() throws CertificateParsingException {
75         //given
76         X509Certificate certificate = mock(X509Certificate.class);
77         when(certificate.getSubjectAlternativeNames()).thenReturn(null);
78         //when
79         final GeneralName[] sans = parser.getSans(certificate);
80         //then
81         assertThat(sans).isEmpty();
82     }
83
84 }