WIP.First cut of amqp-rabbit configuration-based api
[aquarium] / logic / src / test / scala / gr / grnet / aquarium / messaging / MessagingTest.scala
1 /*
2  * Copyright 2011 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials
15  *      provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * The views and conclusions contained in the software and
31  * documentation are those of the authors and should not be
32  * interpreted as representing official policies, either expressed
33  * or implied, of GRNET S.A.
34  */
35
36 package gr.grnet.aquarium.messaging
37
38 import amqp.rabbitmq.confmodel._
39 import org.junit.Test
40 import org.junit.Assert._
41 import com.ckkloverdos.resource.DefaultResourceContext
42 import com.ckkloverdos.props.Props
43 import com.ckkloverdos.maybe.Just
44 import com.thoughtworks.xstream.XStream
45 import gr.grnet.aquarium.util.xstream.XStreamHelpers
46
47 /**
48  * 
49  * @author Christos KK Loverdos <loverdos@gmail.com>.
50  */
51 class MessagingTest {
52   object RCFolders {
53     val rabbitmq = "rabbitmq"
54
55     val aquarium_dev = "aquarium_dev"
56
57     val producers = "producers"
58     val consumers = "consumers"
59   }
60   
61   object PropFiles {
62     val configurations = "configuration.properties"
63
64     val aquarium_dev = "aquarium_dev.properties"
65
66     val main_producer = "main_producer.properties"
67
68     val main_consumer = "main_consumer.properties"
69   }
70
71   object PropKeys {
72     val configurations = "configurations"
73
74     // configuration
75     val addresses = "addresses"
76     val username  = "username"
77     val password = "password"
78     val host = "host"
79     val virtualHost = "virtualHost"
80     val port = "port"
81     val connections = "connections"
82
83     // connection
84     val exchange = "exchange"
85     val exchangeType = "exchangeType"
86     val exchangeIsDurable = "exchangeIsDurable"
87     val producers = "producers"
88     val consumers = "consumers"
89
90     // producer
91     val routingKey = "routingKey"
92
93     // consumer
94     val queue = "queue"
95   }
96   
97   object PropValues {
98     val aquarium_dev = "aquarium_dev"
99     val localhost = "localhost"
100   }
101
102   val baseRC = DefaultResourceContext
103   val rabbitmqRC = baseRC / RCFolders.rabbitmq
104
105   @Test
106   def testConfigurationsExist {
107     assertTrue(rabbitmqRC.getResource(PropFiles.configurations).isJust)
108   }
109   
110   @Test
111   def testConfigurations {
112     val props = Props(PropFiles.configurations, rabbitmqRC).getOr(Props.empty)
113     val confList = props.getTrimmedList(PropKeys.configurations)
114     assertEquals(List(PropValues.aquarium_dev, PropValues.localhost), confList)
115
116     val xs = XStreamHelpers.newXStream
117
118     val cm1 = new RabbitMQConsumerModel("queue1")
119     val pm1 = new RabbitMQProducerModel("routing.key.1")
120     val con1 = new RabbitMQConnectionModel("exchnage1", "direct", true, List(pm1), List(cm1))
121     val conf1 = new RabbitMQConfigurationModel("aquarium", "aquarium", "localhost", 5672, Nil, "/", List(con1))
122     val confs = new RabbitMQConfigurationsModel2(List(conf1))
123     
124     val xml = xs.toXML(confs)
125     println(xml)
126
127     val xml2 = """<gr.grnet.aquarium.messaging.amqp.rabbitmq.confmodel.RabbitMQConfigurationsModel2>
128       <configurations class="List">
129         <RabbitMQConfigurationModel>
130           <username>aquarium</username>
131           <password>aquarium</password>
132           <host>localhost</host>
133           <port>5672</port>
134           <addresses class="Nil"/>
135           <virtualHost>/</virtualHost>
136           <connections class="List">
137             <RabbitMQConnectionModel>
138               <exchange>exchnage1</exchange>
139               <exchangeType>direct</exchangeType>
140               <isDurable>true</isDurable>
141               <producers class="List">
142                 <RabbitMQProducerModel>
143                   <routingKey>routing.key.1</routingKey>
144                 </RabbitMQProducerModel>
145               </producers>
146               <consumers class="List">
147                 <RabbitMQConsumerModel>
148                   <queue>queue1</queue>
149                 </RabbitMQConsumerModel>
150               </consumers>
151             </RabbitMQConnectionModel>
152           </connections>
153         </RabbitMQConfigurationModel>
154       </configurations>
155     </gr.grnet.aquarium.messaging.amqp.rabbitmq.confmodel.RabbitMQConfigurationsModel2>"""
156     for(model2 <- XStreamHelpers.parseType[RabbitMQConfigurationsModel2](xml2, xs)) {
157       println(model2.configurations(0).addresses)
158     }
159   }
160
161   @Test
162   def testConfigurationAquarium_DevExists {
163     val aquariumDevRC = rabbitmqRC / RCFolders.aquarium_dev
164     assertTrue(aquariumDevRC.getResource(PropFiles.aquarium_dev).isJust)
165   }
166
167   @Test
168   def testConfigurationAquarium_Dev {
169     val props = Props(PropFiles.aquarium_dev, rabbitmqRC / RCFolders.aquarium_dev).getOr(Props.empty)
170
171     assertTrue(props.get(PropKeys.username).isJust)
172     assertTrue(props.get(PropKeys.password).isJust)
173     assertTrue(props.get(PropKeys.addresses).isJust)
174     assertTrue(props.get(PropKeys.host).isJust)
175     assertTrue(props.get(PropKeys.virtualHost).isJust)
176     assertTrue(props.get(PropKeys.port).isJust)
177     assertTrue(props.get(PropKeys.connections).isJust)
178     
179     assertEquals(Just(PropValues.aquarium_dev), props.get(PropKeys.connections))
180   }
181
182   @Test
183   def testConnectionAquarium_DevExists {
184     val aquariumDevRC2 = rabbitmqRC / RCFolders.aquarium_dev / RCFolders.aquarium_dev
185     assertTrue(aquariumDevRC2.getResource(PropFiles.aquarium_dev).isJust)
186   }
187   
188   @Test
189   def testConnectionAquarium_Dev {
190     val props = Props(PropFiles.aquarium_dev, rabbitmqRC / RCFolders.aquarium_dev / RCFolders.aquarium_dev).getOr(Props.empty)
191     
192     assertTrue(props.get(PropKeys.exchange).isJust)
193     assertTrue(props.get(PropKeys.exchangeType).isJust)
194     assertTrue(props.get(PropKeys.exchangeIsDurable).isJust)
195     assertTrue(props.get(PropKeys.producers).isJust)
196     assertTrue(props.get(PropKeys.consumers).isJust)
197
198     assertEquals(List("main_producer"), props.getTrimmedList(PropKeys.producers))
199     assertEquals(List("main_consumer"), props.getTrimmedList(PropKeys.consumers))
200   }
201
202   @Test
203   def testProducerMainProducerExists {
204     val rc = rabbitmqRC / RCFolders.aquarium_dev / RCFolders.aquarium_dev / RCFolders.producers
205     assertTrue(rc.getResource(PropFiles.main_producer).isJust)
206   }
207
208   @Test
209   def testProducerMainProducer {
210     val props = Props(PropFiles.main_producer, rabbitmqRC / RCFolders.aquarium_dev / RCFolders.aquarium_dev / RCFolders.producers).getOr(Props.empty)
211
212     assertTrue(props.get(PropKeys.routingKey).isJust)
213   }
214
215
216
217
218   @Test
219   def testConsumerMainConsumerExists {
220     val rc = rabbitmqRC / RCFolders.aquarium_dev / RCFolders.aquarium_dev / RCFolders.consumers
221     assertTrue(rc.getResource(PropFiles.main_consumer).isJust)
222   }
223
224   @Test
225   def testConsumerMainConsumer {
226     val props = Props(PropFiles.main_consumer, rabbitmqRC / RCFolders.aquarium_dev / RCFolders.aquarium_dev / RCFolders.consumers).getOr(Props.empty)
227
228     assertTrue(props.get(PropKeys.queue).isJust)
229   }
230 }