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.apache.log4j.Logger;
|
15
|
import org.junit.BeforeClass;
|
16
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
17
|
import org.springframework.security.core.Authentication;
|
18
|
import org.springframework.security.core.context.SecurityContextHolder;
|
19
|
import org.springframework.security.core.context.SecurityContextImpl;
|
20
|
import org.unitils.database.DatabaseUnitils;
|
21
|
import org.unitils.database.annotations.Transactional;
|
22
|
import org.unitils.database.util.TransactionMode;
|
23
|
import org.unitils.spring.annotation.SpringApplicationContext;
|
24
|
|
25
|
import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
|
26
|
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
|
27
|
import eu.etaxonomy.cdm.api.application.CdmDataChangeService;
|
28
|
import eu.etaxonomy.cdm.api.cache.CdmServiceCacher;
|
29
|
import eu.etaxonomy.cdm.cache.CdmRemoteCacheManager;
|
30
|
import eu.etaxonomy.cdm.cache.CdmTransientEntityCacher;
|
31
|
import eu.etaxonomy.taxeditor.remoting.source.CdmPersistentRemoteSource;
|
32
|
import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSource;
|
33
|
import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
|
34
|
import eu.etaxonomy.taxeditor.session.CdmEntitySession;
|
35
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
|
36
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
|
37
|
import net.sf.ehcache.CacheManager;
|
38
|
|
39
|
|
40
|
/**
|
41
|
* Base class for remoting tests, responsible for
|
42
|
* - starting / stop the cdm server
|
43
|
* - running some basic connection tests
|
44
|
* - setting up the remote configuration.
|
45
|
*
|
46
|
*/
|
47
|
@Transactional(TransactionMode.DISABLED)
|
48
|
@SpringApplicationContext("file:./target/classes/eu/etaxonomy/cdm/testRemotingApplicationContext.xml")
|
49
|
public abstract class BaseRemotingTest extends ThreadedTest {
|
50
|
@SuppressWarnings("unused")
|
51
|
private static final Logger logger = Logger.getLogger(BaseRemotingTest.class);
|
52
|
|
53
|
private static ICdmRemoteSource cdmRemoteSource;
|
54
|
private static CdmPersistentRemoteSource remotePersistentSource;
|
55
|
protected static ICdmEntitySessionManager cdmEntitySessionManager;
|
56
|
|
57
|
@BeforeClass
|
58
|
public static void initializeBaseRemotingTest() {
|
59
|
|
60
|
DatabaseUnitils.disableConstraints();
|
61
|
|
62
|
try {
|
63
|
initializeController(sourceName,
|
64
|
host,
|
65
|
httpPort,
|
66
|
contextPath,
|
67
|
user,
|
68
|
password);
|
69
|
} catch (Exception e) {
|
70
|
e.printStackTrace();
|
71
|
// Assert.fail("Server failed to start. Reason : " + e.getMessage());
|
72
|
}
|
73
|
|
74
|
|
75
|
}
|
76
|
|
77
|
public static void emptyAllCachesExceptModelCache() {
|
78
|
CacheManager cm = CacheManager.create();
|
79
|
String[] cacheNames = CacheManager.create().getCacheNames();
|
80
|
for(String cacheName : cacheNames) {
|
81
|
if(!cacheName.equals(CdmRemoteCacheManager.CDM_MODEL_CACHE_NAME)) {
|
82
|
cm.removeCache(cacheName);
|
83
|
}
|
84
|
}
|
85
|
}
|
86
|
|
87
|
|
88
|
public static void initializeController(String sourceName, String host, int port, String contextPath, String username, String password) {
|
89
|
|
90
|
if(CdmApplicationState.getCurrentAppConfig() != null) {
|
91
|
return;
|
92
|
}
|
93
|
cdmRemoteSource = CdmRemoteSource.NewInstance(sourceName, host, port, contextPath);
|
94
|
CdmApplicationRemoteController remoteApplicationController =
|
95
|
CdmApplicationRemoteController.NewInstance(cdmRemoteSource,
|
96
|
null,
|
97
|
null);
|
98
|
CdmApplicationState.setCurrentAppConfig(remoteApplicationController);
|
99
|
CdmApplicationState.setCdmServiceCacher(new CdmServiceCacher());
|
100
|
|
101
|
cdmEntitySessionManager = getRemoteApplicationController().getCdmEntitySessionManager();
|
102
|
|
103
|
CdmApplicationState.setCurrentDataChangeService(new CdmDataChangeService());
|
104
|
|
105
|
authenticate(username, password);
|
106
|
|
107
|
}
|
108
|
|
109
|
protected static void authenticate(String username, String password) {
|
110
|
|
111
|
//FIXME:Remoting the authentication code should be replaced by a method call which actually
|
112
|
// does the authentication in the editor code so that the 'real' authentication can be tested
|
113
|
SecurityContextHolder.clearContext();
|
114
|
SecurityContextImpl sc = new SecurityContextImpl();
|
115
|
Authentication token = new UsernamePasswordAuthenticationToken(username,password);
|
116
|
Authentication authentication = getRemoteApplicationController().getAuthenticationManager().authenticate(token);
|
117
|
authentication = new UsernamePasswordAuthenticationToken(username,password, authentication.getAuthorities());
|
118
|
sc.setAuthentication(authentication);
|
119
|
|
120
|
SecurityContextHolder.setContext(sc);
|
121
|
CdmApplicationState.setCurrentSecurityContext(SecurityContextHolder.getContext());
|
122
|
|
123
|
}
|
124
|
|
125
|
|
126
|
protected static CdmApplicationRemoteController getRemoteApplicationController() {
|
127
|
return (CdmApplicationRemoteController) CdmApplicationState.getCurrentAppConfig();
|
128
|
}
|
129
|
|
130
|
protected static ICdmRemoteSource getCdmRemoteSource() {
|
131
|
return cdmRemoteSource;
|
132
|
}
|
133
|
|
134
|
protected static CdmPersistentRemoteSource getCdmPersistentRemoteSource() {
|
135
|
return remotePersistentSource;
|
136
|
}
|
137
|
|
138
|
protected static ICdmEntitySessionManager getCdmEntitySessionManager() {
|
139
|
return cdmEntitySessionManager;
|
140
|
}
|
141
|
|
142
|
|
143
|
protected static CdmEntitySession getSession(ICdmEntitySessionEnabled sessionOwner) {
|
144
|
Map<ICdmEntitySessionEnabled, CdmEntitySession> ownerSessionMap =
|
145
|
(Map<ICdmEntitySessionEnabled, CdmEntitySession>) getFieldValueViaReflection(cdmEntitySessionManager, "ownerSessionMap");
|
146
|
return ownerSessionMap.get(sessionOwner);
|
147
|
}
|
148
|
|
149
|
protected static CdmEntitySession getActiveSession() {
|
150
|
return ((InheritableThreadLocal<CdmEntitySession>) getFieldValueViaReflection(cdmEntitySessionManager, "tlActiveSession")).get();
|
151
|
}
|
152
|
|
153
|
protected static eu.etaxonomy.cdm.cache.CdmTransientEntityCacher getCacher(ICdmEntitySessionEnabled sessionOwner) {
|
154
|
return (CdmTransientEntityCacher) getFieldValueViaReflection(getSession(sessionOwner), "cdmTransientEntityCacher");
|
155
|
}
|
156
|
|
157
|
|
158
|
protected static Object getFieldValueViaReflection(Object object, String fieldName) {
|
159
|
Class<?> clazz = object.getClass();
|
160
|
try {
|
161
|
Field field = clazz.getDeclaredField(fieldName);
|
162
|
field.setAccessible(true);
|
163
|
return field.get(object);
|
164
|
} catch (NoSuchFieldException e) {
|
165
|
e.printStackTrace();
|
166
|
} catch (SecurityException e) {
|
167
|
e.printStackTrace();
|
168
|
} catch (IllegalArgumentException e) {
|
169
|
e.printStackTrace();
|
170
|
} catch (IllegalAccessException e) {
|
171
|
e.printStackTrace();
|
172
|
}
|
173
|
return null;
|
174
|
}
|
175
|
|
176
|
|
177
|
protected static void authenticateDefaultUser() {
|
178
|
authenticate(user, password);
|
179
|
}
|
180
|
|
181
|
|
182
|
}
|