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