Statistics
| Revision:

gvsig-projects-pool / org.gvsig.vcsgis / trunk / org.gvsig.vcsgis / org.gvsig.vcsgis.lib / org.gvsig.vcsgis.lib.impl / src / test / java / org / gvsig / vcsgis / lib / impl / server / TestS05Update.java @ 2823

History | View | Annotate | Download (7.97 KB)

1
package org.gvsig.vcsgis.lib.impl.server;
2

    
3
import java.io.ByteArrayInputStream;
4
import java.io.ByteArrayOutputStream;
5
import java.io.File;
6
import java.io.InputStream;
7
import java.net.URL;
8
import java.sql.Timestamp;
9
import java.util.List;
10
import java.util.Map;
11
import junit.framework.TestCase;
12
import static junit.framework.TestCase.assertEquals;
13
import static junit.framework.TestCase.assertNotNull;
14
import org.apache.commons.io.FileUtils;
15
import org.apache.commons.io.IOUtils;
16
import org.apache.commons.lang3.StringUtils;
17
import org.gvsig.expressionevaluator.Expression;
18
import org.gvsig.fmap.dal.feature.EditableFeature;
19
import org.gvsig.fmap.dal.feature.Feature;
20
import org.gvsig.fmap.dal.feature.FeatureStore;
21
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
22
import org.gvsig.tools.dispose.DisposeUtils;
23
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
24
import org.gvsig.vcsgis.lib.SequentialCodeGenerator;
25
import org.gvsig.vcsgis.lib.VCSGisEntity;
26
import org.gvsig.vcsgis.lib.VCSGisLocator;
27
import org.gvsig.vcsgis.lib.VCSGisManager;
28
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_NO_ERROR;
29
import org.gvsig.vcsgis.lib.impl.TestUtils;
30
import org.gvsig.vcsgis.lib.repository.VCSGisRepository;
31
import org.gvsig.vcsgis.lib.repository.VCSGisRepositoryChange;
32
import org.gvsig.vcsgis.lib.repository.remoteclient.requests.CheckoutRequestClient;
33
import org.gvsig.vcsgis.lib.repository.remoteclient.requests.UpdateRequestClient;
34
import org.gvsig.vcsgis.lib.server.VCSGisServerHandler;
35
import org.gvsig.vcsgis.lib.server.handlers.CheckoutHandler;
36
import org.gvsig.vcsgis.lib.server.handlers.UpdateHandler;
37
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace;
38
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceChanges;
39
import org.slf4j.Logger;
40
import org.slf4j.LoggerFactory;
41

    
42

    
43
public class TestS05Update extends TestCase {
44

    
45
    private static final Logger LOGGER = LoggerFactory.getLogger(TestS05Update.class);
46

    
47
    public TestS05Update(String testName) {
48
        super(testName);
49
    }
50

    
51
    @Override
52
    protected void setUp() throws Exception {
53
        super.setUp();
54
        new DefaultLibrariesInitializer().fullInitialize();
55
    }
56

    
57
    @Override
58
    protected void tearDown() throws Exception {
59
        super.tearDown();
60
    }
61

    
62
    // TODO add test methods here. The name must begin with 'test'. For example:
63
    // public void testHello() {}
64

    
65
    public void testUpdate() throws Exception {
66
        final String testid = "ServerCheckout";
67
        final URL serverUrl = new URL("http://localhost:9810/");
68
        
69
        VCSGisManager manager = VCSGisLocator.getVCSGisManager();
70
        manager.setCodeGenerator(new SequentialCodeGenerator());
71
        
72
        JDBCServerExplorerParameters serverParameters = TestUtils.getServerExplorerParameters("srv-"+testid);
73
        File ws1file = TestUtils.getFile(FileUtils.getFile("test-dbs","ws1-"+testid)); 
74
        File ws2file = TestUtils.getFile(FileUtils.getFile("test-dbs","ws2-"+testid)); 
75

    
76
        int r;
77
        Feature f;
78
        EditableFeature ef;
79
        List<Feature> features;
80
        FeatureStore store1;
81
        FeatureStore store2;
82
        
83
        // ------------------------------------------------------------
84
        // Inicializamos el repositorio y lo abrimos
85
        r = manager.initRepository(serverParameters, null);
86
        assertEquals("init_server status", ERR_NO_ERROR, r);
87
        
88
        VCSGisRepository repoLocaldb = manager.openRepository(serverParameters);
89
        TestUtils.h2sql_repository(repoLocaldb);
90

    
91
        // ------------------------------------------------------------
92
        // Creamos workspace1 y lo abrimos.
93
        r = manager.initWorkspace(ws1file, repoLocaldb, "Test update1",null);
94
        assertEquals("init_ws1 status", ERR_NO_ERROR, r);
95
        
96
        VCSGisWorkspace ws1 = manager.openWorkspace(ws1file);
97
        TestUtils.h2sql_workspace(ws1);
98

    
99
        // ------------------------------------------------------------
100
        // Adicionamos al workspace1 la tabla "test" y la commitamos.
101
        FeatureStore sourceTest = TestUtils.openSourceStore2();
102
        r = ws1.add("test", sourceTest, "text");
103
        assertEquals("ws1.add status", ERR_NO_ERROR, r);
104
        r = ws1.commit();
105
        assertEquals("ws1.commit-1 status", ERR_NO_ERROR, r);
106
        DisposeUtils.disposeQuietly(sourceTest);
107

    
108
        
109
        // ------------------------------------------------------------
110
        // Inicializamos el workspace2 asociandolo al mismo repositorio que 
111
        // workspace1 y lo abrimos.
112
        r = manager.initWorkspace(ws2file, repoLocaldb, "Test update2",null);
113
        assertEquals("init_ws2 status", ERR_NO_ERROR, r);
114
        VCSGisWorkspace ws2 = manager.openWorkspace(ws2file);
115
        TestUtils.h2sql_workspace(ws2);
116
        
117
        // ------------------------------------------------------------
118
        // Hacemos un checkout en workspace2 de la tabla "test" 
119
        // y comprobamos que tiene lo que toca.
120
        r = ws2.checkout("test");
121
        assertEquals("ws2-checkout status", ERR_NO_ERROR, r);
122
       
123
        VCSGisEntity entity = ws2.getWorkspaceEntityByName("test");
124
        assertNotNull("ws2.update-1 entity", entity);
125
        assertEquals("ws2.update-1 label", "text", entity.getFieldForLabel());
126

    
127
        store2 = ws2.getFeatureStore("test");
128
        assertEquals("ws2-checkout sizes", 3, store2.size64());
129
        features = store2.getFeatures((Expression)null, "id", true);
130
        assertEquals(1    , features.get(0).getInt("id"));
131
        assertEquals("AAA", features.get(0).getString("text"));
132
        assertEquals(2    , features.get(1).getInt("id"));
133
        assertEquals("BBB", features.get(1).getString("text"));
134
        assertEquals(3    , features.get(2).getInt("id"));
135
        assertEquals("CCC", features.get(2).getString("text"));
136
        
137
        DisposeUtils.disposeQuietly(store2);
138

    
139
        // ------------------------------------------------------------
140
        // Modificamos la tabla "test" en el workspace1 y la commitamos.
141
        store1 = ws1.getFeatureStore("test");
142
        store1.edit();
143
        f = store1.findFirst("id=2");
144
        ef = f.getEditable();
145
        ef.set("text", "BB2");
146
        store1.update(ef);
147
        
148
        ef = store1.createNewFeature();
149
        ef.set("id",4);
150
        ef.set("text","DDD");
151
        store1.insert(ef);
152
        store1.finishEditing();
153
        DisposeUtils.disposeQuietly(store1);
154
        r = ws1.commit(Timestamp.valueOf("2020-02-17 10:11:12.123"), "Insertado DDD y modificado BBB a BB2", null);
155
        assertEquals("ws1.commit-2 status", ERR_NO_ERROR, r);
156
        
157
        String localRevisionCode = ws2.getWorkspaceEntity("test").getRepositoryRevisionCode();
158
        assertEquals("ws2 localRevisionCode", "00000000000015abcdef0123456789", localRevisionCode);
159
        
160
        ws1.dispose();
161
        ws2.dispose();
162
        ws1 = null;
163
        ws2 = null;
164
        
165
        // -------------------------
166
        // Check that the UpdateRequestClient produce the expected json output
167
        Map<String, String> expected = TestUtils.getExpecteds("S05Update.txt");
168

    
169
        VCSGisRepository repoRemote = manager.openRepository(serverUrl);
170
        UpdateRequestClient request = (UpdateRequestClient) repoRemote.createUpdateRequest("test");
171
        request.setLocalRevisionCode(localRevisionCode);
172
        
173
        ByteArrayOutputStream request_stream = new ByteArrayOutputStream(10024);
174
        request.requestProducer(request_stream);
175
        String request_s = request_stream.toString().trim();
176
        assertEquals("request data", expected.get("request"), request_s);
177
        
178
        // -------------------------
179
        // Check that CheckoutHandler produce the expected json as response to request
180
        ByteArrayInputStream is = new ByteArrayInputStream(request_stream.toByteArray());
181
        VCSGisServerHandler handler = new UpdateHandler(repoLocaldb);
182
        InputStream response_stream = handler.handle(is);
183
                
184
        String response_s = StringUtils.join(IOUtils.readLines(response_stream),'\n').trim();
185
        assertEquals("response data", expected.get("response"), response_s);
186

    
187

    
188
    }
189
}