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