6598fbe40feda33b7ab09c880cb68e7c580706ca
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / aaf / marshal / test / JU_CertsMarshal.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 org.junit.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.junit.Assert.*;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 import java.io.PrintStream;
31 import java.util.ArrayList;
32
33 import javax.xml.datatype.XMLGregorianCalendar;
34 import org.onap.aaf.cadi.aaf.marshal.CertsMarshal;
35 import org.onap.aaf.misc.env.util.Chrono;
36 import org.onap.aaf.misc.rosetta.OutRaw;
37 import org.onap.aaf.misc.rosetta.ParseException;
38 import org.onap.aaf.misc.rosetta.marshal.DataWriter;
39
40 import aaf.v2_0.Certs;
41 import aaf.v2_0.Certs.Cert;
42
43 public class JU_CertsMarshal {
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                 CertsStub certs = new CertsStub();
67                 CertsMarshal cm = new CertsMarshal();
68                 OutRaw raw = new OutRaw();
69
70                 raw.extract(certs, new PrintStream(outStream), cm);
71                 String[] output = outStream.toString().split("\n");
72
73                 String[] expected = new String[] {
74                 "{ - ",
75                         "[ - cert",
76                         "{ - ",
77                                 ", - fingerprint : \"" + fingerprintAsString + "\"",
78                                 ", - id : \"" + id + "\"",
79                                 ", - x500 : \"" + x500 + "\"",
80                                 ", - expires : \"" + Chrono.dateTime(expires) + "\"",
81                         "} - ",
82                         ", - ",
83                         "{ - ",
84                                 ", - fingerprint : \"" + fingerprintAsString + "\"",
85                                 ", - id : \"" + id + "\"",
86                                 ", - x500 : \"" + x500 + "\"",
87                                 ", - expires : \"" + Chrono.dateTime(expires) + "\"",
88                         "} - ",
89                         "] - ",
90                         "} - ",
91                 };
92
93                 assertThat(output.length, is(expected.length));
94
95                 for (int i = 0; i < output.length; i++) {
96                         assertThat(output[i], is(expected[i]));
97                 }
98         }
99
100         private Cert setupCert() {
101                 Cert cert = new Cert();
102                 cert.setId(id);
103                 cert.setX500(x500);
104                 cert.setExpires(expires);
105                 cert.setFingerprint(fingerprint.getBytes());
106                 return cert;
107         }
108
109         private class CertsStub extends Certs {
110                 public CertsStub() {
111                         cert = new ArrayList<>();
112                         for (int i = 0; i < 2; i++) {
113                                 cert.add(setupCert());
114                         }
115                 }
116         }
117
118 }