aboutsummaryrefslogtreecommitdiffstats
path: root/cadi/core/src/test/java/org/onap/ccsdk/apps/cadi/wsse/test/JU_XReader.java
blob: 3cb731776d2cbf4e3398bb32edb33534328f4041 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.ccsdk
 * * ===========================================================================
 * * Copyright © 2023 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * *
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * *
 ******************************************************************************/

package org.onap.ccsdk.apps.cadi.wsse.test;

import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.is;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.ccsdk.apps.cadi.wsse.XEvent;
import org.onap.ccsdk.apps.cadi.wsse.XReader;

public class JU_XReader {

    private final static String TEST_DIR_NAME = "src/test/resources";
    private final static String TEST_XML_NAME = "test.xml";
    private static File testXML;

    private final static String COMMENT = "a comment";
    private final static String OUTER_TAG = "outerTag";
    private final static String INNER_TAG = "innerTag";
    private final static String DATA_TAG = "dataTag";
    private final static String DATA = "some text that represents data";
    private final static String SELF_CLOSING_TAG = "selfClosingTag";
    private final static String PREFIX = "prefix";
    private final static String SUFFIX = "suffix";

    @BeforeClass
    public static void setupOnce() throws IOException {
        testXML = setupXMLFile();
    }

    @AfterClass
    public static void tearDownOnce() {
        testXML.delete();
    }

    @Test
    public void test() throws XMLStreamException, IOException {
        FileInputStream fis = new FileInputStream(TEST_DIR_NAME + '/' + TEST_XML_NAME);
        try {
            XReader xr = new XReader(fis);
            assertThat(xr.hasNext(), is(true));
            XEvent xe;

            xe = getNextEvent(xr);
            assertThat(xe.getEventType(), is(XMLEvent.START_DOCUMENT));

            xe = getNextEvent(xr);
            assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));

            xe = getNextEvent(xr);
            assertThat(xe.getEventType(), is(XMLEvent.COMMENT));
            assertThat(((XEvent.Comment)xe).value, is(COMMENT));

            xe = getNextEvent(xr);
            assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
            assertThat(xe.asStartElement().getName().toString(), is(OUTER_TAG));

            xe = getNextEvent(xr);
            assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
            assertThat(xe.asStartElement().getName().toString(), is(INNER_TAG));

            xe = getNextEvent(xr);
            assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
            assertThat(xe.asStartElement().getName().toString(), is(DATA_TAG));

            xe = getNextEvent(xr);
            assertThat(xe.getEventType(), is(XMLEvent.CHARACTERS));
            assertThat(xe.asCharacters().getData().toString(), is(DATA));

            xe = getNextEvent(xr);
            assertThat(xe.getEventType(), is(XMLEvent.END_ELEMENT));
            assertThat(xe.asEndElement().getName().toString(), is(DATA_TAG));

            xe = getNextEvent(xr);
            assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
            assertThat(xe.asStartElement().getName().toString(), is(SELF_CLOSING_TAG));

            xe = getNextEvent(xr);
            assertThat(xe.getEventType(), is(XMLEvent.START_ELEMENT));
            assertThat(xe.asStartElement().getName().toString(), is(SUFFIX));

            xe = getNextEvent(xr);
            assertThat(xe.getEventType(), is(XMLEvent.END_ELEMENT));
            assertThat(xe.asEndElement().getName().toString(), is(INNER_TAG));

            xe = getNextEvent(xr);
            assertThat(xe.getEventType(), is(XMLEvent.END_ELEMENT));
            assertThat(xe.asEndElement().getName().toString(), is(OUTER_TAG));

            assertThat(xr.hasNext(), is(false));

        } finally {
            fis.close();
        }
    }

    private static XEvent getNextEvent(XReader xr) throws XMLStreamException {
        if (xr.hasNext()) {
            return xr.nextEvent();
        }
        return null;
    }

    private static File setupXMLFile() throws IOException {
        File xmlFile = new File(TEST_DIR_NAME, TEST_XML_NAME);
        PrintWriter writer = new PrintWriter(xmlFile);
        writer.println("    ");  // Whitespace before the document - this is for coverage
        writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        writer.println("<!DOCTYPE xml>");
        writer.println("<!--" + COMMENT + "-->");
        writer.println("<" + OUTER_TAG + ">");
        writer.println("  <" + INNER_TAG + ">");
        writer.println("    <" + DATA_TAG + ">" + DATA + "</" + DATA_TAG + ">");
        writer.println("    <" + SELF_CLOSING_TAG + " withAnAttribute=\"That has nested \\\" marks\" />");
        writer.println("    <" + PREFIX + ":" + SUFFIX + "/>");
        writer.println("  </" + INNER_TAG + ">");
        writer.println("</" + OUTER_TAG + ">");
        writer.flush();
        writer.close();
        return xmlFile;
    }
}