summaryrefslogtreecommitdiff
path: root/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/ExchangeLoggingTest.java
blob: 07faf1ef3eed35998dd91ea8c11ba1b8d39260c0 (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
/*
 *
 * 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.server.logging;

import java.io.IOException;
import java.util.List;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.qpid.client.AMQConnection;
import org.apache.qpid.client.AMQSession;
import org.apache.qpid.client.AMQSession_0_10;
import org.apache.qpid.framing.AMQFrame;
import org.apache.qpid.framing.AMQShortString;
import org.apache.qpid.framing.ExchangeDeleteBody;
import org.apache.qpid.framing.ExchangeDeleteOkBody;
import org.apache.qpid.framing.amqp_8_0.MethodRegistry_8_0;

/**
 * Exchange
 *
 * The Exchange test suite validates that the follow log messages as specified in the Functional Specification.
 *
 * This suite of tests validate that the Exchange messages occur correctly and according to the following format:
 *
 * EXH-1001 : Create : [Durable] Type:<value> Name:<value>
 * EXH-1002 : Deleted
 */
public class ExchangeLoggingTest extends AbstractTestLogging
{

    static final String EXH_PREFIX = "EXH-";

    Connection _connection;
    Session _session;
    Queue _queue;
    String _name;
    String _type;

    @Override
    public void setUp() throws Exception
    {
        super.setUp();

        _connection = getConnection();

        _session = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        _type = "direct";
        _name = getTestQueueName()+ "-exchange";

        _queue = _session.createQueue(_type + "://" + _name + "/queue/queue");

    }

    /**
     * Description:
     * When a durable exchange is created an EXH-1001 message is logged with the Durable tag. This will be the first message from this exchange.
     * Input:
     *
     * 1. Running broker
     * 2. Client requests a durable exchange be created.
     * Output:
     *
     * <date> EXH-1001 : Create : Durable Type:<value> Name:<value>
     *
     * Validation Steps:
     * 3. The EXH ID is correct
     * 4. The Durable tag is present in the message
     */

    public void testExchangeCreateDurable() throws JMSException, IOException
    {
        // The client cannot create durable exchanges lets just look at the
        // ones the broker creates at startup.

        // They should all be durable

        // Ensure we have received the EXH log msg.
        waitForMessage("EXH-1001");

        List<String> results = findMatches(EXH_PREFIX);

        assertTrue("No Results found for Exchange.", results.size()>0);

        validateExchangeCreate(results, true, false);
    }

    /**
     * Description:
     * When an exchange is created an EXH-1001 message is logged. This will be the first message from this exchange.
     * Input:
     *
     * 1. Running broker
     * 2. Client requests an exchange be created.
     * Output:
     *
     * <date> EXH-1001 : Create : Type:<value> Name:<value>
     *
     * Validation Steps:
     * 3. The EXH ID is correct
     */
    public void testExchangeCreate() throws JMSException, IOException
    {
        //Ignore broker startup messages
        _monitor.markDiscardPoint();

        _session.createConsumer(_queue);
        // Ensure we have received the EXH log msg.
        waitForMessage("EXH-1001");

        List<String> results = findMatches(EXH_PREFIX);

        assertEquals("Result set larger than expected.", 1, results.size());

        validateExchangeCreate(results, false, true);
    }

    private void validateExchangeCreate(List<String> results, boolean durable, boolean checkNameAndType)
    {
        String log = getLogMessage(results, 0);
        String message = getMessageString(fromMessage(log));
        
        validateMessageID("EXH-1001", log);
        
        assertTrue("Log Message does not start with create:" + message,
                   message.startsWith("Create"));

        assertEquals("Unexpected Durable state:" + message, durable,
                message.contains("Durable"));
        
        if(checkNameAndType)
        {
            assertTrue("Log Message does not contain Type:" + message,
                    message.contains("Type: " + _type));
            assertTrue("Log Message does not contain Name:" + message,
                    message.contains("Name: " + _name));
        }
    }

    /**
     * Description:
     * An Exchange can be deleted through an AMQP ExchangeDelete method. When this is successful an EXH-1002 Delete message will be logged. This will be the last message from this exchange.
     * Input:
     *
     * 1. Running broker
     * 2. A new Exchange has been created
     * 3. Client requests that the new exchange be deleted.
     * Output:
     *
     * <date> EXH-1002 : Deleted
     *
     * Validation Steps:
     * 4. The EXH ID is correct
     * 5. There is a corresponding EXH-1001 Create message logged.
     */
    public void testExchangeDelete() throws Exception, IOException
    {
        //Ignore broker startup messages
        _monitor.markDiscardPoint();

        //create the exchange by creating a consumer
        _session.createConsumer(_queue);

        //now delete the exchange
        if(isBroker010())
        {
            ((AMQSession_0_10) _session).sendExchangeDelete(_name, false);
        }
        else
        {
            MethodRegistry_8_0 registry = new MethodRegistry_8_0();

            ExchangeDeleteBody body = registry.createExchangeDeleteBody(0, new AMQShortString(_name), false, true);

            AMQFrame exchangeDeclare = body.generateFrame(((AMQSession)_session).getChannelId());

            ((AMQConnection) _connection).getProtocolHandler().syncWrite(exchangeDeclare, ExchangeDeleteOkBody.class);
        }

        //Wait and ensure we get our last EXH-1002 msg
        waitForMessage("EXH-1002");

        List<String> results = findMatches(EXH_PREFIX);

        assertEquals("Result set larger than expected.", 2, results.size());

        validateExchangeCreate(results, false, false);

        String log = getLogMessage(results, 1);
        validateMessageID("EXH-1002", log);

        String message = getMessageString(fromMessage(log));
        assertEquals("Log Message not as expected", "Deleted", message);

    }

    public void testDiscardedMessage() throws Exception
    {
        //Ignore broker startup messages
        _monitor.markDiscardPoint();

        if (!isBroker010())
        {
            // Default 0-8..-0-9-1 behaviour is for messages to be rejected (returned to client).
            setTestClientSystemProperty("qpid.default_mandatory", "false");
        }

        _session = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // Do not create consumer so queue is not created and message will be discarded.
        final MessageProducer producer = _session.createProducer(_queue);

        // Sending message
        final TextMessage msg = _session.createTextMessage("msg");
        producer.send(msg);

        final String expectedMessageBody = "Discarded Message : Name: " + _name + " Routing Key: " + _queue.getQueueName();

        // Ensure we have received the EXH log msg.
        waitForMessage("EXH-1003");

        List<String> results = findMatches(EXH_PREFIX);
        assertEquals("Result set larger than expected.", 2, results.size());

        final String log = getLogMessage(results, 1);
        validateMessageID("EXH-1003", log);

        final String message = getMessageString(fromMessage(log));
        assertEquals("Log Message not as expected", expectedMessageBody, message);
    }
}