summaryrefslogtreecommitdiffstats
path: root/das-java/branches/das-java-M2/rdb/src/test/java/org/apache/tuscany/das/rdb/test/CommandGroupTests.java
blob: 076f8d9db77c211c1165fec4e7348796ae267344 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
package org.apache.tuscany.das.rdb.test;

/*
 * This test
 * 
 */

import org.apache.tuscany.das.rdb.Command;
import org.apache.tuscany.das.rdb.DAS;
import org.apache.tuscany.das.rdb.config.Config;
import org.apache.tuscany.das.rdb.test.data.CustomerData;
import org.apache.tuscany.das.rdb.test.data.OrderData;
import org.apache.tuscany.das.rdb.test.framework.DasTest;
import org.apache.tuscany.das.rdb.util.ConfigUtil;

import commonj.sdo.DataObject;

/**
 * 
 * This tests use of the XML Config file. Tests will utilize the 
 * customer-orders-orderdetails tables. The plan is for the config file to have a
 * section that applies to all commands and another that applies to specific commands.
 * 
 * The config file will be used to initialize a command factory that will then return named commands.
 * 
 * There will be two read commands:
 * 
 * 1) Return all customers 2) Return a specific customer (by ID) and related orders and order details
 * 
 * A test will demonstrate the creation of the factory and then reuse of commands created from the same config data file
 * 
 */
public class CommandGroupTests extends DasTest {

    protected void setUp() throws Exception {
        super.setUp();

        new CustomerData(getAutoConnection()).refresh();
        new OrderData(getAutoConnection()).refresh();
    }

    protected void tearDown() throws Exception {
        super.tearDown();
    }

    /**
     * Read
     */
    public void testRead() throws Exception {

        DAS das = DAS.FACTORY.createDAS(getConfig("CustomersOrdersConfig.xml"), getConnection());

        Command read = das.getCommand("all customers");
        DataObject root = read.executeQuery();

        assertEquals(5, root.getList("CUSTOMER").size());

    }

    /**
     * Read
     */
    public void testReadUsingConfigInput() throws Exception {
        Config config = ConfigUtil.loadConfig(getConfig("CustomersOrdersConfig.xml"));
        DAS das = DAS.FACTORY.createDAS(config, getConnection());

        Command read = das.getCommand("all customers");
        DataObject root = read.executeQuery();

        assertEquals(5, root.getList("CUSTOMER").size());

    }

    /**
     * Read an order using parm marker
     */
    public void testReadWithParmmarker() throws Exception {

        DAS das = DAS.FACTORY.createDAS(getConfig("CustomersOrdersConfig.xml"), getConnection());

        Command read = das.getCommand("order by id");
        read.setParameter(1, Integer.valueOf(1));
        DataObject root = read.executeQuery();

        assertEquals("recombobulator", root.getString("ANORDER[1]/PRODUCT"));

    }

    /**
     * Specify connection properties in config. Add explicit update command
     */
    public void testUpdate() throws Exception {

        DAS das = DAS.FACTORY.createDAS(getConfig("CustomersOrdersConfig.xml"), getConnection());

        Command read = das.getCommand("all customers");
        DataObject root = read.executeQuery();
        // Verify precondition
        assertFalse(root.get("CUSTOMER[1]/LASTNAME").equals("Pavick"));
        int id = root.getInt("CUSTOMER[1]/ID");

        Command update = das.getCommand("update customer");
        update.setParameter(1, Integer.valueOf(id));
        update.execute();

        // Verify update - reuse select command
        root = read.executeQuery();
        assertEquals("Pavick", root.get("CUSTOMER[1]/LASTNAME"));

    }

    /**
     * Read all customers, select a specific customer. Then read that 
     * customer and related orders. Modify an order and flush changes back
     */
    public void testRead2() throws Exception {

        // Create the group and set common connection
        DAS das = DAS.FACTORY.createDAS(getConfig("CustomersOrdersConfig.xml"), getConnection());

        // Read all customers and remember the first one
        Command read = das.getCommand("all customers");
        DataObject root = read.executeQuery();
        Integer id = (Integer) root.get("CUSTOMER[1]/ID");

        // Read the specific Customer from above and its related orders
        Command custOrders = das.getCommand("customer and orders");
        custOrders.setParameter(1, id);
        root = custOrders.executeQuery();

        // Modify the first order and flush this change back to the database
        root.setString("CUSTOMER[1]/orders[1]/PRODUCT", "Defibrillator");
        Integer orderId = (Integer) root.get("CUSTOMER[1]/orders[1]/ID");
        das.applyChanges(root);

        // Verify
        Command orderByID = das.getCommand("order by id");
        orderByID.setParameter(1, orderId);
        assertEquals("Defibrillator", root.getString("ANORDER[1]/PRODUCT"));

    }

}