View Javadoc
1   /*
2    * Copyright 2018 the original author or authors.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.openehealth.ipf.commons.audit.queue;
17  
18  import org.apache.activemq.broker.BrokerService;
19  import org.apache.activemq.pool.PooledConnectionFactory;
20  import org.junit.After;
21  import org.junit.Before;
22  import org.junit.BeforeClass;
23  import org.junit.Test;
24  import org.openehealth.ipf.commons.audit.DefaultAuditContext;
25  import org.openehealth.ipf.commons.audit.codes.EventOutcomeIndicator;
26  import org.openehealth.ipf.commons.audit.event.ApplicationActivityBuilder;
27  import org.openehealth.ipf.commons.audit.protocol.RecordingAuditMessageTransmission;
28  import org.openehealth.ipf.commons.audit.utils.AuditUtils;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  import org.springframework.jms.listener.SimpleMessageListenerContainer;
32  
33  import javax.jms.MessageListener;
34  import java.util.Locale;
35  
36  import static org.junit.Assert.assertEquals;
37  
38  /**
39   * @author Dmytro Rud
40   */
41  public class JmsAuditMessageQueueTest {
42  
43      private static final String JMS_BROKER_URL = "tcp://localhost:61616";
44      private static final String JMS_QUEUE_NAME = "atna";
45  
46      private static BrokerService jmsBroker;
47  
48      private JmsAuditMessageQueue atnaQueue;
49      private DefaultAuditContext auditContext;
50      private RecordingAuditMessageTransmission recorder;
51  
52      private Logger LOG = LoggerFactory.getLogger(JmsAuditMessageQueueTest.class);
53  
54      @BeforeClass
55      public static void beforeClass() throws Exception {
56          Locale.setDefault(Locale.ENGLISH);
57  
58          jmsBroker = new BrokerService();
59          jmsBroker.addConnector(JMS_BROKER_URL);
60          jmsBroker.setUseJmx(false);
61          jmsBroker.setPersistent(false);
62          jmsBroker.deleteAllMessages();
63          jmsBroker.start();
64      }
65  
66      @Before
67      public void setup() {
68          recorder = new RecordingAuditMessageTransmission();
69          auditContext = new DefaultAuditContext();
70          auditContext.setAuditTransmissionProtocol(recorder);
71      }
72  
73      @After
74      public void tearDown() {
75          if (atnaQueue != null) {
76              atnaQueue.shutdown();
77          }
78          recorder.shutdown();
79      }
80  
81      @Test
82      public void testActiveMQ() throws Exception {
83          PooledConnectionFactory jmsConnectionFactory = new PooledConnectionFactory(JMS_BROKER_URL);
84          MessageListener messageListener = new JmsAuditMessageListener(auditContext);
85  
86          // Setup Consumer
87          SimpleMessageListenerContainer messageListenerContainer = new SimpleMessageListenerContainer();
88          messageListenerContainer.setupMessageListener(messageListener);
89          messageListenerContainer.setConnectionFactory(jmsConnectionFactory);
90          messageListenerContainer.setDestinationName(JMS_QUEUE_NAME);
91          messageListenerContainer.start();
92  
93          // Setup producer
94          atnaQueue = new JmsAuditMessageQueue(jmsConnectionFactory, JMS_QUEUE_NAME, null, null);
95          auditContext.setAuditMessageQueue(atnaQueue);
96  
97          sendAudit();
98          Thread.sleep(500);
99  
100         assertEquals(1, recorder.getMessages().size());
101 
102     }
103 
104     private void sendAudit() {
105         LOG.debug("Sending audit record");
106         auditContext.audit(
107                 new ApplicationActivityBuilder.ApplicationStart(EventOutcomeIndicator.Success)
108                         .setAuditSource(auditContext)
109                         .setApplicationParticipant(
110                                 "appName",
111                                 null,
112                                 null,
113                                 AuditUtils.getLocalHostName())
114                         .addApplicationStarterParticipant(System.getProperty("user.name"))
115                         .getMessages());
116     }
117 
118 
119 }