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