560014d10917b5d7c10b69679d64f1974feec7c2
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / aaf / marshal / test / JU_CertMarshal.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. 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
22 package org.onap.aaf.cadi.aaf.marshal.test;
23
24 import static org.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
26
27 import java.io.ByteArrayOutputStream;
28 import java.io.IOException;
29 import java.io.PrintStream;
30
31 import javax.xml.datatype.XMLGregorianCalendar;
32
33 import org.junit.*;
34
35 import org.onap.aaf.cadi.aaf.marshal.CertMarshal;
36 import org.onap.aaf.misc.env.util.Chrono;
37 import org.onap.aaf.misc.rosetta.OutRaw;
38 import org.onap.aaf.misc.rosetta.ParseException;
39 import org.onap.aaf.misc.rosetta.marshal.DataWriter;
40
41 import aaf.v2_0.Certs.Cert;
42
43 public class JU_CertMarshal {
44
45         private static final String fingerprint = "fingerprint";
46         private static final String id = "id";
47         private static final String x500 = "x500";
48
49         private String fingerprintAsString;
50
51         private XMLGregorianCalendar expires;
52
53         private ByteArrayOutputStream outStream;
54
55         @Before
56         public void setup() {
57                 expires = Chrono.timeStamp();
58                 outStream = new ByteArrayOutputStream();
59                 StringBuilder sb = new StringBuilder();
60                 DataWriter.HEX_BINARY.write(fingerprint.getBytes(), sb);
61                 fingerprintAsString = sb.toString();
62         }
63
64         @Test
65         public void test() throws ParseException, IOException {
66                 Cert cert = setupCert();
67                 CertMarshal cm = new CertMarshal();
68                 OutRaw raw = new OutRaw();
69
70                 raw.extract(cert, new PrintStream(outStream), cm);
71
72                 String[] output = outStream.toString().split("\n");
73
74                 String[] expected = new String[] {
75                                 "{ - ",
76                                 ", - fingerprint : \"" + fingerprintAsString + "\"",
77                                 ", - id : \"" + id + "\"",
78                                 ", - x500 : \"" + x500 + "\"",
79                                 ", - expires : \"" + Chrono.dateTime(expires) + "\"",
80                                 "} - ",
81                 };
82
83                 assertThat(output.length, is(expected.length));
84
85                 for (int i = 0; i < output.length; i++) {
86                         assertThat(output[i], is(expected[i]));
87                 }
88         }
89
90         private Cert setupCert() {
91                 Cert cert = new Cert();
92                 cert.setId(id);
93                 cert.setX500(x500);
94                 cert.setExpires(expires);
95                 cert.setFingerprint(fingerprint.getBytes());
96                 return cert;
97         }
98
99 }