import org.apache.activemq.artemis.core.config.impl.ConfigurationImpl;
import org.apache.activemq.artemis.core.config.impl.RoleSet;
import org.apache.activemq.artemis.core.config.impl.SecurityConfiguration;
import org.apache.activemq.artemis.core.security.Role;
import org.apache.activemq.artemis.core.server.embedded.EmbeddedActiveMQ;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.activemq.artemis.spi.core.security.ActiveMQJAASSecurityManager;
import org.apache.activemq.artemis.spi.core.security.jaas.InVMLoginModule;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
public class AMQUnitTest {
@BeforeAll
public static void setup() throws Exception {
EmbeddedActiveMQ embeddedActiveMQ = new EmbeddedActiveMQ();
ConfigurationImpl configuration = new ConfigurationImpl();
RoleSet roles1 = new RoleSet();
roles1.add(new Role("test",true,true,true,true,true,true,true,true,true,true));
RoleSet roles2 = new RoleSet();
roles2.add(new Role("test",false,false,true,true,true,true,true,true,true,true));
configuration.addSecurityRole("foo::q1", roles1);
configuration.addSecurityRole("foo", roles2);
configuration.setSecurityEnabled(true);
configuration.addAcceptorConfiguration("artemis","tcp:);
configuration.setMaxDiskUsage(100);
embeddedActiveMQ.setConfiguration(configuration);
ActiveMQJAASSecurityManager securityManager = new ActiveMQJAASSecurityManager(InVMLoginModule.class.getName());
SecurityConfiguration securityConfiguration = new SecurityConfiguration();
securityConfiguration.addUser("test","test");
securityConfiguration.addRole("test","test");
securityConfiguration.getAppConfigurationEntry("activemq");
securityManager.setConfiguration(securityConfiguration);
embeddedActiveMQ.setSecurityManager(securityManager);
embeddedActiveMQ.start();
}
@Test
public void sendMessage() throws Exception {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
connectionFactory.setBrokerURL("tcp:);
connectionFactory.setUser("test");
connectionFactory.setPassword("test");
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("foo::q1");
MessageProducer messageProducer = session.createProducer(destination);
messageProducer.send(session.createTextMessage("hi"));
}
}