summaryrefslogtreecommitdiff
path: root/qpid/java/broker-plugins/management-http/src/test
diff options
context:
space:
mode:
authorKeith Wall <kwall@apache.org>2015-03-03 14:56:40 +0000
committerKeith Wall <kwall@apache.org>2015-03-03 14:56:40 +0000
commit9dc57fe738f366d875c2319dafdfa2c50ce2f20b (patch)
treebe6634866a966f358fcb1ba6ba29dfb5c9c340c1 /qpid/java/broker-plugins/management-http/src/test
parentfe37626d4fd8fb3ee5b3146a5159024a3d6d3357 (diff)
downloadqpid-python-9dc57fe738f366d875c2319dafdfa2c50ce2f20b.tar.gz
merge from trunk
git-svn-id: https://svn.apache.org/repos/asf/qpid/branches/QPID-6262-JavaBrokerNIO@1663717 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'qpid/java/broker-plugins/management-http/src/test')
-rw-r--r--qpid/java/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/report/ReportRunnerTest.java186
-rw-r--r--qpid/java/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/report/TestBinaryReport.java114
-rw-r--r--qpid/java/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/report/TestTextReport.java84
-rw-r--r--qpid/java/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/rest/ConfiguredObjectToMapConverterTest.java1
-rw-r--r--qpid/java/broker-plugins/management-http/src/test/resources/META-INF/services/org.apache.qpid.server.management.plugin.report.QueueReport2
5 files changed, 387 insertions, 0 deletions
diff --git a/qpid/java/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/report/ReportRunnerTest.java b/qpid/java/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/report/ReportRunnerTest.java
new file mode 100644
index 0000000000..38432a26f4
--- /dev/null
+++ b/qpid/java/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/report/ReportRunnerTest.java
@@ -0,0 +1,186 @@
+/*
+ *
+ * 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.management.plugin.report;
+
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectOutputStream;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.mockito.ArgumentCaptor;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+import org.apache.qpid.server.message.AMQMessageHeader;
+import org.apache.qpid.server.message.ServerMessage;
+import org.apache.qpid.server.model.Queue;
+import org.apache.qpid.server.queue.AMQQueue;
+import org.apache.qpid.server.queue.QueueEntry;
+import org.apache.qpid.server.queue.QueueEntryVisitor;
+import org.apache.qpid.test.utils.QpidTestCase;
+
+public class ReportRunnerTest extends QpidTestCase
+{
+ public void testTextReportCountsMessages()
+ {
+ ReportRunner<String> runner = (ReportRunner<String>) ReportRunner.createRunner(TestTextReport.NAME,
+ Collections.<String, String[]>emptyMap());
+ Queue queue = createMockQueue();
+ assertEquals("There are 0 messages on the queue.", runner.runReport(queue));
+
+ runner = (ReportRunner<String>) ReportRunner.createRunner(TestTextReport.NAME,
+ Collections.<String, String[]>emptyMap());
+ Queue queue1 = createMockQueue(mock(ServerMessage.class));
+ assertEquals("There are 1 messages on the queue.", runner.runReport(queue1));
+
+ runner = (ReportRunner<String>) ReportRunner.createRunner(TestTextReport.NAME,
+ Collections.<String, String[]>emptyMap());
+ Queue queue2 = createMockQueue(mock(ServerMessage.class), mock(ServerMessage.class));
+ assertEquals("There are 2 messages on the queue.", runner.runReport(queue2));
+ }
+
+ public void testTextReportSingleStringParam()
+ {
+ Queue queue2 = createMockQueue(mock(ServerMessage.class), mock(ServerMessage.class));
+
+ Map<String, String[]> parameterMap = new HashMap<>();
+ parameterMap.put("stringParam", new String[]{"hello world"});
+ ReportRunner<String> runner =
+ (ReportRunner<String>) ReportRunner.createRunner(TestTextReport.NAME, parameterMap);
+ assertEquals("There are 2 messages on the queue. stringParam = hello world.", runner.runReport(queue2));
+ }
+
+ public void testTextReportSingleStringArrayParam()
+ {
+ Queue queue = createMockQueue();
+
+ Map<String, String[]> parameterMap = new HashMap<>();
+ parameterMap.put("stringArrayParam", new String[] { "hello world", "goodbye"});
+ ReportRunner<String> runner = (ReportRunner<String>) ReportRunner.createRunner(TestTextReport.NAME, parameterMap);
+ assertEquals("There are 0 messages on the queue. stringArrayParam = [hello world, goodbye].", runner.runReport(queue));
+
+ }
+
+
+ public void testTextReportBothParams()
+ {
+ Queue queue = createMockQueue();
+
+ Map<String, String[]> parameterMap = new HashMap<>();
+ parameterMap.put("stringParam", new String[]{"hello world"});
+ parameterMap.put("stringArrayParam", new String[] { "hello world", "goodbye"});
+ ReportRunner<String> runner = (ReportRunner<String>) ReportRunner.createRunner(TestTextReport.NAME, parameterMap);
+ assertEquals("There are 0 messages on the queue. stringParam = hello world. stringArrayParam = [hello world, goodbye].", runner.runReport(queue));
+
+ }
+
+ public void testInvalidReportName()
+ {
+ try
+ {
+ ReportRunner.createRunner("unknown", Collections.<String, String[]>emptyMap());
+ fail("Unknown report name should throw exception");
+ }
+ catch(IllegalArgumentException e)
+ {
+ assertEquals("Unknown report: unknown", e.getMessage());
+ }
+ }
+
+ public void testBinaryReportWithLimit() throws Exception
+ {
+ Queue queue = createMockQueue(createMessageWithAppProperties(Collections.<String,Object>singletonMap("key",1)),
+ createMessageWithAppProperties(Collections.<String,Object>singletonMap("key",2)),
+ createMessageWithAppProperties(Collections.<String, Object>singletonMap("key", 3)),
+ createMessageWithAppProperties(Collections.<String, Object>singletonMap("key", 4)));
+ Map<String, String[]> parameterMap = new HashMap<>();
+ parameterMap.put("propertyName", new String[]{"key"});
+ parameterMap.put("limit", new String[] { "3" });
+
+ ReportRunner<byte[]> runner = (ReportRunner<byte[]>) ReportRunner.createRunner(TestBinaryReport.NAME, parameterMap);
+
+ ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+ ObjectOutputStream objects = new ObjectOutputStream(bytes);
+ objects.writeObject(Integer.valueOf(1));
+ objects.writeObject(Integer.valueOf(2));
+ objects.writeObject(Integer.valueOf(3));
+ objects.flush();
+ byte[] expected = bytes.toByteArray();
+ byte[] actual = runner.runReport(queue);
+ assertTrue("Output not as expected", Arrays.equals(expected, actual));
+ }
+
+ private ServerMessage<?> createMessageWithAppProperties(final Map<String,Object> props)
+ {
+ ServerMessage<?> message = mock(ServerMessage.class);
+ final AMQMessageHeader header = mock(AMQMessageHeader.class);
+ when(message.getMessageHeader()).thenReturn(header);
+ final ArgumentCaptor<String> headerNameCaptor = ArgumentCaptor.forClass(String.class);
+ when(header.getHeader(headerNameCaptor.capture())).thenAnswer(new Answer<Object>()
+ {
+ @Override
+ public Object answer(final InvocationOnMock invocation) throws Throwable
+ {
+ String header = headerNameCaptor.getValue();
+ return props.get(header);
+ }
+ });
+ when(header.getHeaderNames()).thenReturn(props.keySet());
+ return message;
+ }
+
+ private Queue createMockQueue(final ServerMessage<?>... messages)
+ {
+ final AMQQueue queue = mock(AMQQueue.class);
+ final ArgumentCaptor<QueueEntryVisitor> captor = ArgumentCaptor.forClass(QueueEntryVisitor.class);
+ doAnswer(new Answer()
+ {
+ @Override
+ public Object answer(final InvocationOnMock invocation) throws Throwable
+ {
+ QueueEntryVisitor visitor = captor.getValue();
+ for(ServerMessage<?> message : messages)
+ {
+ if(visitor.visit(makeEntry(queue, message)))
+ {
+ break;
+ }
+ }
+ return null;
+ }
+ }).when(queue).visit(captor.capture());
+ return queue;
+ }
+
+ private QueueEntry makeEntry(final AMQQueue queue, final ServerMessage<?> message)
+ {
+ QueueEntry entry = mock(QueueEntry.class);
+ when(entry.getQueue()).thenReturn(queue);
+ when(entry.getMessage()).thenReturn(message);
+ return entry;
+ }
+}
diff --git a/qpid/java/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/report/TestBinaryReport.java b/qpid/java/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/report/TestBinaryReport.java
new file mode 100644
index 0000000000..fc5e93631e
--- /dev/null
+++ b/qpid/java/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/report/TestBinaryReport.java
@@ -0,0 +1,114 @@
+/*
+ *
+ * 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.management.plugin.report;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectOutputStream;
+
+public class TestBinaryReport extends QueueBinaryReport
+{
+
+
+ private int _limit;
+ private String _propertyName;
+ private int _count;
+ private final ByteArrayOutputStream _bytesOutputStream = new ByteArrayOutputStream();
+ private final ObjectOutputStream _objectOutputStream;
+ public static final String NAME = "testBinary";
+
+ public TestBinaryReport()
+ {
+ try
+ {
+ _objectOutputStream = new ObjectOutputStream(_bytesOutputStream);
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException(e);
+ }
+ ;
+ }
+
+ @Override
+ public String getName()
+ {
+ return NAME;
+ }
+
+ @Override
+ public String getContentType()
+ {
+ return "application/octet-stream";
+ }
+
+ @Override
+ public void addMessage(final ReportableMessage reportableMessage)
+ {
+ if(_propertyName != null)
+ {
+ Object value = reportableMessage.getMessageHeader().getHeader(_propertyName);
+ if(value != null)
+ {
+ try
+ {
+ _objectOutputStream.writeObject(value);
+ }
+ catch (IOException e)
+ {
+ // ignore
+ }
+ }
+ }
+ _count++;
+ }
+
+ @Override
+ public boolean isComplete()
+ {
+ return _limit != 0 && _count >= _limit;
+ }
+
+ @Override
+ public byte[] getReport()
+ {
+ try
+ {
+ _objectOutputStream.flush();
+
+ return _bytesOutputStream.toByteArray();
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public void setLimit(final String limit)
+ {
+ _limit = Integer.parseInt(limit);
+ }
+
+ public void setPropertyName(final String propertyName)
+ {
+ this._propertyName = propertyName;
+ }
+}
diff --git a/qpid/java/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/report/TestTextReport.java b/qpid/java/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/report/TestTextReport.java
new file mode 100644
index 0000000000..7f9e1e2962
--- /dev/null
+++ b/qpid/java/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/report/TestTextReport.java
@@ -0,0 +1,84 @@
+/*
+ *
+ * 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.management.plugin.report;
+
+import java.util.Arrays;
+
+public class TestTextReport extends QueueTextReport
+{
+ public static final String NAME = "testText";
+ private int _count;
+ private String _stringParam;
+ private String[] _stringArrayParam;
+
+ @Override
+ public String getName()
+ {
+ return NAME;
+ }
+
+ @Override
+ public String getContentType()
+ {
+ return "text/plain";
+ }
+
+ @Override
+ public void addMessage(final ReportableMessage reportableMessage)
+ {
+ _count++;
+ }
+
+ @Override
+ public boolean isComplete()
+ {
+ return false;
+ }
+
+ @Override
+ public String getReport()
+ {
+ StringBuilder result = new StringBuilder("There are " + _count + " messages on the queue.");
+ if(_stringParam != null)
+ {
+ result.append(" stringParam = " + _stringParam + ".");
+ }
+ if(_stringArrayParam != null)
+ {
+ result.append(" stringArrayParam = " + Arrays.asList(_stringArrayParam) + ".");
+ }
+ return result.toString();
+ }
+
+ @SuppressWarnings("unused")
+ public void setStringParam(final String value)
+ {
+ _stringParam = value;
+ }
+
+ @SuppressWarnings("unused")
+ public void setStringArrayParam(final String[] value)
+ {
+ _stringArrayParam = value;
+ }
+
+
+}
diff --git a/qpid/java/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/rest/ConfiguredObjectToMapConverterTest.java b/qpid/java/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/rest/ConfiguredObjectToMapConverterTest.java
index b3c9bd911f..5fb73c8ee4 100644
--- a/qpid/java/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/rest/ConfiguredObjectToMapConverterTest.java
+++ b/qpid/java/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/rest/ConfiguredObjectToMapConverterTest.java
@@ -313,6 +313,7 @@ public class ConfiguredObjectToMapConverterTest extends TestCase
Map<String, ConfiguredObjectAttribute<?, ?>> attributeTypes = typeRegistry.getAttributeTypes(TestChild.class);
ConfiguredObjectAttribute secureAttribute = mock(ConfiguredObjectAttribute.class);
when(secureAttribute.isSecure()).thenReturn(true);
+ when(secureAttribute.isSecureValue(any())).thenReturn(true);
when(attributeTypes.get(eq("secureAttribute"))).thenReturn(secureAttribute);
TestChild mockChild = mock(TestChild.class);
diff --git a/qpid/java/broker-plugins/management-http/src/test/resources/META-INF/services/org.apache.qpid.server.management.plugin.report.QueueReport b/qpid/java/broker-plugins/management-http/src/test/resources/META-INF/services/org.apache.qpid.server.management.plugin.report.QueueReport
new file mode 100644
index 0000000000..7d25ec4378
--- /dev/null
+++ b/qpid/java/broker-plugins/management-http/src/test/resources/META-INF/services/org.apache.qpid.server.management.plugin.report.QueueReport
@@ -0,0 +1,2 @@
+org.apache.qpid.server.management.plugin.report.TestTextReport
+org.apache.qpid.server.management.plugin.report.TestBinaryReport