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