2 * ============LICENSE_START=======================================================
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
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.aaf.certservice.cmpv2client.external;
23 import org.bouncycastle.asn1.ASN1ObjectIdentifier;
24 import org.bouncycastle.asn1.x500.style.BCStyle;
25 import org.bouncycastle.cert.CertException;
26 import org.junit.jupiter.api.Test;
28 import java.util.Arrays;
29 import java.util.List;
31 import static org.junit.jupiter.api.Assertions.assertEquals;
32 import static org.junit.jupiter.api.Assertions.assertThrows;
37 void shouldCreateCorrectRdn() throws CertException {
39 final String expectedValue = "ManagmentCA";
40 final ASN1ObjectIdentifier expectedAoi = BCStyle.CN;
43 Rdn rdn1 = new Rdn("CN=ManagmentCA");
44 Rdn rdn2 = new Rdn("CN = ManagmentCA ");
45 Rdn rdn3 = new Rdn("CN", "ManagmentCA");
48 assertEquals(expectedValue, rdn1.getValue());
49 assertEquals(expectedValue, rdn2.getValue());
50 assertEquals(expectedValue, rdn3.getValue());
51 assertEquals(expectedAoi, rdn1.getAoi());
52 assertEquals(expectedAoi, rdn2.getAoi());
53 assertEquals(expectedAoi, rdn3.getAoi());
57 void shouldCorrectlySplitAndTrimString() {
59 String value1 = " T = Test";
60 List<String> expected1 = Arrays.asList("T", "Test");
62 String value2 = "This 123 is 99 tested 12345 string";
63 List<String> expected2 = Arrays.asList("This", "is 99 tested", "string");
66 List<String> actual1 = Rdn.parseRdn("=", value1);
67 List<String> actual2 = Rdn.parseRdn("[0-9]{3,}", value2);
70 assertEquals(expected1, actual1);
71 assertEquals(expected2, actual2);
75 void shouldConvertAoiStringToEnum() throws CertException {
76 Rdn rdn = new Rdn("CN", "ManagmentCA");
78 assertEquals(BCStyle.CN, rdn.getAoi("CN"));
79 assertEquals(BCStyle.C, rdn.getAoi("C"));
80 assertEquals(BCStyle.ST, rdn.getAoi("ST"));
81 assertEquals(BCStyle.L, rdn.getAoi("L"));
82 assertEquals(BCStyle.O, rdn.getAoi("O"));
83 assertEquals(BCStyle.OU, rdn.getAoi("OU"));
84 assertEquals(BCStyle.DC, rdn.getAoi("DC"));
85 assertEquals(BCStyle.GIVENNAME, rdn.getAoi("GN"));
86 assertEquals(BCStyle.SN, rdn.getAoi("SN"));
87 assertEquals(BCStyle.E, rdn.getAoi("E"));
88 assertEquals(BCStyle.E, rdn.getAoi("EMAIL"));
89 assertEquals(BCStyle.E, rdn.getAoi("EMAILADDRESS"));
90 assertEquals(BCStyle.INITIALS, rdn.getAoi("INITIALS"));
91 assertEquals(BCStyle.PSEUDONYM, rdn.getAoi("PSEUDONYM"));
92 assertEquals(BCStyle.GENERATION, rdn.getAoi("GENERATIONQUALIFIER"));
93 assertEquals(BCStyle.SERIALNUMBER, rdn.getAoi("SERIALNUMBER"));
94 assertThrows(CertException.class, () -> rdn.getAoi("INVALIDTAG"));