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.httpinvoker;
|
10
|
|
11
|
import java.lang.reflect.Field;
|
12
|
import java.util.Map;
|
13
|
|
14
|
import org.junit.AfterClass;
|
15
|
import org.junit.Assert;
|
16
|
import org.junit.BeforeClass;
|
17
|
import org.junit.Test;
|
18
|
import org.springframework.core.io.ClassPathResource;
|
19
|
import org.springframework.core.io.Resource;
|
20
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
21
|
import org.springframework.security.core.Authentication;
|
22
|
import org.springframework.security.core.context.SecurityContextHolder;
|
23
|
import org.springframework.security.core.context.SecurityContextImpl;
|
24
|
import org.unitils.UnitilsJUnit4;
|
25
|
import org.unitils.database.DatabaseUnitils;
|
26
|
import org.unitils.database.annotations.Transactional;
|
27
|
import org.unitils.database.util.TransactionMode;
|
28
|
import org.unitils.spring.annotation.SpringApplicationContext;
|
29
|
|
30
|
import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
|
31
|
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
|
32
|
import eu.etaxonomy.taxeditor.remoting.cache.CdmTransientEntityCacher;
|
33
|
import eu.etaxonomy.taxeditor.remoting.source.CdmPersistentRemoteSource;
|
34
|
import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSource;
|
35
|
import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSourceException;
|
36
|
import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
|
37
|
import eu.etaxonomy.taxeditor.session.CdmEntitySession;
|
38
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
|
39
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
|
40
|
|
41
|
|
42
|
/**
|
43
|
* Base class for remoting tests, responsible for
|
44
|
* - starting / stop the cdm server
|
45
|
* - running some basic connection tests
|
46
|
* - setting up the remote configuration.
|
47
|
*
|
48
|
*/
|
49
|
@Transactional(TransactionMode.DISABLED)
|
50
|
@SpringApplicationContext("file:./target/classes/eu/etaxonomy/cdm/testRemotingApplicationContext.xml")
|
51
|
public abstract class BaseRemotingTest extends UnitilsJUnit4 {
|
52
|
//private static final Logger logger = Logger.getLogger(BaseRemotingTest.class);
|
53
|
|
54
|
private static final Resource TEST_REMOTE_APPLICATION_CONTEXT_RESOURCE =
|
55
|
new ClassPathResource("/eu/etaxonomy/cdm/testRemotingApplicationContext.xml");
|
56
|
|
57
|
private static CdmApplicationRemoteController remoteApplicationController;
|
58
|
private static ICdmRemoteSource cdmRemoteSource;
|
59
|
private static CdmPersistentRemoteSource remotePersistentSource;
|
60
|
|
61
|
public static boolean useManagedServer = false;
|
62
|
|
63
|
|
64
|
protected static ICdmEntitySessionManager cdmEntitySessionManager;
|
65
|
|
66
|
|
67
|
@BeforeClass
|
68
|
public static void initializeBaseRemotingTest() {
|
69
|
//NOTE: Run this the cdmTest H2 DB whenever it needs to be
|
70
|
// recreated e.g. after a model change
|
71
|
DatabaseUnitils.disableConstraints();
|
72
|
|
73
|
|
74
|
useManagedServer = (System.getProperty("use.managed.server") == null) ? useManagedServer : Boolean.valueOf(System.getProperty("use.managed.server"));
|
75
|
if(useManagedServer) {
|
76
|
try {
|
77
|
CDMServer.getInstance().start();
|
78
|
} catch (Exception e) {
|
79
|
e.printStackTrace();
|
80
|
Assert.fail("Server failed to start. Reason : " + e.getMessage());
|
81
|
}
|
82
|
|
83
|
initializeController(CDMServer.getInstance().getName(),
|
84
|
CDMServer.getInstance().getHost(),
|
85
|
CDMServer.getInstance().getPort(),
|
86
|
CDMServer.getInstance().getContextPath(),
|
87
|
NomenclaturalCode.ICNAFP,
|
88
|
"admin",
|
89
|
"00000");
|
90
|
}
|
91
|
|
92
|
|
93
|
}
|
94
|
|
95
|
|
96
|
@Test
|
97
|
public void disableConstraints() {
|
98
|
// To be run on the src/test/resources/h2/cdmTest h2 db after
|
99
|
// updating the unitils.properties 'database.url' property
|
100
|
DatabaseUnitils.disableConstraints();
|
101
|
}
|
102
|
|
103
|
public static void initializeController(String sourceName, String host, int port, String contextPath, NomenclaturalCode ncode, String username, String password) {
|
104
|
|
105
|
cdmRemoteSource = CdmRemoteSource.NewInstance(sourceName, host, port, contextPath, ncode);
|
106
|
remoteApplicationController =
|
107
|
CdmApplicationRemoteController.NewInstance(cdmRemoteSource,
|
108
|
null,
|
109
|
null);
|
110
|
|
111
|
try {
|
112
|
remotePersistentSource = CdmPersistentRemoteSource.NewInstance(sourceName);
|
113
|
} catch (CdmRemoteSourceException e) {
|
114
|
Assert.fail("Default Remote Persistent Source failed to load. Reason : " + e.getMessage());
|
115
|
}
|
116
|
cdmEntitySessionManager = getRemoteApplicationController().getCdmEntitySessionManager();
|
117
|
|
118
|
SecurityContextHolder.clearContext();
|
119
|
SecurityContextImpl sc = new SecurityContextImpl();
|
120
|
Authentication token = new UsernamePasswordAuthenticationToken(username,password);
|
121
|
Authentication authentication = getRemoteApplicationController().getAuthenticationManager().authenticate(token);
|
122
|
authentication = new UsernamePasswordAuthenticationToken(username,password, authentication.getAuthorities());
|
123
|
sc.setAuthentication(authentication);
|
124
|
|
125
|
SecurityContextHolder.setContext(sc);
|
126
|
}
|
127
|
|
128
|
|
129
|
|
130
|
protected static CdmApplicationRemoteController getRemoteApplicationController() {
|
131
|
return remoteApplicationController;
|
132
|
}
|
133
|
|
134
|
protected static ICdmRemoteSource getCdmRemoteSource() {
|
135
|
return cdmRemoteSource;
|
136
|
}
|
137
|
|
138
|
protected static CdmPersistentRemoteSource getCdmPersistentRemoteSource() {
|
139
|
return remotePersistentSource;
|
140
|
}
|
141
|
|
142
|
protected static ICdmEntitySessionManager getCdmEntitySessionManager() {
|
143
|
return cdmEntitySessionManager;
|
144
|
}
|
145
|
|
146
|
|
147
|
protected static CdmEntitySession getSession(ICdmEntitySessionEnabled sessionOwner) {
|
148
|
Map<ICdmEntitySessionEnabled, CdmEntitySession> ownerSessionMap =
|
149
|
(Map<ICdmEntitySessionEnabled, CdmEntitySession>) getFieldValueViaReflection(cdmEntitySessionManager, "ownerSessionMap");
|
150
|
return ownerSessionMap.get(sessionOwner);
|
151
|
}
|
152
|
|
153
|
protected static CdmTransientEntityCacher getActiveSession() {
|
154
|
return (CdmTransientEntityCacher) getFieldValueViaReflection(cdmEntitySessionManager, "activeSession");
|
155
|
}
|
156
|
|
157
|
protected static CdmTransientEntityCacher getCacher(ICdmEntitySessionEnabled sessionOwner) {
|
158
|
return (CdmTransientEntityCacher) getFieldValueViaReflection(getSession(sessionOwner), "cdmTransientEntityCacher");
|
159
|
}
|
160
|
|
161
|
@AfterClass
|
162
|
public static void cleanup() {
|
163
|
try {
|
164
|
CDMServer.getInstance().stop();
|
165
|
} catch (Exception e) {
|
166
|
Assert.fail("Server could not be stopped. Reason : " + e.getMessage());
|
167
|
}
|
168
|
}
|
169
|
|
170
|
|
171
|
protected static Object getFieldValueViaReflection(Object object, String fieldName) {
|
172
|
Class<?> clazz = object.getClass();
|
173
|
try {
|
174
|
Field field = clazz.getDeclaredField(fieldName);
|
175
|
field.setAccessible(true);
|
176
|
return field.get(object);
|
177
|
} catch (NoSuchFieldException e) {
|
178
|
e.printStackTrace();
|
179
|
} catch (SecurityException e) {
|
180
|
e.printStackTrace();
|
181
|
} catch (IllegalArgumentException e) {
|
182
|
e.printStackTrace();
|
183
|
} catch (IllegalAccessException e) {
|
184
|
e.printStackTrace();
|
185
|
}
|
186
|
return null;
|
187
|
}
|
188
|
|
189
|
|
190
|
|
191
|
|
192
|
}
|