View Javadoc
1   package org.kuali.student.enrollment.registration.engine;
2   
3   import org.apache.camel.CamelContext;
4   import org.apache.camel.EndpointInject;
5   import org.apache.camel.Produce;
6   import org.apache.camel.ProducerTemplate;
7   import org.apache.camel.ServiceStatus;
8   import org.apache.camel.component.mock.MockEndpoint;
9   import org.apache.camel.test.spring.CamelSpringJUnit4ClassRunner;
10  import org.apache.camel.test.spring.DisableJmx;
11  import org.apache.camel.test.spring.MockEndpoints;
12  import org.junit.Test;
13  import org.junit.runner.RunWith;
14  import org.kuali.student.common.eventing.EventMessage;
15  import org.kuali.student.common.util.security.ContextUtils;
16  import org.springframework.test.annotation.DirtiesContext;
17  import org.springframework.test.context.ContextConfiguration;
18  
19  import javax.annotation.Resource;
20  
21  import static org.junit.Assert.assertEquals;
22  
23  /**
24   * Tests Lui event camel routes.
25   */
26  @RunWith(CamelSpringJUnit4ClassRunner.class)
27  @ContextConfiguration(locations = {"classpath:lui-event-route-test-context.xml"})
28  @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
29  @MockEndpoints("log:*")
30  @DisableJmx(false)
31  public class LuiEventRouteTest {
32  
33      @Resource
34      protected CamelContext camelContext;
35  
36      @Resource
37      protected CamelContext camelContextTwo;
38  
39      @EndpointInject(uri = "mock:a", context = "camelContext")
40      protected MockEndpoint mockA;
41  
42      @EndpointInject(uri = "mock:b", context = "camelContext")
43      protected MockEndpoint mockB;
44  
45      @EndpointInject(uri = "mock:c", context = "camelContextTwo")
46      protected MockEndpoint mockC;
47  
48      @EndpointInject(uri = "mock:d", context = "camelContextTwo")
49      protected MockEndpoint mockD;
50  
51      @Produce(uri = "direct:start", context = "camelContext")
52      protected ProducerTemplate start;
53  
54      @Produce(uri = "direct:start2", context = "camelContextTwo")
55      protected ProducerTemplate startTwo;
56  
57      /**
58       * This test makes sure that the pattern matching works for our eventing route.
59       *
60       *
61       * @throws Exception
62       */
63      @Test
64      public void testPositive() throws Exception {
65          assertEquals(ServiceStatus.Started, camelContext.getStatus());
66  
67          EventMessage wrongTypeMessage = getEventMessage("1", "UPDATE", "kuali.lui.type.course.NO.OFFERING");
68          EventMessage updateMessage = getEventMessage("2", "UPDATE", "kuali.lui.type.course.offering");
69          EventMessage createMessage = getEventMessage("3", "CREATE", "kuali.lui.type.course.offering");
70  
71          mockA.expectedBodiesReceived(updateMessage);
72          mockB.expectedBodiesReceived(createMessage);
73  
74          mockA.expectedMessageCount(1);
75          mockB.expectedMessageCount(1);
76  
77          start.sendBody(updateMessage);
78          start.sendBody(wrongTypeMessage);
79          start.sendBody(createMessage);
80  
81          MockEndpoint.assertIsSatisfied(camelContext);
82      }
83  
84      /**
85       * Tests filtering of messages.
86       * @throws Exception
87       */
88      @Test
89      public void testComplexLogic() throws Exception {
90          assertEquals(ServiceStatus.Started, camelContextTwo.getStatus());
91  
92          EventMessage wrongTypeMessage = getEventMessage("1", "UPDATE", "kuali.lui.type.course.NO.OFFERING");
93          EventMessage updateMessage = getEventMessage("2", "UPDATE", "kuali.lui.type.course.offering");
94          EventMessage createMessage = getEventMessage("3", "CREATE", "kuali.lui.type.course.offering");
95          EventMessage deleteMessage = getEventMessage("4", "DELETE", "kuali.lui.type.course.offering");
96  
97          mockC.expectedMessageCount(3); // 1 create and 1 update == 2
98  
99          startTwo.sendBody(updateMessage);
100         startTwo.sendBody(wrongTypeMessage);
101         startTwo.sendBody(deleteMessage);
102         startTwo.sendBody(createMessage);
103 
104         MockEndpoint.assertIsSatisfied(camelContextTwo);
105     }
106 
107 
108 
109     /**
110      * This tests that our aggregation is working. ie. when a user updates seat counts
111      * there are often multiple lui updates called. we only want the last one over a
112      * span of 5 seconds (configurable)
113      * @throws Exception
114      */
115     @Test
116     public void testAggregation() throws Exception {
117         assertEquals(ServiceStatus.Started, camelContext.getStatus());
118 
119         EventMessage wrongTypeMessage = getEventMessage("1", "UPDATE", "kuali.lui.type.course.NO.OFFERING");
120         EventMessage updateMessage = getEventMessage("2", "UPDATE", "kuali.lui.type.course.offering");
121         EventMessage createMessage = getEventMessage("3", "CREATE", "kuali.lui.type.course.offering");
122 
123         mockA.expectedBodiesReceived(updateMessage);
124         mockB.expectedBodiesReceived(createMessage);
125 
126         mockA.expectedMessageCount(1);  // we're sending 10 messages, but only 1 should actually make it out
127         mockB.expectedMessageCount(1);
128 
129         // Send 10 messages through the route.
130         for(int i =0; i<10; i++) {
131             start.sendBody(updateMessage);
132         }
133         start.sendBody(wrongTypeMessage);
134         start.sendBody(createMessage);
135 
136         MockEndpoint.assertIsSatisfied(camelContext);
137     }
138 
139     /**
140      * helper method to create EventMessages
141      * @param id
142      * @param action
143      * @param type
144      * @return
145      */
146     private EventMessage getEventMessage(String id, String action, String type){
147        EventMessage eventMessage = new EventMessage();
148 
149         eventMessage.setId(id);
150         eventMessage.setAction(action);
151         eventMessage.setType(type);
152         eventMessage.setContextInfo(ContextUtils.createDefaultContextInfo());
153 
154         return eventMessage;
155     }
156 }