2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.oom.certservice.certification;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
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;
35 class X509CertificateParserTest {
37 private static final String SUBJECT = "CN=onap.org,OU=Linux-Foundation,O=ONAP,L=San-Francisco,ST=California,C=US";
39 private final X509CertificateParser parser = new X509CertificateParser();
42 void getSubject_shouldReturnCorrectSubject() {
44 X509Certificate certificate = mock(X509Certificate.class);
45 when(certificate.getSubjectX500Principal())
46 .thenReturn(new X500Principal(SUBJECT));
48 final X500Name subject = parser.getSubject(certificate);
50 assertThat(subject).isEqualTo(new X500Name(SUBJECT));
54 void getSans_shouldReturnCorrectSansArray() throws CertificateParsingException {
56 X509Certificate certificate = mock(X509Certificate.class);
57 when(certificate.getSubjectAlternativeNames())
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")
64 final GeneralName[] sans = parser.getSans(certificate);
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")
74 void getSans_shouldReturnEmptyArrayWhenNoSans() throws CertificateParsingException {
76 X509Certificate certificate = mock(X509Certificate.class);
77 when(certificate.getSubjectAlternativeNames()).thenReturn(null);
79 final GeneralName[] sans = parser.getSans(certificate);
81 assertThat(sans).isEmpty();