View Javadoc

1   /*
2    * SymmetricDS is an open source database synchronization solution.
3    *   
4    * Copyright (C) Eric Long <erilong@users.sourceforge.net>,
5    *               Chris Henson <chenson42@users.sourceforge.net>
6    *
7    * This library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 3 of the License, or (at your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this library; if not, see
19   * <http://www.gnu.org/licenses/>.
20   */
21  
22  package org.jumpmind.symmetric.service.impl;
23  
24  import java.io.ByteArrayOutputStream;
25  
26  import org.jumpmind.symmetric.common.Constants;
27  import org.jumpmind.symmetric.common.ParameterConstants;
28  import org.jumpmind.symmetric.model.Node;
29  import org.jumpmind.symmetric.model.NodeSecurity;
30  import org.jumpmind.symmetric.service.INodeService;
31  import org.jumpmind.symmetric.service.IRegistrationService;
32  import org.jumpmind.symmetric.test.AbstractDatabaseTest;
33  import org.jumpmind.symmetric.test.TestConstants;
34  import org.junit.Before;
35  import org.junit.Test;
36  
37  public class RegistrationServiceTest extends AbstractDatabaseTest {
38  
39      protected INodeService nodeService;
40  
41      protected IRegistrationService registrationService;
42  
43      public RegistrationServiceTest() throws Exception {
44          super();
45      }
46  
47      public RegistrationServiceTest(String dbName) {
48          super(dbName);
49      }
50  
51      @Before
52      public void setUp() {
53          nodeService = (INodeService) find(Constants.NODE_SERVICE);
54          registrationService = (IRegistrationService) find(Constants.REGISTRATION_SERVICE);
55      }
56  
57      @Test
58      public void testRegisterNodeFail() throws Exception {
59          Node node = new Node();
60          ByteArrayOutputStream out = new ByteArrayOutputStream();
61  
62          // Existing domain name and ID that is not open to register
63          node.setNodeGroupId(TestConstants.TEST_CLIENT_NODE_GROUP);
64          node.setExternalId("00001");
65          assertFalse(registrationService.registerNode(node, out, false), "Node should NOT be allowed to register");
66  
67          // Existing domain name but wrong ID
68          node.setNodeGroupId(TestConstants.TEST_CLIENT_NODE_GROUP);
69          node.setExternalId("wrongId");
70          assertFalse(registrationService.registerNode(node, out, false), "Node should NOT be allowed to register");
71  
72          // Wrong domain name and wrong ID
73          node.setNodeGroupId("wrongDomain");
74          node.setExternalId("wrongId");
75          assertFalse(registrationService.registerNode(node, out, false), "Node should NOT be allowed to register");
76      }
77  
78      @Test
79      public void testRegisterNode() throws Exception {
80          Node node = new Node();
81          node.setNodeGroupId(TestConstants.TEST_CLIENT_NODE_GROUP);
82          node.setExternalId("00002");
83          node.setSyncURL("http://localhost:8080/sync");
84          node.setSchemaVersion("1");
85          node.setDatabaseType("MySQL");
86          node.setDatabaseVersion("5");
87          ByteArrayOutputStream out = new ByteArrayOutputStream();
88          assertTrue(registrationService.registerNode(node, out, false), "Node should be allowed to register");
89  
90          node = nodeService.findNode("00002");
91          assertEquals(node.getNodeId(), "00002", "Wrong nodeId");
92          assertEquals(node.getNodeGroupId(), TestConstants.TEST_CLIENT_NODE_GROUP, "Wrong domainName");
93          assertEquals(node.getExternalId(), "00002", "Wrong domainId");
94          assertEquals(node.getSyncURL().toString(), "http://localhost:8080/sync", "Wrong syncUrl");
95          assertEquals(node.getSchemaVersion(), "1", "Wrong schemaVersion");
96          assertEquals(node.getDatabaseType(), "MySQL", "Wrong databaseType");
97          assertEquals(node.getDatabaseVersion(), "5", "Wrong databaseVersion");
98  
99          NodeSecurity security = nodeService.findNodeSecurity("00002");
100         assertEquals(security.getNodeId(), "00002", "Wrong nodeId");
101         assertNotSame(security.getPassword(), null, "Wrong password");
102         assertEquals(security.isRegistrationEnabled(), false, "Wrong isRegistrationEnabled");
103         assertNotSame(security.getRegistrationTime(), null, "Wrong registrationTime");
104     }
105 
106     @Test
107     public void testRegisterNodeAutomatic() throws Exception {
108         try {
109 
110             getParameterService().saveParameter(ParameterConstants.AUTO_REGISTER_ENABLED, true);
111             doTestRegisterNodeAutomatic();
112         } finally {
113             getParameterService().saveParameter(ParameterConstants.AUTO_REGISTER_ENABLED, false);
114         }
115     }
116 
117     private void doTestRegisterNodeAutomatic() throws Exception {
118         Node node = new Node();
119         node.setNodeGroupId(TestConstants.TEST_CLIENT_NODE_GROUP);
120         node.setExternalId("00008");
121         node.setSyncURL("http://localhost:8080/sync");
122         node.setSchemaVersion("1");
123         node.setDatabaseType("MySQL");
124         node.setDatabaseVersion("5");
125         ByteArrayOutputStream out = new ByteArrayOutputStream();
126         assertTrue(registrationService.registerNode(node, out, false), "Node should be allowed to register");
127 
128         node = nodeService.findNode("00008");
129         assertEquals(node.getNodeId(), "00008", "Wrong nodeId");
130         assertEquals(node.getNodeGroupId(), TestConstants.TEST_CLIENT_NODE_GROUP, "Wrong domainName");
131         assertEquals(node.getExternalId(), "00008", "Wrong domainId");
132         assertEquals(node.getSyncURL().toString(), "http://localhost:8080/sync", "Wrong syncUrl");
133         assertEquals(node.getSchemaVersion(), "1", "Wrong schemaVersion");
134         assertEquals(node.getDatabaseType(), "MySQL", "Wrong databaseType");
135         assertEquals(node.getDatabaseVersion(), "5", "Wrong databaseVersion");
136 
137         NodeSecurity security = nodeService.findNodeSecurity("00008");
138         assertEquals(security.getNodeId(), "00008", "Wrong nodeId");
139         assertNotSame(security.getPassword(), null, "Wrong password");
140         assertEquals(security.isRegistrationEnabled(), false, "Wrong isRegistrationEnabled");
141         assertNotSame(security.getRegistrationTime(), null, "Wrong registrationTime");
142 
143         // Make sure opening registration still works with auto-registration
144         registrationService.openRegistration(TestConstants.TEST_CLIENT_NODE_GROUP, "00009");
145         node = new Node();
146         node.setNodeGroupId(TestConstants.TEST_CLIENT_NODE_GROUP);
147         node.setExternalId("00009");
148         node.setSyncURL("http://localhost:8080/sync");
149         node.setSchemaVersion("1");
150         node.setDatabaseType("MySQL");
151         node.setDatabaseVersion("5");
152         out = new ByteArrayOutputStream();
153         assertTrue(registrationService.registerNode(node, out, false), "Node should be allowed to register");
154     }
155 
156     @Test
157     public void testRegisterNodeWithResponse() throws Exception {
158         registrationService.openRegistration("test-root-group", "09999");
159 
160         Node node = new Node();
161         node.setNodeGroupId("test-root-group");
162         node.setExternalId("09999");
163         node.setSyncURL("http://localhost:8080/sync");
164         node.setSchemaVersion("1");
165         node.setDatabaseType("MySQL");
166         node.setDatabaseVersion("5");
167         ByteArrayOutputStream out = new ByteArrayOutputStream();
168         assertTrue(registrationService.registerNode(node, out, false), "Node should be allowed to register");
169         out.close();
170         String response = out.toString();
171         assertTrue(response.indexOf("batch,") != -1, "Expected batch");
172         assertTrue(response.indexOf("table,") != -1, "Expected table");
173         assertTrue(response.indexOf("keys,") != -1, "Expected table");
174         assertTrue(response.indexOf("columns,") != -1, "Expected table");
175         assertTrue(response.indexOf("insert,") != -1, "Expected insert");
176         assertTrue(response.indexOf("commit,") != -1, "Expected commit");
177     }
178 
179     @Test
180     public void testReOpenRegistration() throws Exception {
181         registrationService.reOpenRegistration("00003");
182         NodeSecurity security = nodeService.findNodeSecurity("00003");
183         assertEquals(security.getNodeId(), "00003", "Wrong nodeId");
184         assertNotSame(security.getPassword(), "notsecret", "Wrong password");
185         assertEquals(security.isRegistrationEnabled(), true, "Wrong isRegistrationEnabled");
186         assertEquals(security.getRegistrationTime(), null, "Wrong registrationTime");
187 
188         Node node = new Node();
189         node.setNodeGroupId(TestConstants.TEST_CLIENT_NODE_GROUP);
190         node.setExternalId("00003");
191         node.setSyncURL("http://0:8080/sync");
192         node.setSchemaVersion("1");
193         node.setDatabaseType("hqsql");
194         node.setDatabaseVersion("1");
195         ByteArrayOutputStream out = new ByteArrayOutputStream();
196         assertTrue(registrationService.registerNode(node, out, false), "Node should be allowed to register");
197 
198         node = nodeService.findNode("00003");
199         assertEquals(node.getNodeId(), "00003", "Wrong nodeId");
200         assertEquals(node.getNodeGroupId(), TestConstants.TEST_CLIENT_NODE_GROUP, "Wrong domainName");
201         assertEquals(node.getExternalId(), "00003", "Wrong domainId");
202         assertEquals(node.getSyncURL().toString(), "http://0:8080/sync", "Wrong syncUrl");
203         assertEquals(node.getSchemaVersion(), "1", "Wrong schemaVersion");
204         assertEquals(node.getDatabaseType(), "hqsql", "Wrong databaseType");
205         assertEquals(node.getDatabaseVersion(), "1", "Wrong databaseVersion");
206 
207         security = nodeService.findNodeSecurity("00003");
208         assertEquals(security.getNodeId(), "00003", "Wrong nodeId");
209         assertNotSame(security.getPassword(), "notsecret", "Wrong password");
210         assertEquals(security.isRegistrationEnabled(), false, "Wrong isRegistrationEnabled");
211         assertNotSame(security.getRegistrationTime(), null, "Wrong registrationTime");
212     }
213 
214     @Test
215     public void testReOpenRegistrationFail() throws Exception {
216         registrationService.reOpenRegistration("00004");
217         NodeSecurity security = nodeService.findNodeSecurity("00004");
218         assertNull(security, "Should not be able to re-open registration");
219     }
220 
221     @Test
222     public void testOpenRegistration() throws Exception {
223         registrationService.openRegistration(TestConstants.TEST_CLIENT_NODE_GROUP, "00005");
224 
225         Node node = nodeService.findNode("00005");
226         assertEquals(node.getNodeId(), "00005", "Wrong nodeId");
227         assertEquals(node.getNodeGroupId(), TestConstants.TEST_CLIENT_NODE_GROUP, "Wrong domainName");
228         assertEquals(node.getExternalId(), "00005", "Wrong domainId");
229         assertEquals(node.getSyncURL(), null, "Wrong syncUrl");
230         assertEquals(node.getSchemaVersion(), null, "Wrong schemaVersion");
231         assertEquals(node.getDatabaseType(), null, "Wrong databaseType");
232         assertEquals(node.getDatabaseVersion(), null, "Wrong databaseVersion");
233 
234         NodeSecurity security = nodeService.findNodeSecurity("00005");
235         assertEquals(security.getNodeId(), "00005", "Wrong nodeId");
236         assertNotSame(security.getPassword(), null, "Wrong password");
237         assertEquals(security.isRegistrationEnabled(), true, "Wrong isRegistrationEnabled");
238         assertEquals(security.getRegistrationTime(), null, "Wrong registrationTime");
239     }
240 
241     @Test
242     public void testOpenRegistrationOnceAndRegisterTwice() throws Exception {
243         registrationService.openRegistration(TestConstants.TEST_CLIENT_NODE_GROUP, "00006");
244         Node node = new Node();
245         node.setNodeGroupId(TestConstants.TEST_CLIENT_NODE_GROUP);
246         node.setExternalId("00006");
247         node.setSyncURL("http://127.0.0.1");
248         ByteArrayOutputStream out = new ByteArrayOutputStream();
249         assertTrue(registrationService.registerNode(node, out, false), "Node should be able to register");
250         assertFalse(registrationService.registerNode(node, out, false), "Node should NOT be able to register");
251     }
252 
253     @Test
254     public void testOpenRegistrationTwiceAndRegisterThrice() throws Exception {
255         registrationService.openRegistration(TestConstants.TEST_CLIENT_NODE_GROUP, "00007");
256         registrationService.openRegistration(TestConstants.TEST_CLIENT_NODE_GROUP, "00007");
257         Node node = new Node();
258         node.setNodeGroupId(TestConstants.TEST_CLIENT_NODE_GROUP);
259         node.setExternalId("00007");
260         node.setSyncURL("http://0");
261         ByteArrayOutputStream out = new ByteArrayOutputStream();
262         assertTrue(registrationService.registerNode(node, out, false), "Node should be able to register");
263         assertTrue(registrationService.registerNode(node, out, false), "Node should be able to register");
264         assertFalse(registrationService.registerNode(node, out, false), "Node should NOT be able to register");
265     }
266 
267 }