c601d3ad40196558e4b5da19f7f531d79d47f094
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / test / java / eu / etaxonomy / taxeditor / remoting / CdmPersistentRemoteSourceTest.java
1 /**
2 * Copyright (C) 2014 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.remoting;
10
11
12 import java.util.Map;
13
14 import org.apache.log4j.Level;
15 import org.apache.log4j.Logger;
16 import org.junit.Assert;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.unitils.UnitilsJUnit4;
20
21 import static org.hamcrest.CoreMatchers.*;
22
23 import eu.etaxonomy.cdm.config.CdmPersistentSourceUtils;
24 import eu.etaxonomy.cdm.config.CdmPersistentXMLSource.CdmSourceProperties;
25 import eu.etaxonomy.cdm.config.CdmSourceException;
26 import eu.etaxonomy.cdm.model.metadata.CdmMetaData.MetaDataPropertyName;
27 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
28 import eu.etaxonomy.cdm.remote.CdmPersistentRemoteSource;
29 import eu.etaxonomy.cdm.remote.CdmRemoteSource;
30 import eu.etaxonomy.cdm.remote.CdmRemoteSourceException;
31 import eu.etaxonomy.cdm.remote.ICdmRemoteSource;
32
33 /**
34 * Class to test the cdm persistent source functionality, including
35 * initialising / saving / updating and deleting
36 *
37 * Corresponding test sources settings can be found at
38 * /src/text/resources/eu.etaxonomy.cdm/cdm.datasources.xml
39 *
40 */
41 public class CdmPersistentRemoteSourceTest extends UnitilsJUnit4 {
42 private static final Logger logger = Logger.getLogger(CdmPersistentRemoteSourceTest.class);
43
44 private CdmPersistentRemoteSource remotePersistentSource;
45
46 @BeforeClass
47 public static void initialize() {
48 Logger.getRootLogger().setLevel(Level.INFO);
49
50 }
51
52 @Test
53 public void whenLoadingExistingCompleteRemoteSourceThenLoadIsSuccessful() {
54
55 try {
56 remotePersistentSource = CdmPersistentRemoteSource.NewInstance("default");
57
58 String expectedServer = "127.0.0.1";
59 String expectedPort = "8080";
60 String expectedContextPath = "";
61 logger.info("Default remote source server : " + remotePersistentSource.getServer());
62 Assert.assertTrue("Default server should be " + expectedServer,
63 expectedServer.equals(remotePersistentSource.getServer()));
64
65 logger.info("Default remote source port : " + remotePersistentSource.getPort());
66 Assert.assertTrue("Default port should be " + 8080,
67 expectedPort.equals(String.valueOf(remotePersistentSource.getPort())));
68
69 logger.info("Default remote source context path : " + remotePersistentSource.getContextPath());
70 Assert.assertTrue("Default remote source context path should be " + expectedContextPath,
71 expectedContextPath.equals(remotePersistentSource.getContextPath()));
72
73 logger.info("Default remote source nomenclatural code : " + remotePersistentSource.getNomenclaturalCode().toString());
74 Assert.assertTrue("Default NomenclaturalCode should be ICNAFP",
75 NomenclaturalCode.ICNAFP.equals(remotePersistentSource.getNomenclaturalCode()));
76
77 } catch (CdmRemoteSourceException e) {
78 Assert.fail("Initialising an existing remote source should not throw an exception");
79 }
80 }
81
82
83
84
85 @Test
86 public void whenConnectingToAnActiveServerThenConnectSuccessfully() {
87 // check if active server throws the right exception
88 try {
89 remotePersistentSource = CdmPersistentRemoteSource.NewInstance("default");
90
91 String dbSchemaVersion = "";
92 try {
93 dbSchemaVersion = remotePersistentSource.getDbSchemaVersion();
94 } catch (CdmSourceException e) {
95 Assert.fail("getDbSchemaVersion() on active cdm server should not have thrown CdmSourceException");
96 }
97 logger.info("dbSchemaVersion is " + dbSchemaVersion);
98
99
100 boolean isDbEmpty = false;
101 try {
102 isDbEmpty = remotePersistentSource.isDbEmpty();
103 } catch (CdmSourceException e) {
104 Assert.fail("isDbEmpty() on active cdm server should not have thrown CdmSourceException");
105 }
106 Assert.assertFalse(isDbEmpty);
107
108
109 boolean check = false;
110 try {
111 check = remotePersistentSource.checkConnection();
112 } catch (CdmSourceException e) {
113 Assert.fail("checkConnection() on active cdm server should not have thrown CdmSourceException");
114 }
115 Assert.assertTrue(check);
116 } catch (CdmRemoteSourceException e1) {
117 // TODO Auto-generated catch block
118 e1.printStackTrace();
119 }
120
121 }
122
123 @Test
124 public void whenRequestingMetadatFromRemoteSourceThenMetaDataIsRetrieved() {
125 try {
126 remotePersistentSource = CdmPersistentRemoteSource.NewInstance("default");
127 Map<MetaDataPropertyName, String> cdmMetaDataMap = remotePersistentSource.getMetaDataMap();
128 for(MetaDataPropertyName mdpn : cdmMetaDataMap.keySet()) {
129 String value = cdmMetaDataMap.get(mdpn);
130 logger.info("MetaData Property " + mdpn.name() + " has value " + value);
131 Assert.assertNotNull("Meta Data Value for property for " + mdpn.name() + " should not be null", value);
132 }
133
134 } catch (CdmRemoteSourceException e) {
135 Assert.fail("Initialising an existing remote source should not throw an exception");
136 } catch (CdmSourceException e) {
137 // TODO Auto-generated catch block
138 e.printStackTrace();
139 }
140 }
141
142 @Test
143 public void whenLoadingNonExistingRemoteSourceThenThrowException() {
144 try {
145 remotePersistentSource = CdmPersistentRemoteSource.NewInstance("nonExistingSource");
146 Assert.fail("Initialising a non-existing remote source should throw an exception");
147 } catch (CdmRemoteSourceException e) {
148 logger.info(e.getMessage());
149 }
150 }
151
152 @Test
153 public void whenLoadingExistingIncompleteRemoteSourceThenThrowException() {
154 try {
155 remotePersistentSource = CdmPersistentRemoteSource.NewInstance("incomplete");
156 Assert.fail("Initialising an incomplete remote source should throw an exception");
157 } catch (CdmRemoteSourceException e) {
158 Assert.assertThat(e.getMessage(), containsString("Required property '" + CdmSourceProperties.SERVER.toString() + "' is missing"));
159 }
160 }
161
162 @Test
163 public void whenSavingUpdatingDeletingNewCompleteRemoteSourceThenSaveUpdateDeleteIsSuccessful() {
164 String strRemoteSource = "local";
165 Assert.assertFalse("New remote source '" + strRemoteSource + "' should not yet exist in source settings file",
166 CdmPersistentRemoteSource.exists(strRemoteSource));
167 try {
168 ICdmRemoteSource remoteSource =
169 CdmRemoteSource.NewInstance(strRemoteSource,
170 "127.0.0.1",
171 8080,
172 "col",
173 NomenclaturalCode.ICNAFP);
174 remotePersistentSource = CdmPersistentRemoteSource.save(strRemoteSource,remoteSource);
175
176 logger.info("Server : " + remotePersistentSource.getServer());
177 Assert.assertTrue("Server value should be equal",
178 remoteSource.getServer().equals(remotePersistentSource.getServer()));
179
180 logger.info("Port : " + remotePersistentSource.getPort());
181 Assert.assertTrue("Port value should be equal",
182 remoteSource.getPort() == remotePersistentSource.getPort());
183
184 logger.info("Context Path : " + remotePersistentSource.getContextPath());
185 Assert.assertTrue("Context Path value should be equal",
186 remoteSource.getContextPath().equals(remotePersistentSource.getContextPath()));
187
188 logger.info("Nomenclatural Code : " + remotePersistentSource.getNomenclaturalCode().name());
189 Assert.assertTrue("Nomenclatural Code value should be equal",
190 remoteSource.getNomenclaturalCode().name().equals(remotePersistentSource.getNomenclaturalCode().name()));
191
192 Assert.assertTrue("New remote source '" + strRemoteSource + "' should be saved in source settings file",
193 CdmPersistentRemoteSource.exists(strRemoteSource));
194
195 remoteSource = CdmRemoteSource.NewInstance(strRemoteSource,
196 "127.0.0.55",
197 9090,
198 "cyprus",
199 NomenclaturalCode.ICNB);
200 remotePersistentSource = CdmPersistentRemoteSource.update(strRemoteSource,remoteSource);
201
202 logger.info("Server : " + remotePersistentSource.getServer());
203 Assert.assertTrue("Server value should be equal",
204 remoteSource.getServer().equals(remotePersistentSource.getServer()));
205
206 logger.info("Port : " + remotePersistentSource.getPort());
207 Assert.assertTrue("Port value should be equal",
208 remoteSource.getPort() == remotePersistentSource.getPort());
209
210 logger.info("Context Path : " + remotePersistentSource.getContextPath());
211 Assert.assertTrue("Context Path value should be equal",
212 remoteSource.getContextPath().equals(remotePersistentSource.getContextPath()));
213
214 logger.info("Nomenclatural Code : " + remotePersistentSource.getNomenclaturalCode().name());
215 Assert.assertTrue("Nomenclatural Code value should be equal",
216 remoteSource.getNomenclaturalCode().name().equals(remotePersistentSource.getNomenclaturalCode().name()));
217
218 Assert.assertTrue("New remote source '" + strRemoteSource + "' should be saved in source settings file",
219 CdmPersistentRemoteSource.exists(strRemoteSource));
220
221 CdmPersistentSourceUtils.delete(remotePersistentSource);
222
223 Assert.assertFalse("New remote source '" + strRemoteSource + "' should no longer exist in source settings file",
224 CdmPersistentRemoteSource.exists(strRemoteSource));
225
226 } catch (CdmRemoteSourceException e) {
227 Assert.fail("Saving a new complete remote source should not throw an exception");
228 logger.info(e.getMessage());
229 }
230 }
231
232
233
234 }