1   /*
2    * $Id: JdbcSelectOnOutboundFunctionalTestCase.java 10571 2008-01-28 14:47:36Z ashaposhnikov $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.providers.jdbc;
12  
13  import org.mule.MuleManager;
14  import org.mule.extras.client.MuleClient;
15  import org.mule.impl.MuleMessage;
16  import org.mule.providers.NullPayload;
17  import org.mule.providers.jdbc.util.MuleDerbyUtils;
18  import org.mule.tck.FunctionalTestCase;
19  import org.mule.umo.UMOMessage;
20  
21  import java.io.Serializable;
22  import java.sql.PreparedStatement;
23  import java.sql.ResultSet;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.commons.dbutils.QueryRunner;
29  
30  public class JdbcSelectOnOutboundFunctionalTestCase extends FunctionalTestCase
31  {
32  
33      private  static final String[] TEST_VALUES = {"Test", "The Moon", "Terra"};
34  
35      protected String getConfigResources()
36      {
37          return "jdbc-select-outbound.xml";
38      }
39  
40      protected void doPostFunctionalSetUp() throws Exception
41      {
42          super.doPostFunctionalSetUp();
43  
44          JdbcConnector jdbcConnector = (JdbcConnector) MuleManager.getInstance().getConnectors().get("jdbcConnector");
45          QueryRunner qr = new QueryRunner(jdbcConnector.getDataSource());
46  
47          qr.update(jdbcConnector.getConnection(), "CREATE PROCEDURE TEST_PROC(IN A INTEGER,\n" +
48                  "INOUT B INTEGER, OUT C DOUBLE, OUT S VARCHAR(1024))\n" +
49                  "PARAMETER STYLE JAVA READS SQL DATA LANGUAGE JAVA EXTERNAL NAME \n" +
50                  "'org.mule.providers.jdbc.DerbyStoredProc.plus'");
51          logger.debug("Procedure created");
52  
53          int updated;
54  
55          try
56          {
57              updated = qr.update(jdbcConnector.getConnection(), "DELETE FROM TEST");
58              logger.debug(updated + " rows deleted");
59          }
60          catch (Exception e)
61          {
62              qr.update(jdbcConnector.getConnection(), "CREATE TABLE TEST(ID INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 0)  NOT NULL PRIMARY KEY,TYPE INTEGER,DATA VARCHAR(255),ACK TIMESTAMP,RESULT VARCHAR(255))");
63              logger.debug("Table created");
64          }
65  
66          updated = qr.update(jdbcConnector.getConnection(), "INSERT INTO TEST(TYPE, DATA) VALUES (1, '" + TEST_VALUES[0] + "')");
67          logger.debug(updated + " rows updated");
68          updated = qr.update(jdbcConnector.getConnection(), "INSERT INTO TEST(TYPE, DATA) VALUES (2, '" + TEST_VALUES[1] + "')");
69          logger.debug(updated + " rows updated");
70          updated = qr.update(jdbcConnector.getConnection(), "INSERT INTO TEST(TYPE, DATA) VALUES (3, '" + TEST_VALUES[2] + "')");
71          logger.debug(updated + " rows updated");
72  
73      }
74  
75      protected void doFunctionalTearDown() throws Exception
76      {
77          JdbcConnector jdbcConnector = (JdbcConnector) MuleManager.getInstance().getConnectors().get("jdbcConnector");
78          QueryRunner qr = new QueryRunner(jdbcConnector.getDataSource());
79          int updated = qr.update(jdbcConnector.getConnection(), "DELETE FROM TEST");
80          logger.debug(updated + " rows deleted");
81          qr.update(jdbcConnector.getConnection(), "DROP PROCEDURE TEST_PROC");
82          logger.debug("Procedure dropped");
83  
84          super.doFunctionalTearDown();
85      }
86  
87      protected void suitePreSetUp() throws Exception
88      {
89          MuleDerbyUtils.defaultDerbyCleanAndInit("derby.properties", "database.name");
90          super.suitePreSetUp();
91      }
92  
93      public void testSelectOnOutbound() throws Exception
94      {
95          MuleClient client = new MuleClient();
96          UMOMessage reply = client.send("vm://jdbc.test", new MuleMessage(NullPayload.getInstance()));
97          assertNotNull(reply.getPayload());
98          assertTrue(reply.getPayload() instanceof List);
99          List resultList = (List) reply.getPayload();
100         assertTrue(resultList.size() == 1);
101         assertTrue(resultList.get(0) instanceof Map);
102         Map resultMap = (Map) resultList.get(0);
103         assertEquals(new Integer(1), resultMap.get("TYPE"));
104         assertEquals(TEST_VALUES[0], resultMap.get("DATA"));
105     }
106 
107     private void internalStoredProcTest(String endpoint, UMOMessage message) throws Exception
108     {
109         MuleClient client = new MuleClient();
110         UMOMessage reply = client.send(endpoint, message);
111         assertNotNull(reply.getPayload());
112         assertTrue(reply.getPayload() instanceof Map);
113         Map resultMap = (Map) reply.getPayload();
114         assertTrue(resultMap.size() == 3);
115         assertEquals(resultMap.get("b"), new Integer(10));
116         assertEquals(resultMap.get("c"), new Double(8.3));
117         assertEquals(resultMap.get("s"), "test");
118     }
119 
120     public void testStoredProc() throws Exception 
121     {
122         internalStoredProcTest("vm://proc.test", new MuleMessage(NullPayload.getInstance()));
123     }
124 
125     public void testEndpointProperties() throws Exception
126     {
127         internalStoredProcTest("vm://proc2.test", new MuleMessage(NullPayload.getInstance()));
128     }
129 
130     public void testCallProcByMapPropertyExtractor() throws Exception
131     {
132         internalStoredProcTest("vm://proc3.test", new MapMessageFactory().createMessage());
133     }
134 
135     public void testCallProcByPayloadPropertyExtractor() throws Exception
136     {
137         internalStoredProcTest("vm://proc3.test", new PayloadMessageFactory().createMessage());
138     }
139 
140     public void testCallProcByMessagePropertyExtractor() throws Exception
141     {
142         //TODO: MULE-2941
143         internalStoredProcTest("vm://proc4.test", new PropertiesMessageFactory().createMessage());
144     }
145 
146     public void testInsert() throws Exception
147     {
148         MuleClient client = new MuleClient();
149         UMOMessage reply = client.send("vm://insert.test", new MuleMessage(NullPayload.getInstance()));
150         assertTrue(reply.getPayload() instanceof NullPayload);
151         JdbcConnector jdbcConnector = (JdbcConnector) MuleManager.getInstance().getConnectors().get("jdbcConnector");
152         PreparedStatement statement = jdbcConnector.getConnection().prepareStatement("SELECT type, data FROM Test WHERE type = 4 AND data='test insert'");
153         ResultSet result = statement.executeQuery();
154         int counter = 0;
155         while (result.next())
156         {
157             counter++;
158         }
159         assertEquals(counter, 1);
160         result.close();
161         statement.close();
162 
163     }
164 
165     public void testSelectOnOutboundByPropertyExtractor() throws Exception
166     {
167         MuleClient client = new MuleClient();
168         MyMessage payload = new MyMessage(2);
169         UMOMessage reply = client.send("vm://terra", new MuleMessage(payload));
170         assertNotNull(reply.getPayload());
171         assertTrue(reply.getPayload() instanceof List);
172         List resultList = (List) reply.getPayload();
173         logger.debug("resultList.size() " + resultList.size());
174         assertTrue(resultList.size() == 1);
175         assertTrue(resultList.get(0) instanceof Map);
176         Map resultMap = (Map) resultList.get(0);
177         assertEquals(new Integer(2), resultMap.get("TYPE"));
178         assertEquals(TEST_VALUES[1], resultMap.get("DATA"));
179     }
180 
181     public static class MyMessage implements Serializable
182     {
183 
184         public MyMessage(int type)
185         {
186             this.type = type;
187         }
188 
189         private int type;
190 
191         public int getType()
192         {
193             return type;
194         }
195 
196         public void setType(int type)
197         {
198             this.type = type;
199         }
200     }
201 
202     public static class AnotherMessage implements Serializable
203     {
204         private int a;
205         private int b;
206 
207         public AnotherMessage(int a, int b) {
208             this.a = a;
209             this.b = b;
210         }
211 
212         public int getA() {
213             return a;
214         }
215 
216         public void setA(int a) {
217             this.a = a;
218         }
219 
220         public int getB() {
221             return b;
222         }
223 
224         public void setB(int b) {
225             this.b = b;
226         }
227     }
228 
229     interface MessageFactory
230     {
231         UMOMessage createMessage();
232     }
233 
234     class MapMessageFactory implements MessageFactory
235     {
236         public UMOMessage createMessage() {
237             Map payload = new HashMap();
238             payload.put("a", new Integer(3));
239             payload.put("b", new Integer(5));
240             return new MuleMessage(payload);
241         }
242     }
243 
244     class PayloadMessageFactory implements MessageFactory
245     {
246         public UMOMessage createMessage() {
247             AnotherMessage payload = new AnotherMessage(3, 5);
248             return new MuleMessage(payload);
249         }
250     }
251 
252     class PropertiesMessageFactory implements MessageFactory
253     {
254         public UMOMessage createMessage() {
255             UMOMessage message = new MuleMessage(NullPayload.getInstance());
256             message.setIntProperty("a", 3);
257             message.setIntProperty("b", 5);
258             return message;
259         }
260     }
261 
262 }
263 
264