Project

General

Profile

Download (7.15 KB) Statistics
| Branch: | Tag: | Revision:
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.cdm.model.name.NomenclaturalCode;
32
import eu.etaxonomy.taxeditor.remoting.cache.CdmRemoteCacheManager;
33
import eu.etaxonomy.taxeditor.remoting.cache.CdmTransientEntityCacher;
34
import eu.etaxonomy.taxeditor.remoting.source.CdmPersistentRemoteSource;
35
import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSource;
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 ThreadedTest {
52
    @SuppressWarnings("unused")
53
	private static final Logger logger = Logger.getLogger(BaseRemotingTest.class);
54

    
55
    private static ICdmRemoteSource cdmRemoteSource;
56
    private static CdmPersistentRemoteSource remotePersistentSource;
57
    protected static ICdmEntitySessionManager cdmEntitySessionManager;
58

    
59
    @BeforeClass
60
    public static void initializeBaseRemotingTest() {
61

    
62
        DatabaseUnitils.disableConstraints();
63

    
64
        try {
65
            initializeController(sourceName,
66
                    host,
67
                    httpPort,
68
                    contextPath,
69
                    NomenclaturalCode.ICNAFP,
70
                    user,
71
                    password);
72
        } catch (Exception e) {
73
            e.printStackTrace();
74
           // Assert.fail("Server failed to start. Reason : " + e.getMessage());
75
        }
76

    
77

    
78
    }
79

    
80
    public static void emptyAllCachesExceptModelCache() {
81
        CacheManager cm = CacheManager.create();
82
        String[] cacheNames = CacheManager.create().getCacheNames();
83
        for(String cacheName : cacheNames) {
84
            if(!cacheName.equals(CdmRemoteCacheManager.CDM_MODEL_CACHE_NAME)) {
85
                cm.removeCache(cacheName);
86
            }
87
        }
88
    }
89

    
90

    
91
    public static void initializeController(String sourceName, String host, int port, String contextPath, NomenclaturalCode ncode, String username, String password) {
92

    
93
        if(CdmApplicationState.getCurrentAppConfig() != null) {
94
            return;
95
        }
96
        cdmRemoteSource = CdmRemoteSource.NewInstance(sourceName, host, port, contextPath, ncode);
97
        CdmApplicationRemoteController remoteApplicationController =
98
                CdmApplicationRemoteController.NewInstance(cdmRemoteSource,
99
                        null,
100
                        null);
101
        CdmApplicationState.setCurrentAppConfig(remoteApplicationController);
102
        CdmApplicationState.setCdmServiceCacher(new CdmServiceCacher());
103

    
104
        cdmEntitySessionManager = getRemoteApplicationController().getCdmEntitySessionManager();
105

    
106
        CdmApplicationState.setCurrentDataChangeService(new CdmDataChangeService());
107

    
108
        authenticate(username, password);
109

    
110
    }
111

    
112
    protected static void authenticate(String username, String password) {
113

    
114
        //FIXME:Remoting the authentication code should be replaced by a method call which actually
115
        // does the authentication in the editor code so that the 'real' authentication can be tested
116
        SecurityContextHolder.clearContext();
117
        SecurityContextImpl sc = new SecurityContextImpl();
118
        Authentication token = new UsernamePasswordAuthenticationToken(username,password);
119
        Authentication authentication = getRemoteApplicationController().getAuthenticationManager().authenticate(token);
120
        authentication = new UsernamePasswordAuthenticationToken(username,password, authentication.getAuthorities());
121
        sc.setAuthentication(authentication);
122

    
123
        SecurityContextHolder.setContext(sc);
124
        CdmApplicationState.setCurrentSecurityContext(SecurityContextHolder.getContext());
125

    
126
    }
127

    
128

    
129
    protected static CdmApplicationRemoteController getRemoteApplicationController() {
130
        return (CdmApplicationRemoteController) CdmApplicationState.getCurrentAppConfig();
131
    }
132

    
133
    protected static ICdmRemoteSource getCdmRemoteSource() {
134
        return cdmRemoteSource;
135
    }
136

    
137
    protected static CdmPersistentRemoteSource getCdmPersistentRemoteSource() {
138
        return remotePersistentSource;
139
    }
140

    
141
    protected static ICdmEntitySessionManager getCdmEntitySessionManager() {
142
        return cdmEntitySessionManager;
143
    }
144

    
145

    
146
    protected static CdmEntitySession getSession(ICdmEntitySessionEnabled sessionOwner) {
147
        Map<ICdmEntitySessionEnabled, CdmEntitySession> ownerSessionMap =
148
                (Map<ICdmEntitySessionEnabled, CdmEntitySession>) getFieldValueViaReflection(cdmEntitySessionManager, "ownerSessionMap");
149
        return ownerSessionMap.get(sessionOwner);
150
    }
151

    
152
    protected static CdmEntitySession getActiveSession() {
153
        return ((InheritableThreadLocal<CdmEntitySession>) getFieldValueViaReflection(cdmEntitySessionManager, "tlActiveSession")).get();
154
    }
155

    
156
    protected static CdmTransientEntityCacher getCacher(ICdmEntitySessionEnabled sessionOwner) {
157
        return (CdmTransientEntityCacher) getFieldValueViaReflection(getSession(sessionOwner), "cdmTransientEntityCacher");
158
    }
159

    
160

    
161
    protected static Object getFieldValueViaReflection(Object object, String fieldName) {
162
        Class<?> clazz = object.getClass();
163
        try {
164
            Field field = clazz.getDeclaredField(fieldName);
165
            field.setAccessible(true);
166
            return field.get(object);
167
        } catch (NoSuchFieldException e) {
168
            e.printStackTrace();
169
        } catch (SecurityException e) {
170
            e.printStackTrace();
171
        } catch (IllegalArgumentException e) {
172
            e.printStackTrace();
173
        } catch (IllegalAccessException e) {
174
            e.printStackTrace();
175
        }
176
        return null;
177
    }
178

    
179

    
180
    protected static void authenticateDefaultUser() {
181
        authenticate(user, password);
182
    }
183

    
184

    
185
}
(1-1/8)