e75cea4ec8cb898f2eac01b06e12e4fec9e504f7
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / wsse / test / JU_XReader.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
4  * * ===========================================================================
5  * * Copyright © 2017 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.wsse.test;
23
24 import static org.junit.Assert.assertThat;
25 import static org.hamcrest.CoreMatchers.is;
26
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.io.PrintWriter;
31
32 import javax.xml.stream.XMLStreamException;
33 import javax.xml.stream.events.XMLEvent;
34
35 import org.junit.AfterClass;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.onap.aaf.cadi.wsse.XEvent;
39 import org.onap.aaf.cadi.wsse.XReader;
40
41 public class JU_XReader {
42
43         private final static String TEST_DIR_NAME = "src/test/resources";
44         private final static String TEST_XML_NAME = "test.xml";
45         private static File testXML;
46
47         private final static String COMMENT = "a comment";
48         private final static String OUTER_TAG = "outerTag";
49         private final static String INNER_TAG = "innerTag";
50         private final static String DATA_TAG = "dataTag";
51         private final static String DATA = "some text that represents data";
52         private final static String SELF_CLOSING_TAG = "selfClosingTag";
53         private final static String PREFIX = "prefix";
54         private final static String SUFFIX = "suffix";
55
56         @BeforeClass
57         public static void setupOnce() throws IOException {
58                 testXML = setupXMLFile();
59         }
60
61         @AfterClass
62         public static void tearDownOnce() {
63                 testXML.delete();
64         }
65
66         @Test
67         public void test() throws XMLStreamException, IOException {
68                 FileInputStream fis = new FileInputStream(TEST_DIR_NAME + '/' + TEST_XML_NAME);
69                 try {
70                         XReader xr = new XReader(fis);
71                         assertThat(xr.hasNext(), is(true));
72                         XEvent xe;
73
74                         xe = getNextEvent(xr);
75                         assertThat(xe.getEventType(), is(XMLEvent.START_DOCUMENT));
76
77                         xe = getNextEvent(xr);
78                         assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
79
80                         xe = getNextEvent(xr);
81                         assertThat(xe.getEventType(), is(XMLEvent.COMMENT));
82                         assertThat(((XEvent.Comment)xe).value, is(COMMENT));
83
84                         xe = getNextEvent(xr);
85                         assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
86                         assertThat(xe.asStartElement().getName().toString(), is(OUTER_TAG));
87
88                         xe = getNextEvent(xr);
89                         assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
90                         assertThat(xe.asStartElement().getName().toString(), is(INNER_TAG));
91
92                         xe = getNextEvent(xr);
93                         assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
94                         assertThat(xe.asStartElement().getName().toString(), is(DATA_TAG));
95
96                         xe = getNextEvent(xr);
97                         assertThat(xe.getEventType(), is(XMLEvent.CHARACTERS));
98                         assertThat(xe.asCharacters().getData().toString(), is(DATA));
99
100                         xe = getNextEvent(xr);
101                         assertThat(xe.getEventType(), is(XMLEvent.END_ELEMENT));
102                         assertThat(xe.asEndElement().getName().toString(), is(DATA_TAG));
103
104                         xe = getNextEvent(xr);
105                         assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
106                         assertThat(xe.asStartElement().getName().toString(), is(SELF_CLOSING_TAG));
107
108                         xe = getNextEvent(xr);
109                         assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
110                         assertThat(xe.asStartElement().getName().toString(), is(SUFFIX));
111
112                         xe = getNextEvent(xr);
113                         assertThat(xe.getEventType(), is(XMLEvent.END_ELEMENT));
114                         assertThat(xe.asEndElement().getName().toString(), is(INNER_TAG));
115
116                         xe = getNextEvent(xr);
117                         assertThat(xe.getEventType(), is(XMLEvent.END_ELEMENT));
118                         assertThat(xe.asEndElement().getName().toString(), is(OUTER_TAG));
119
120                         assertThat(xr.hasNext(), is(false));
121
122                 } finally {
123                         fis.close();
124                 }
125         }
126
127         private static XEvent getNextEvent(XReader xr) throws XMLStreamException {
128                 if (xr.hasNext()) {
129                         return xr.nextEvent();
130                 }
131                 return null;
132         }
133
134         private static File setupXMLFile() throws IOException {
135                 File xmlFile = new File(TEST_DIR_NAME, TEST_XML_NAME);
136                 PrintWriter writer = new PrintWriter(xmlFile);
137                 writer.println("    ");  // Whitespace before the document - this is for coverage
138                 writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
139                 writer.println("<!DOCTYPE xml>");
140                 writer.println("<!--" + COMMENT + "-->");
141                 writer.println("<" + OUTER_TAG + ">");
142                 writer.println("  <" + INNER_TAG + ">");
143                 writer.println("    <" + DATA_TAG + ">" + DATA + "</" + DATA_TAG + ">");
144                 writer.println("    <" + SELF_CLOSING_TAG + " withAnAttribute=\"That has nested \\\" marks\" />");
145                 writer.println("    <" + PREFIX + ":" + SUFFIX + "/>");
146                 writer.println("  </" + INNER_TAG + ">");
147                 writer.println("</" + OUTER_TAG + ">");
148                 writer.flush();
149                 writer.close();
150                 return xmlFile;
151         }
152 }