From b1ea572f8eee4b54a53dc268a1c9136f6d1891c8 Mon Sep 17 00:00:00 2001 From: Clebert Suconic Date: Mon, 24 Oct 2016 11:57:16 -0400 Subject: [PATCH] ARTEMIS-735 Fixing code after Rest refactoring --- .../org/apache/activemq/artemis/rest/Jms.java | 174 +++++ .../integration/EmbeddedRestActiveMQ.java | 2 +- .../integration/EmbeddedRestActiveMQJMS.java | 8 +- .../activemq/artemis/rest/test/Embedded.java | 91 +++ .../artemis/rest/test/EmbeddedTest.java | 161 ++++ .../activemq/artemis/rest/test/JMSTest.java | 270 +++++++ .../artemis/rest/test/RepostingTopicTest.java | 688 ++++++++++++++++++ 7 files changed, 1389 insertions(+), 5 deletions(-) create mode 100644 artemis-rest/src/main/java/org/apache/activemq/artemis/rest/Jms.java create mode 100644 artemis-rest/src/test/java/org/apache/activemq/artemis/rest/test/Embedded.java create mode 100644 artemis-rest/src/test/java/org/apache/activemq/artemis/rest/test/EmbeddedTest.java create mode 100644 artemis-rest/src/test/java/org/apache/activemq/artemis/rest/test/JMSTest.java create mode 100644 artemis-rest/src/test/java/org/apache/activemq/artemis/rest/test/RepostingTopicTest.java diff --git a/artemis-rest/src/main/java/org/apache/activemq/artemis/rest/Jms.java b/artemis-rest/src/main/java/org/apache/activemq/artemis/rest/Jms.java new file mode 100644 index 0000000000..e5aed5854d --- /dev/null +++ b/artemis-rest/src/main/java/org/apache/activemq/artemis/rest/Jms.java @@ -0,0 +1,174 @@ +/* + * 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.activemq.artemis.rest; + +import javax.jms.BytesMessage; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.ObjectMessage; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.ext.MessageBodyReader; +import javax.ws.rs.ext.Providers; +import java.io.ByteArrayInputStream; +import java.lang.reflect.Type; + +import org.apache.activemq.artemis.rest.util.HttpMessageHelper; +import org.jboss.resteasy.core.Headers; +import org.jboss.resteasy.spi.ResteasyProviderFactory; +import org.jboss.resteasy.util.GenericType; + +public class Jms { + + /** + * Set a JMS Message property to the value of an HTTP header + * + * @param message + * @param name + * @param value + */ + public static void setHttpHeader(Message message, String name, String value) { + try { + message.setStringProperty(HttpHeaderProperty.toPropertyName(name), value); + } catch (JMSException e) { + throw new RuntimeException(e); + } + } + + /** + * Get an HTTP header value from a JMS Message + * + * @param message + * @param name + * @return the header or {@code null} if not present + */ + public static String getHttpHeader(Message message, String name) { + try { + return message.getStringProperty(HttpHeaderProperty.toPropertyName(name)); + } catch (JMSException e) { + throw new RuntimeException(e); + } + } + + /** + * Extract an object using a built-in RESTEasy JAX-RS MessageBodyReader + * + * @param message + * @param type + * @param + * @return + */ + public static T getEntity(Message message, Class type) { + return getEntity(message, type, null, ResteasyProviderFactory.getInstance()); + } + + /** + * Extract an object using a built-in RESTEasy JAX-RS MessageBodyReader + * + * @param message + * @param type + * @param factory + * @param + * @return + */ + public static T getEntity(Message message, Class type, ResteasyProviderFactory factory) { + return getEntity(message, type, null, factory); + } + + /** + * Extract an object using a built-in RESTEasy JAX-RS MessageBodyReader + * + * @param message + * @param type + * @param factory + * @param + * @return + * @throws UnknownMediaType + * @throws UnmarshalException + */ + public static T getEntity(Message message, + GenericType type, + ResteasyProviderFactory factory) throws UnknownMediaType { + return getEntity(message, type.getType(), type.getGenericType(), factory); + } + + public static boolean isHttpMessage(Message message) { + try { + return message.getBooleanProperty(HttpMessageHelper.POSTED_AS_HTTP_MESSAGE); + } catch (JMSException e) { + return false; + } + } + + /** + * Extract an object using a built-in RESTEasy JAX-RS MessageBodyReader + * + * @param message + * @param type + * @param genericType + * @param factory + * @param + * @return + * @throws UnknownMediaType + * @throws UnmarshalException + */ + public static T getEntity(Message message, + Class type, + Type genericType, + ResteasyProviderFactory factory) throws UnknownMediaType { + if (!isHttpMessage(message)) { + try { + return (T) ((ObjectMessage) message).getObject(); + } catch (JMSException e) { + throw new RuntimeException(e); + } + } + BytesMessage bytesMessage = (BytesMessage) message; + + try { + long size = bytesMessage.getBodyLength(); + if (size <= 0) { + return null; + } + + byte[] body = new byte[(int) size]; + bytesMessage.readBytes(body); + + String contentType = message.getStringProperty(HttpHeaderProperty.CONTENT_TYPE); + if (contentType == null) { + throw new UnknownMediaType("Message did not have a Content-Type header cannot extract entity"); + } + MediaType ct = MediaType.valueOf(contentType); + MessageBodyReader reader = factory.getMessageBodyReader(type, genericType, null, ct); + if (reader == null) { + throw new UnmarshalException("Unable to find a JAX-RS reader for type " + type.getName() + " and media type " + contentType); + } + + Providers current = ResteasyProviderFactory.getContextData(Providers.class); + ResteasyProviderFactory.pushContext(Providers.class, factory); + try { + return reader.readFrom(type, genericType, null, ct, new Headers(), new ByteArrayInputStream(body)); + } finally { + ResteasyProviderFactory.popContextData(Providers.class); + if (current != null) + ResteasyProviderFactory.pushContext(Providers.class, current); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + } + +} diff --git a/artemis-rest/src/main/java/org/apache/activemq/artemis/rest/integration/EmbeddedRestActiveMQ.java b/artemis-rest/src/main/java/org/apache/activemq/artemis/rest/integration/EmbeddedRestActiveMQ.java index 9b64591c4a..fff8a444d3 100644 --- a/artemis-rest/src/main/java/org/apache/activemq/artemis/rest/integration/EmbeddedRestActiveMQ.java +++ b/artemis-rest/src/main/java/org/apache/activemq/artemis/rest/integration/EmbeddedRestActiveMQ.java @@ -41,7 +41,7 @@ class EmbeddedRestActiveMQ { embeddedActiveMQ = new EmbeddedActiveMQ(); } - MessageServiceManager getManager() { + public MessageServiceManager getManager() { return manager; } diff --git a/artemis-rest/src/main/java/org/apache/activemq/artemis/rest/integration/EmbeddedRestActiveMQJMS.java b/artemis-rest/src/main/java/org/apache/activemq/artemis/rest/integration/EmbeddedRestActiveMQJMS.java index bd8541c645..550be7ded6 100644 --- a/artemis-rest/src/main/java/org/apache/activemq/artemis/rest/integration/EmbeddedRestActiveMQJMS.java +++ b/artemis-rest/src/main/java/org/apache/activemq/artemis/rest/integration/EmbeddedRestActiveMQJMS.java @@ -20,9 +20,9 @@ import org.apache.activemq.artemis.jms.client.ConnectionFactoryOptions; import org.apache.activemq.artemis.jms.server.embedded.EmbeddedJMS; import org.apache.activemq.artemis.spi.core.naming.BindingRegistry; -class EmbeddedRestActiveMQJMS extends EmbeddedRestActiveMQ { +public class EmbeddedRestActiveMQJMS extends EmbeddedRestActiveMQ { - EmbeddedRestActiveMQJMS(ConnectionFactoryOptions jmsOptions) { + public EmbeddedRestActiveMQJMS(ConnectionFactoryOptions jmsOptions) { super(jmsOptions); } @@ -31,11 +31,11 @@ class EmbeddedRestActiveMQJMS extends EmbeddedRestActiveMQ { embeddedActiveMQ = new EmbeddedJMS(); } - BindingRegistry getRegistry() { + public BindingRegistry getRegistry() { return ((EmbeddedJMS) embeddedActiveMQ).getRegistry(); } - EmbeddedJMS getEmbeddedJMS() { + public EmbeddedJMS getEmbeddedJMS() { return (EmbeddedJMS) embeddedActiveMQ; } } diff --git a/artemis-rest/src/test/java/org/apache/activemq/artemis/rest/test/Embedded.java b/artemis-rest/src/test/java/org/apache/activemq/artemis/rest/test/Embedded.java new file mode 100644 index 0000000000..67a7a53110 --- /dev/null +++ b/artemis-rest/src/test/java/org/apache/activemq/artemis/rest/test/Embedded.java @@ -0,0 +1,91 @@ +/* + * 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.activemq.artemis.rest.test; + +import org.apache.activemq.artemis.api.core.TransportConfiguration; +import org.apache.activemq.artemis.core.config.Configuration; +import org.apache.activemq.artemis.core.config.impl.ConfigurationImpl; +import org.apache.activemq.artemis.core.remoting.impl.invm.InVMAcceptorFactory; +import org.apache.activemq.artemis.core.server.ActiveMQServer; +import org.apache.activemq.artemis.core.server.ActiveMQServers; +import org.apache.activemq.artemis.rest.MessageServiceConfiguration; +import org.apache.activemq.artemis.rest.MessageServiceManager; +import org.jboss.resteasy.plugins.server.tjws.TJWSEmbeddedJaxrsServer; +import org.jboss.resteasy.test.TestPortProvider; + +public class Embedded { + + protected MessageServiceManager manager = new MessageServiceManager(null); + protected MessageServiceConfiguration config = new MessageServiceConfiguration(); + protected ActiveMQServer activeMQServer; + protected TJWSEmbeddedJaxrsServer tjws = new TJWSEmbeddedJaxrsServer(); + + public Embedded() { + int port = TestPortProvider.getPort(); + System.out.println("default port is: " + port); + tjws.setPort(port); + tjws.setRootResourcePath(""); + tjws.setSecurityDomain(null); + } + + public MessageServiceConfiguration getConfig() { + return config; + } + + public void setConfig(MessageServiceConfiguration config) { + this.config = config; + } + + public ActiveMQServer getActiveMQServer() { + return activeMQServer; + } + + public void setActiveMQServer(ActiveMQServer activeMQServer) { + this.activeMQServer = activeMQServer; + } + + public TJWSEmbeddedJaxrsServer getJaxrsServer() { + return tjws; + } + + public MessageServiceManager getManager() { + return manager; + } + + public void start() throws Exception { + System.out.println("\nStarting Embedded"); + if (activeMQServer == null) { + Configuration configuration = new ConfigurationImpl().setPersistenceEnabled(false).setSecurityEnabled(false).addAcceptorConfiguration(new TransportConfiguration(InVMAcceptorFactory.class.getName())); + + activeMQServer = ActiveMQServers.newActiveMQServer(configuration); + activeMQServer.start(); + } + tjws.start(); + manager.setConfiguration(config); + manager.start(); + tjws.getDeployment().getRegistry().addSingletonResource(manager.getQueueManager().getDestination()); + tjws.getDeployment().getRegistry().addSingletonResource(manager.getTopicManager().getDestination()); + + } + + public void stop() throws Exception { + System.out.println("\nStopping Embedded"); + manager.stop(); + tjws.stop(); + activeMQServer.stop(); + } +} diff --git a/artemis-rest/src/test/java/org/apache/activemq/artemis/rest/test/EmbeddedTest.java b/artemis-rest/src/test/java/org/apache/activemq/artemis/rest/test/EmbeddedTest.java new file mode 100644 index 0000000000..dea9c0e0ed --- /dev/null +++ b/artemis-rest/src/test/java/org/apache/activemq/artemis/rest/test/EmbeddedTest.java @@ -0,0 +1,161 @@ +/* + * 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.activemq.artemis.rest.test; + +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.Destination; +import javax.jms.MessageProducer; +import javax.jms.ObjectMessage; +import javax.jms.Session; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +import org.apache.activemq.artemis.api.jms.JMSFactoryType; +import org.apache.activemq.artemis.core.config.impl.SecurityConfiguration; +import org.apache.activemq.artemis.rest.HttpHeaderProperty; +import org.apache.activemq.artemis.rest.integration.EmbeddedRestActiveMQJMS; +import org.apache.activemq.artemis.spi.core.naming.BindingRegistry; +import org.apache.activemq.artemis.spi.core.security.ActiveMQJAASSecurityManager; +import org.apache.activemq.artemis.spi.core.security.jaas.InVMLoginModule; +import org.jboss.resteasy.client.ClientRequest; +import org.jboss.resteasy.client.ClientResponse; +import org.jboss.resteasy.spi.Link; +import org.jboss.resteasy.test.TestPortProvider; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +public class EmbeddedTest { + + public static EmbeddedRestActiveMQJMS server; + + @BeforeClass + public static void startEmbedded() throws Exception { + server = new EmbeddedRestActiveMQJMS(null); + server.getManager().setConfigResourcePath("activemq-rest.xml"); + SecurityConfiguration securityConfiguration = new SecurityConfiguration(); + securityConfiguration.addUser("guest", "guest"); + securityConfiguration.addRole("guest", "guest"); + securityConfiguration.setDefaultUser("guest"); + ActiveMQJAASSecurityManager securityManager = new ActiveMQJAASSecurityManager(InVMLoginModule.class.getName(), securityConfiguration); + server.getEmbeddedJMS().setSecurityManager(securityManager); + server.start(); + List connectors = new ArrayList<>(); + connectors.add("in-vm"); + server.getEmbeddedJMS().getJMSServerManager().createConnectionFactory("ConnectionFactory", false, JMSFactoryType.CF, connectors, "ConnectionFactory"); + } + + @AfterClass + public static void stopEmbedded() throws Exception { + server.stop(); + server = null; + } + + public static void publish(String destination, Serializable object, String contentType) throws Exception { + BindingRegistry reg = server.getRegistry(); + ConnectionFactory factory = (ConnectionFactory) reg.lookup("ConnectionFactory"); + Connection conn = factory.createConnection(); + Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); + Destination dest = session.createQueue(destination); + + try { + Assert.assertNotNull("Destination was null", dest); + MessageProducer producer = session.createProducer(dest); + ObjectMessage message = session.createObjectMessage(); + + if (contentType != null) { + message.setStringProperty(HttpHeaderProperty.CONTENT_TYPE, contentType); + } + message.setObject(object); + + producer.send(message); + } finally { + conn.close(); + } + } + + @Test + public void testTransform() throws Exception { + + ClientRequest request = new ClientRequest(TestPortProvider.generateURL("/queues/jms.queue.exampleQueue")); + + ClientResponse response = request.head(); + response.releaseConnection(); + Assert.assertEquals(200, response.getStatus()); + Link sender = response.getLinkHeader().getLinkByTitle("create"); + System.out.println("create: " + sender); + Link consumers = response.getLinkHeader().getLinkByTitle("pull-consumers"); + System.out.println("pull: " + consumers); + response = Util.setAutoAck(consumers, true); + Link consumeNext = response.getLinkHeader().getLinkByTitle("consume-next"); + System.out.println("consume-next: " + consumeNext); + + // test that Accept header is used to set content-type + { + TransformTest.Order order = new TransformTest.Order(); + order.setName("1"); + order.setAmount("$5.00"); + publish("exampleQueue", order, null); + + ClientResponse res = consumeNext.request().header("Accept-Wait", "2").accept("application/xml").post(String.class); + Assert.assertEquals(200, res.getStatus()); + Assert.assertEquals("application/xml", res.getHeaders().getFirst("Content-Type").toString().toLowerCase()); + TransformTest.Order order2 = res.getEntity(TransformTest.Order.class); + Assert.assertEquals(order, order2); + consumeNext = res.getLinkHeader().getLinkByTitle("consume-next"); + res.releaseConnection(); + Assert.assertNotNull(consumeNext); + } + + // test that Accept header is used to set content-type + { + TransformTest.Order order = new TransformTest.Order(); + order.setName("1"); + order.setAmount("$5.00"); + publish("exampleQueue", order, null); + + ClientResponse res = consumeNext.request().header("Accept-Wait", "2").accept("application/json").post(String.class); + Assert.assertEquals(200, res.getStatus()); + Assert.assertEquals("application/json", res.getHeaders().getFirst("Content-Type").toString().toLowerCase()); + TransformTest.Order order2 = res.getEntity(TransformTest.Order.class); + Assert.assertEquals(order, order2); + consumeNext = res.getLinkHeader().getLinkByTitle("consume-next"); + res.releaseConnection(); + Assert.assertNotNull(consumeNext); + } + + // test that message property is used to set content type + { + TransformTest.Order order = new TransformTest.Order(); + order.setName("2"); + order.setAmount("$15.00"); + publish("exampleQueue", order, "application/xml"); + + ClientResponse res = consumeNext.request().header("Accept-Wait", "2").post(String.class); + Assert.assertEquals(200, res.getStatus()); + Assert.assertEquals("application/xml", res.getHeaders().getFirst("Content-Type").toString().toLowerCase()); + TransformTest.Order order2 = res.getEntity(TransformTest.Order.class); + Assert.assertEquals(order, order2); + consumeNext = res.getLinkHeader().getLinkByTitle("consume-next"); + res.releaseConnection(); + Assert.assertNotNull(consumeNext); + } + } +} diff --git a/artemis-rest/src/test/java/org/apache/activemq/artemis/rest/test/JMSTest.java b/artemis-rest/src/test/java/org/apache/activemq/artemis/rest/test/JMSTest.java new file mode 100644 index 0000000000..c3228ad81e --- /dev/null +++ b/artemis-rest/src/test/java/org/apache/activemq/artemis/rest/test/JMSTest.java @@ -0,0 +1,270 @@ +/* + * 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.activemq.artemis.rest.test; + +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.Destination; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageListener; +import javax.jms.MessageProducer; +import javax.jms.ObjectMessage; +import javax.jms.Session; +import javax.xml.bind.annotation.XmlRootElement; +import java.io.Serializable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.apache.activemq.artemis.jms.client.ActiveMQDestination; +import org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory; +import org.apache.activemq.artemis.rest.HttpHeaderProperty; +import org.apache.activemq.artemis.rest.Jms; +import org.apache.activemq.artemis.rest.queue.QueueDeployment; +import org.jboss.resteasy.client.ClientRequest; +import org.jboss.resteasy.client.ClientResponse; +import org.jboss.resteasy.spi.Link; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.jboss.resteasy.test.TestPortProvider.generateURL; + +public class JMSTest extends MessageTestBase { + + public static ConnectionFactory connectionFactory; + + @BeforeClass + public static void setup() throws Exception { + connectionFactory = new ActiveMQJMSConnectionFactory(manager.getQueueManager().getServerLocator()); + } + + @XmlRootElement + public static class Order implements Serializable { + + private static final long serialVersionUID = 1397854679589606480L; + private String name; + private String amount; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAmount() { + return amount; + } + + public void setAmount(String amount) { + this.amount = amount; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + Order order = (Order) o; + + if (!amount.equals(order.amount)) { + return false; + } + if (!name.equals(order.name)) { + return false; + } + + return true; + } + + @Override + public int hashCode() { + int result = name.hashCode(); + result = 31 * result + amount.hashCode(); + return result; + } + } + + public static Destination createDestination(String dest) { + ActiveMQDestination destination = (ActiveMQDestination) ActiveMQDestination.fromAddress(dest); + System.out.println("SimpleAddress: " + destination.getSimpleAddress()); + return destination; + } + + public static void publish(String dest, Serializable object, String contentType) throws Exception { + Connection conn = connectionFactory.createConnection(); + try { + Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); + Destination destination = createDestination(dest); + MessageProducer producer = session.createProducer(destination); + ObjectMessage message = session.createObjectMessage(); + + if (contentType != null) { + message.setStringProperty(HttpHeaderProperty.CONTENT_TYPE, contentType); + } + message.setObject(object); + + producer.send(message); + } finally { + conn.close(); + } + } + + public static class Listener implements MessageListener { + + public static Order order; + public static String messageID = null; + public static CountDownLatch latch = new CountDownLatch(1); + + @Override + public void onMessage(Message message) { + try { + order = Jms.getEntity(message, Order.class); + messageID = message.getJMSMessageID(); + } catch (Exception e) { + e.printStackTrace(); + } + latch.countDown(); + } + } + + @Test + public void testJmsConsumer() throws Exception { + String queueName = ActiveMQDestination.createQueueAddressFromName("testQueue2").toString(); + System.out.println("Queue name: " + queueName); + QueueDeployment deployment = new QueueDeployment(); + deployment.setDuplicatesAllowed(true); + deployment.setDurableSend(false); + deployment.setName(queueName); + manager.getQueueManager().deploy(deployment); + Connection conn = connectionFactory.createConnection(); + try { + Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); + Destination destination = createDestination(queueName); + MessageConsumer consumer = session.createConsumer(destination); + consumer.setMessageListener(new Listener()); + conn.start(); + + ClientRequest request = new ClientRequest(generateURL(Util.getUrlPath(queueName))); + + ClientResponse response = request.head(); + response.releaseConnection(); + Assert.assertEquals(200, response.getStatus()); + Link sender = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "create"); + System.out.println("create: " + sender); + Link consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consume-next"); + System.out.println("consume-next: " + consumeNext); + + // test that Accept header is used to set content-type + { + Order order = new Order(); + order.setName("1"); + order.setAmount("$5.00"); + response = sender.request().body("application/xml", order).post(); + response.releaseConnection(); + Assert.assertEquals(201, response.getStatus()); + + Listener.latch.await(1, TimeUnit.SECONDS); + Assert.assertNotNull(Listener.order); + Assert.assertEquals(order, Listener.order); + Assert.assertNotNull(Listener.messageID); + } + } finally { + conn.close(); + } + } + + @Test + public void testJmsProducer() throws Exception { + String queueName = ActiveMQDestination.createQueueAddressFromName("testQueue").toString(); + System.out.println("Queue name: " + queueName); + QueueDeployment deployment = new QueueDeployment(); + deployment.setDuplicatesAllowed(true); + deployment.setDurableSend(false); + deployment.setName(queueName); + manager.getQueueManager().deploy(deployment); + ClientRequest request = new ClientRequest(generateURL(Util.getUrlPath(queueName))); + + ClientResponse response = request.head(); + response.releaseConnection(); + Assert.assertEquals(200, response.getStatus()); + Link sender = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "create"); + System.out.println("create: " + sender); + Link consumers = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "pull-consumers"); + System.out.println("pull: " + consumers); + response = Util.setAutoAck(consumers, true); + Link consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consume-next"); + System.out.println("consume-next: " + consumeNext); + + // test that Accept header is used to set content-type + { + Order order = new Order(); + order.setName("1"); + order.setAmount("$5.00"); + publish(queueName, order, null); + + ClientResponse res = consumeNext.request().header("Accept-Wait", "2").accept("application/xml").post(String.class); + Assert.assertEquals(200, res.getStatus()); + Assert.assertEquals("application/xml", res.getHeaders().getFirst("Content-Type").toString().toLowerCase()); + Order order2 = res.getEntity(Order.class); + res.releaseConnection(); + Assert.assertEquals(order, order2); + consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), res, "consume-next"); + Assert.assertNotNull(consumeNext); + } + + // test that Accept header is used to set content-type + { + Order order = new Order(); + order.setName("1"); + order.setAmount("$5.00"); + publish(queueName, order, null); + + ClientResponse res = consumeNext.request().header("Accept-Wait", "2").accept("application/json").post(String.class); + Assert.assertEquals(200, res.getStatus()); + Assert.assertEquals("application/json", res.getHeaders().getFirst("Content-Type").toString().toLowerCase()); + Order order2 = res.getEntity(Order.class); + res.releaseConnection(); + Assert.assertEquals(order, order2); + consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), res, "consume-next"); + Assert.assertNotNull(consumeNext); + } + + // test that message property is used to set content type + { + Order order = new Order(); + order.setName("2"); + order.setAmount("$15.00"); + publish(queueName, order, "application/xml"); + + ClientResponse res = consumeNext.request().header("Accept-Wait", "2").post(String.class); + Assert.assertEquals(200, res.getStatus()); + Assert.assertEquals("application/xml", res.getHeaders().getFirst("Content-Type").toString().toLowerCase()); + Order order2 = res.getEntity(Order.class); + res.releaseConnection(); + Assert.assertEquals(order, order2); + consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), res, "consume-next"); + Assert.assertNotNull(consumeNext); + } + } +} diff --git a/artemis-rest/src/test/java/org/apache/activemq/artemis/rest/test/RepostingTopicTest.java b/artemis-rest/src/test/java/org/apache/activemq/artemis/rest/test/RepostingTopicTest.java new file mode 100644 index 0000000000..28b9132e6a --- /dev/null +++ b/artemis-rest/src/test/java/org/apache/activemq/artemis/rest/test/RepostingTopicTest.java @@ -0,0 +1,688 @@ +/* + * 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.activemq.artemis.rest.test; + +import org.apache.activemq.artemis.rest.topic.TopicDeployment; +import org.apache.activemq.artemis.rest.util.Constants; +import org.jboss.resteasy.client.ClientRequest; +import org.jboss.resteasy.client.ClientResponse; +import org.jboss.resteasy.spi.Link; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.jboss.resteasy.test.TestPortProvider.generateURL; + +/** + * repost on same consume-next + * repost on old consume-next + * repost on same consume-next with timeouts + * repost on same ack-next + * repost successful ack + * repost successful unack + * repost ack after unack + * repost unack after ack + * post on old ack-next + * post on old ack-next after an ack + * ack with an old ack link + */ +public class RepostingTopicTest extends MessageTestBase { + + @BeforeClass + public static void setup() throws Exception { + TopicDeployment deployment1 = new TopicDeployment("testTopic", true); + manager.getTopicManager().deploy(deployment1); + } + + @Test + public void testReconnectOnNamedSubscriber() throws Exception { + ClientRequest request = new ClientRequest(generateURL("/topics/testTopic")); + + ClientResponse response = request.head(); + response.releaseConnection(); + Assert.assertEquals(200, response.getStatus()); + Link sender = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "create"); + System.out.println("create: " + sender); + Link consumers = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "pull-subscriptions"); + System.out.println("pull: " + consumers); + response = consumers.request().formParameter("name", "bill").post(); + response.releaseConnection(); + + response = sender.request().body("text/plain", Integer.toString(1)).post(); + response.releaseConnection(); + Assert.assertEquals(201, response.getStatus()); + response = sender.request().body("text/plain", Integer.toString(2)).post(); + response.releaseConnection(); + Assert.assertEquals(201, response.getStatus()); + + // recreate subscription a second time as named. Should pick up old one. + + response = consumers.request().formParameter("name", "bill").post(); + response.releaseConnection(); + Link consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consume-next"); + System.out.println("resource consume-next: " + consumeNext); + response = consumeNext.request().post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("1", response.getEntity(String.class)); + response.releaseConnection(); + consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consume-next"); + response = consumeNext.request().header("Accept-Wait", "2").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("2", response.getEntity(String.class)); + response.releaseConnection(); + + Link session = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consumer"); + response = session.request().delete(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + } + + @Test + public void testRestartOnDurableNamedSubscriber() throws Exception { + ClientRequest request = new ClientRequest(generateURL("/topics/testTopic")); + + ClientResponse response = request.head(); + response.releaseConnection(); + Assert.assertEquals(200, response.getStatus()); + Link sender = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "create"); + System.out.println("create: " + sender); + Link consumers = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "pull-subscriptions"); + System.out.println("pull: " + consumers); + response = consumers.request().formParameter("name", "bill").formParameter("durable", "true").post(); + response.releaseConnection(); + + response = sender.request().body("text/plain", Integer.toString(1)).post(); + response.releaseConnection(); + Assert.assertEquals(201, response.getStatus()); + response = sender.request().body("text/plain", Integer.toString(2)).post(); + response.releaseConnection(); + Assert.assertEquals(201, response.getStatus()); + + manager.getTopicManager().getDestination().findTopic("testTopic").getSubscriptions().stop(); + + // recreate subscription a second time as named. Should pick up old one. + + response = consumers.request().formParameter("name", "bill").formParameter("durable", "true").post(); + response.releaseConnection(); + Link consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consume-next"); + System.out.println("resource consume-next: " + consumeNext); + response = consumeNext.request().header("Accept-Wait", "2").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("1", response.getEntity(String.class)); + response.releaseConnection(); + consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consume-next"); + response = consumeNext.request().header("Accept-Wait", "2").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("2", response.getEntity(String.class)); + response.releaseConnection(); + + Link session = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consumer"); + response = session.request().delete(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + } + + @Test + public void testRestartOnNonDurableNamedSubscriber() throws Exception { + ClientRequest request = new ClientRequest(generateURL("/topics/testTopic")); + + ClientResponse response = request.head(); + response.releaseConnection(); + Assert.assertEquals(200, response.getStatus()); + Link sender = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "create"); + System.out.println("create: " + sender); + Link consumers = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "pull-subscriptions"); + System.out.println("pull: " + consumers); + response = consumers.request().formParameter("name", "bill").post(); + response.releaseConnection(); + + response = sender.request().body("text/plain", Integer.toString(1)).post(); + response.releaseConnection(); + Assert.assertEquals(201, response.getStatus()); + response = sender.request().body("text/plain", Integer.toString(2)).post(); + response.releaseConnection(); + Assert.assertEquals(201, response.getStatus()); + + manager.getTopicManager().getDestination().findTopic("testTopic").getSubscriptions().stop(); + + // recreate subscription a second time as named. Should pick up old one. + + response = consumers.request().formParameter("name", "bill").post(); + response.releaseConnection(); + Link consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consume-next"); + System.out.println("resource consume-next: " + consumeNext); + response = consumeNext.request().header("Accept-Wait", "2").post(String.class); + response.releaseConnection(); + Assert.assertEquals(503, response.getStatus()); + + Link session = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consumer"); + response = session.request().delete(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + } + + @Test + public void testPostOnSameConsumeNext() throws Exception { + ClientRequest request = new ClientRequest(generateURL("/topics/testTopic")); + + ClientResponse response = request.head(); + response.releaseConnection(); + Assert.assertEquals(200, response.getStatus()); + Link sender = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "create"); + System.out.println("create: " + sender); + Link consumers = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "pull-subscriptions"); + System.out.println("pull: " + consumers); + response = Util.setAutoAck(consumers, true); + Link consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consume-next"); + System.out.println("resource consume-next: " + consumeNext); + + response = sender.request().body("text/plain", Integer.toString(1)).post(); + response.releaseConnection(); + Assert.assertEquals(201, response.getStatus()); + + response = consumeNext.request().post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("1", response.getEntity(String.class)); + response.releaseConnection(); + Link session = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consumer"); + System.out.println("session: " + session); + consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consume-next"); + System.out.println("session 1st consumeNext: " + consumeNext); + + response = sender.request().body("text/plain", Integer.toString(2)).post(); + response.releaseConnection(); + Assert.assertEquals(201, response.getStatus()); + + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("2", response.getEntity(String.class)); + response.releaseConnection(); + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("2", response.getEntity(String.class)); + response.releaseConnection(); + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("2", response.getEntity(String.class)); + response.releaseConnection(); + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("2", response.getEntity(String.class)); + response.releaseConnection(); + + session = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consumer"); + System.out.println("session: " + session); + consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consume-next"); + System.out.println("session 2nd consumeNext: " + consumeNext); + + response = sender.request().body("text/plain", Integer.toString(3)).post(); + response.releaseConnection(); + Assert.assertEquals(201, response.getStatus()); + + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("3", response.getEntity(String.class)); + response.releaseConnection(); + + response = session.request().delete(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + } + + @Test + public void testPostOnOldConsumeNext() throws Exception { + ClientRequest request = new ClientRequest(generateURL("/topics/testTopic")); + + ClientResponse response = request.head(); + response.releaseConnection(); + Assert.assertEquals(200, response.getStatus()); + Link sender = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "create"); + System.out.println("create: " + sender); + Link consumers = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "pull-subscriptions"); + System.out.println("pull: " + consumers); + response = Util.setAutoAck(consumers, true); + Link consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consume-next"); + System.out.println("resource consume-next: " + consumeNext); + + response = sender.request().body("text/plain", Integer.toString(1)).post(); + response.releaseConnection(); + Assert.assertEquals(201, response.getStatus()); + + response = consumeNext.request().post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("1", response.getEntity(String.class)); + response.releaseConnection(); + Link session = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consumer"); + System.out.println("session: " + session); + consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consume-next"); + Link firstConsumeNext = consumeNext; + System.out.println("session 1st consumeNext: " + consumeNext); + + response = sender.request().body("text/plain", Integer.toString(2)).post(); + response.releaseConnection(); + Assert.assertEquals(201, response.getStatus()); + + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("2", response.getEntity(String.class)); + response.releaseConnection(); + session = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consumer"); + System.out.println("session: " + session); + consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consume-next"); + System.out.println("session 2nd consumeNext: " + consumeNext); + + response = sender.request().body("text/plain", Integer.toString(3)).post(); + response.releaseConnection(); + Assert.assertEquals(201, response.getStatus()); + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("3", response.getEntity(String.class)); + response.releaseConnection(); + + response = firstConsumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(412, response.getStatus()); + System.out.println(response.getEntity(String.class)); + response.releaseConnection(); + + response = session.request().delete(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + } + + @Test + public void testPostOnSameConsumeNextWithTimeout() throws Exception { + ClientRequest request = new ClientRequest(generateURL("/topics/testTopic")); + + ClientResponse response = request.head(); + response.releaseConnection(); + Assert.assertEquals(200, response.getStatus()); + Link sender = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "create"); + System.out.println("create: " + sender); + Link consumers = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "pull-subscriptions"); + System.out.println("pull: " + consumers); + response = Util.setAutoAck(consumers, true); + Link consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consume-next"); + System.out.println("resource consume-next: " + consumeNext); + + response = sender.request().body("text/plain", Integer.toString(1)).post(); + response.releaseConnection(); + Assert.assertEquals(201, response.getStatus()); + + response = consumeNext.request().post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("1", response.getEntity(String.class)); + response.releaseConnection(); + Link session = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consumer"); + System.out.println("session: " + session); + consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consume-next"); + System.out.println("session 1st consumeNext: " + consumeNext); + + // test timeout here + response = consumeNext.request().post(String.class); + response.releaseConnection(); + Assert.assertEquals(503, response.getStatus()); + session = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consumer"); + System.out.println("session: " + session); + consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consume-next"); + System.out.println("session 2nd consumeNext: " + consumeNext); + + response = sender.request().body("text/plain", Integer.toString(3)).post(); + response.releaseConnection(); + Assert.assertEquals(201, response.getStatus()); + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("3", response.getEntity(String.class)); + response.releaseConnection(); + + response = session.request().delete(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + } + + @Test + public void testPostOnSameAcknowledgeNextAndAck() throws Exception { + ClientRequest request = new ClientRequest(generateURL("/topics/testTopic")); + + ClientResponse response = request.head(); + response.releaseConnection(); + Assert.assertEquals(200, response.getStatus()); + Link sender = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "create"); + System.out.println("create: " + sender); + Link consumers = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "pull-subscriptions"); + System.out.println("pull: " + consumers); + response = Util.setAutoAck(consumers, false); + Link consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "acknowledge-next"); + System.out.println("resource acknowledge-next: " + consumeNext); + + response = sender.request().body("text/plain", Integer.toString(1)).post(); + response.releaseConnection(); + Assert.assertEquals(201, response.getStatus()); + + response = consumeNext.request().post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("1", response.getEntity(String.class)); + response.releaseConnection(); + Link session = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consumer"); + System.out.println("session: " + session); + Link ack = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "acknowledgement"); + System.out.println("ack: " + ack); + response = ack.request().formParameter("acknowledge", "true").post(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + response = ack.request().formParameter("acknowledge", "true").post(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + response = ack.request().formParameter("acknowledge", "true").post(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "acknowledge-next"); + System.out.println("session 1st acknowledge-next: " + consumeNext); + + response = sender.request().body("text/plain", Integer.toString(2)).post(); + response.releaseConnection(); + Assert.assertEquals(201, response.getStatus()); + + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("2", response.getEntity(String.class)); + response.releaseConnection(); + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("2", response.getEntity(String.class)); + response.releaseConnection(); + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("2", response.getEntity(String.class)); + response.releaseConnection(); + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("2", response.getEntity(String.class)); + response.releaseConnection(); + ack = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "acknowledgement"); + System.out.println("ack: " + ack); + response = ack.request().formParameter("acknowledge", "true").post(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + + response = session.request().delete(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + } + + @Test + public void testRepostSuccessfulUnacknowledge() throws Exception { + ClientRequest request = new ClientRequest(generateURL("/topics/testTopic")); + + ClientResponse response = request.head(); + response.releaseConnection(); + Assert.assertEquals(200, response.getStatus()); + Link sender = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "create"); + System.out.println("create: " + sender); + Link consumers = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "pull-subscriptions"); + System.out.println("pull: " + consumers); + response = Util.setAutoAck(consumers, false); + Link consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "acknowledge-next"); + System.out.println("resource acknowledge-next: " + consumeNext); + + response = sender.request().body("text/plain", Integer.toString(1)).post(); + response.releaseConnection(); + Assert.assertEquals(201, response.getStatus()); + + response = consumeNext.request().post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("1", response.getEntity(String.class)); + response.releaseConnection(); + Link session = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consumer"); + System.out.println("session: " + session); + Link ack = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "acknowledgement"); + System.out.println("ack: " + ack); + response = ack.request().formParameter("acknowledge", "false").post(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + response = ack.request().formParameter("acknowledge", "false").post(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + response = ack.request().formParameter("acknowledge", "false").post(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "acknowledge-next"); + System.out.println("session 1st acknowledge-next: " + consumeNext); + + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("1", response.getEntity(String.class)); + response.releaseConnection(); + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("1", response.getEntity(String.class)); + response.releaseConnection(); + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("1", response.getEntity(String.class)); + response.releaseConnection(); + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("1", response.getEntity(String.class)); + response.releaseConnection(); + ack = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "acknowledgement"); + System.out.println("ack: " + ack); + response = ack.request().formParameter("acknowledge", "true").post(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + + response = session.request().delete(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + } + + @Test + public void testRepostAckAfterUnacknowledge() throws Exception { + ClientRequest request = new ClientRequest(generateURL("/topics/testTopic")); + + ClientResponse response = request.head(); + response.releaseConnection(); + Assert.assertEquals(200, response.getStatus()); + Link sender = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "create"); + System.out.println("create: " + sender); + Link consumers = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "pull-subscriptions"); + System.out.println("pull: " + consumers); + response = Util.setAutoAck(consumers, false); + Link consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "acknowledge-next"); + System.out.println("resource acknowledge-next: " + consumeNext); + + response = sender.request().body("text/plain", Integer.toString(1)).post(); + response.releaseConnection(); + Assert.assertEquals(201, response.getStatus()); + + response = consumeNext.request().post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("1", response.getEntity(String.class)); + response.releaseConnection(); + Link session = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consumer"); + System.out.println("session: " + session); + Link ack = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "acknowledgement"); + System.out.println("ack: " + ack); + response = ack.request().formParameter("acknowledge", "false").post(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + response = ack.request().formParameter("acknowledge", "true").post(); + Assert.assertEquals(412, response.getStatus()); + System.out.println(response.getEntity(String.class)); + response.releaseConnection(); + consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "acknowledge-next"); + System.out.println("session 1st acknowledge-next: " + consumeNext); + + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("1", response.getEntity(String.class)); + response.releaseConnection(); + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("1", response.getEntity(String.class)); + response.releaseConnection(); + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("1", response.getEntity(String.class)); + response.releaseConnection(); + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("1", response.getEntity(String.class)); + response.releaseConnection(); + ack = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "acknowledgement"); + System.out.println("ack: " + ack); + response = ack.request().formParameter("acknowledge", "true").post(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + + response = session.request().delete(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + } + + @Test + public void testRepostUnAckAfterAcknowledge() throws Exception { + ClientRequest request = new ClientRequest(generateURL("/topics/testTopic")); + + ClientResponse response = request.head(); + response.releaseConnection(); + Assert.assertEquals(200, response.getStatus()); + Link sender = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "create"); + System.out.println("create: " + sender); + Link consumers = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "pull-subscriptions"); + System.out.println("pull: " + consumers); + response = Util.setAutoAck(consumers, false); + Link consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "acknowledge-next"); + System.out.println("resource acknowledge-next: " + consumeNext); + + response = sender.request().body("text/plain", Integer.toString(1)).post(); + response.releaseConnection(); + Assert.assertEquals(201, response.getStatus()); + + response = consumeNext.request().post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("1", response.getEntity(String.class)); + response.releaseConnection(); + Link session = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consumer"); + System.out.println("session: " + session); + Link ack = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "acknowledgement"); + System.out.println("ack: " + ack); + response = ack.request().formParameter("acknowledge", "true").post(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + response = ack.request().formParameter("acknowledge", "false").post(); + Assert.assertEquals(412, response.getStatus()); + System.out.println(response.getEntity(String.class)); + response.releaseConnection(); + consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "acknowledge-next"); + System.out.println("session 1st acknowledge-next: " + consumeNext); + + response = sender.request().body("text/plain", Integer.toString(2)).post(); + response.releaseConnection(); + Assert.assertEquals(201, response.getStatus()); + + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("2", response.getEntity(String.class)); + response.releaseConnection(); + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("2", response.getEntity(String.class)); + response.releaseConnection(); + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("2", response.getEntity(String.class)); + response.releaseConnection(); + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("2", response.getEntity(String.class)); + response.releaseConnection(); + ack = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "acknowledgement"); + System.out.println("ack: " + ack); + response = ack.request().formParameter("acknowledge", "true").post(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + + response = session.request().delete(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + } + + @Test + public void testPostOnOldAcknowledgeNextAndAck() throws Exception { + ClientRequest request = new ClientRequest(generateURL("/topics/testTopic")); + + ClientResponse response = request.head(); + response.releaseConnection(); + Assert.assertEquals(200, response.getStatus()); + Link sender = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "create"); + System.out.println("create: " + sender); + Link consumers = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "pull-subscriptions"); + System.out.println("pull: " + consumers); + response = Util.setAutoAck(consumers, false); + Link consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "acknowledge-next"); + System.out.println("resource acknowledge-next: " + consumeNext); + + response = sender.request().body("text/plain", Integer.toString(1)).post(); + response.releaseConnection(); + Assert.assertEquals(201, response.getStatus()); + + response = consumeNext.request().header(Constants.WAIT_HEADER, "1").post(String.class); + Assert.assertEquals(200, response.getStatus()); + Assert.assertEquals("1", response.getEntity(String.class)); + response.releaseConnection(); + Link session = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "consumer"); + System.out.println("session: " + session); + Link ack = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "acknowledgement"); + Link oldAck = ack; + System.out.println("ack: " + ack); + response = ack.request().formParameter("acknowledge", "true").post(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "acknowledge-next"); + System.out.println("session 1st acknowledge-next: " + consumeNext); + Link firstConsumeNext = consumeNext; + + response = sender.request().body("text/plain", Integer.toString(2)).post(); + response.releaseConnection(); + Assert.assertEquals(201, response.getStatus()); + + response = consumeNext.request().header(Constants.WAIT_HEADER, "10").post(String.class); + response.releaseConnection(); + Assert.assertEquals(200, response.getStatus()); + ack = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "acknowledgement"); + System.out.println("ack: " + ack); + + response = oldAck.request().formParameter("acknowledge", "true").post(); + Assert.assertEquals(412, response.getStatus()); + System.out.println(response.getEntity(String.class)); + response.releaseConnection(); + consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "acknowledge-next"); + + response = ack.request().formParameter("acknowledge", "true").post(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + consumeNext = getLinkByTitle(manager.getQueueManager().getLinkStrategy(), response, "acknowledge-next"); + + response = consumeNext.request().post(String.class); + response.releaseConnection(); + Assert.assertEquals(503, response.getStatus()); + + response = session.request().delete(); + response.releaseConnection(); + Assert.assertEquals(204, response.getStatus()); + } +}