summaryrefslogtreecommitdiff
path: root/java/broker-plugins/experimental/info/src/test/java/org/apache/qpid/info/systest/InfoPluginTest.java
blob: 156c9eb13827794611aa287a6ac59389d834e4aa (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 *
 */
package org.apache.qpid.info.systest;

import org.apache.qpid.test.utils.QpidBrokerTestCase;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class InfoPluginTest extends QpidBrokerTestCase
{
    private String QPID_HOME = null;

    private ServerSocket _server = null;

    private int _port;

    private static final String CR = System.getProperty("line.separator");

    private static final String FS = File.separator;

    private final String _cfgRelPath = "etc" + FS + "qpidinfo.ini";

    private File _tmpCfgFile;

    private final String _soapEnvelopeHead = "<?xml version=\"1.0\"?><soap:Envelope xmlns:soap=\"http://www.w3.org/2001/12/soap-envelope\" soap:encodingStyle=\"http://www.w3.org/2001/12/soap-encoding\">";

    private final String _soapEnvelopeTail = "</soap:Envelope>";

    private String _soapMessage1 = "@ACTION" + "-" + "@VERSION";

    private String _soapMessage2 = "@VERSION" + "-" + "@ACTION";

    private CountDownLatch _latch = new CountDownLatch(2);

    final List<List<String>> _recv = new ArrayList<List<String>>();

    Thread _socketAcceptor;

    public void setUp() throws Exception
    {
        QPID_HOME = System.getProperty("QPID_HOME");
        if (QPID_HOME != null)
        {
            System.out.println("QPID_HOME=" + QPID_HOME);
        }
        else
        {
            fail("QPID_HOME not set");
        }

        startSoapServer();
        // Must start the server first to identify a free port.
        createConfigFile();
    }

    public void tearDown() throws Exception
    {
        System.out.println("*** Stopping socket server...");
        _socketAcceptor.join(2000);

        System.out.println("*** Deleting the config file...");
        if (_tmpCfgFile.isFile())
        {
            _tmpCfgFile.delete();
        }
        super.tearDown();
    }

    private void createConfigFile()
    {
        try
        {
            _tmpCfgFile = new File(QPID_HOME + FS + _cfgRelPath);
            _tmpCfgFile.deleteOnExit();
            if (_tmpCfgFile.isFile())
            {
                _tmpCfgFile.delete();
            }
            assertTrue("Unable to create file.", _tmpCfgFile.createNewFile());
            assertTrue(_tmpCfgFile.isFile());
            FileWriter fwriter = new FileWriter(_tmpCfgFile);
            BufferedWriter writer = new BufferedWriter(fwriter);
            writer.write("protocol=soap");
            writer.write(CR);
            writer.write("soap.hostname=localhost");
            writer.write(CR);
            writer.write("soap.port=" + _port);
            writer.write(CR);
            writer.write(CR);
            writer.write("[MSG1]");
            writer.write(CR);
            writer.write("soap.path=/info1");
            writer.write(CR);
            writer.write("soap.action=submitinfo1");
            writer.write(CR);
            writer.write("soap.envelope=" + _soapEnvelopeHead + _soapMessage1
                         + _soapEnvelopeTail);
            writer.write(CR);
            writer.write(CR);
            writer.write("[MSG2]");
            writer.write(CR);
            writer.write("soap.path=/info2");
            writer.write(CR);
            writer.write("soap.action=submitinfo2");
            writer.write(CR);
            writer.write("soap.envelope=" + _soapEnvelopeHead + _soapMessage2
                         + _soapEnvelopeTail);
            writer.write(CR);
            writer.write(CR);
            writer.close();
            assertTrue("Config file size is zero", _tmpCfgFile.length() > 0);
        }
        catch (IOException e)
        {
            fail("Unable to create the qpidinfo.properties due to: "
                 + e.getMessage());
        }
    }

    private void startSoapServer() throws Exception
    {
        try
        {
            _server = new ServerSocket(0);
            _port = _server.getLocalPort();
            assertTrue("Server not yet bound.", _port != -1);

            assertNotNull("SocketServer is null", _server);
        }
        catch (Exception ex)
        {
            fail("Unable to start the socket server due to: " + ex.getMessage());
        }

        _socketAcceptor = new Thread()
        {
            public void run()
            {
                while (true)
                {
                    try
                    {
                        Socket socket = _server.accept();
                        new ConnectionHandler(socket);
                    }
                    catch (IOException e)
                    {
                        fail("Error opening the socket in accept mode");
                    }
                }
            }
        };
        _socketAcceptor.start();
        System.out.println("*** Socket server started...");
    }

    class ConnectionHandler implements Runnable
    {
        private Socket _socket;

        public ConnectionHandler(Socket socket)
        {
            _socket = socket;
            Thread t = new Thread(this);
            t.start();
        }

        public void run()
        {
            System.out.println("*** Connection handler running...");
            List<String> buf = new ArrayList<String>();
            String line;
            try
            {
                BufferedReader br = new BufferedReader(new InputStreamReader(
                        _socket.getInputStream()));
                assertNotNull(br);
                while ((line = br.readLine()) != null)
                {
                    buf.add(line);
                }
                br.close();
                System.out.println("*** Received buffer: " + buf);
                System.out.println("*** Latch countdown");
                _latch.countDown();
                synchronized (_recv)
                {
                    _recv.add(buf);
                }
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
                fail("Exception while reading from the socket");
            }

        }

    }

    public void testInfoPlugin() throws Exception
    {
        //Start the broker
        super.setUp();
        if (!_latch.await(10, TimeUnit.SECONDS))
        {
            fail("Timeout awaiting for the latch, upon startup");
        }

        validateResponses("STARTUP");

        _recv.clear();
        _latch = new CountDownLatch(2);

        stopBroker();

        if (!_latch.await(10, TimeUnit.SECONDS))
        {
            fail("Timeout awaiting for the latch, upon shutdown");
        }

        validateResponses("SHUTDOWN");

    }

    /**
     * Check the responses from the server to ensure they contain the required messages.
     * @param action String to match for the SHUTDOWN or STARTUP action.
     */
    private void validateResponses(String action)
    {
        assertTrue("Received less than 2 messages", _recv.size() > 1);

        // Message 1
        assertTrue("Message does not contain Host: localhost:" + _port + "\n" + _recv.get(0), _recv.get(0).contains("Host: localhost:" + _port));
        assertTrue("Message does not contain: User-Agent: Axis2 " + "\n" + _recv.get(0), _recv.get(0).contains("User-Agent: Axis2"));
        assertTrue("Message does not contain: SOAPAction: \"urn:submitinfo\"" + "\n" + _recv.get(0).get(4), _recv.get(0).get(4).startsWith("SOAPAction: \"urn:submitinfo"));
        assertTrue("Message does not contain '" + action + "' in the soap envelope" + "\n" + _recv.get(0).get(7), _recv.get(0).get(7).contains(action));

        // Message 2
        assertTrue("Message does not contain Host: localhost:" + _port + "\n" + _recv.get(1), _recv.get(1).contains("Host: localhost:" + _port));
        assertTrue("Message does not contain: User-Agent: Axis2 " + "\n" + _recv.get(1), _recv.get(1).contains("User-Agent: Axis2"));
        assertTrue("Message does not contain: SOAPAction: \"urn:submitinfo\"" + "\n" + _recv.get(1).get(4), _recv.get(1).get(4).startsWith("SOAPAction: \"urn:submitinfo"));
        assertTrue("Message does not contain '" + action + "' in the soap envelope" + "\n" + _recv.get(1).get(7), _recv.get(1).get(7).contains(action));
    }

}