Project

General

Profile

Download (9.58 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.io.File;
12
import java.io.InputStream;
13
import java.lang.reflect.Field;
14
import java.net.URL;
15
import java.util.Map;
16
import java.util.Properties;
17

    
18
import org.apache.log4j.Logger;
19
import org.eclipse.core.runtime.FileLocator;
20
import org.eclipse.core.runtime.Platform;
21
import org.junit.AfterClass;
22
import org.junit.Assert;
23
import org.junit.BeforeClass;
24
import org.junit.Test;
25
import org.osgi.framework.Bundle;
26
import org.springframework.core.io.ClassPathResource;
27
import org.springframework.core.io.Resource;
28
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
29
import org.springframework.security.core.Authentication;
30
import org.springframework.security.core.context.SecurityContextHolder;
31
import org.springframework.security.core.context.SecurityContextImpl;
32
import org.unitils.UnitilsJUnit4;
33
import org.unitils.database.DatabaseUnitils;
34
import org.unitils.database.annotations.Transactional;
35
import org.unitils.database.util.TransactionMode;
36
import org.unitils.spring.annotation.SpringApplicationContext;
37

    
38
import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
39
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
40
import eu.etaxonomy.taxeditor.remoting.cache.CdmTransientEntityCacher;
41
import eu.etaxonomy.taxeditor.remoting.source.CdmPersistentRemoteSource;
42
import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSource;
43
import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSourceException;
44
import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
45
import eu.etaxonomy.taxeditor.session.CdmEntitySession;
46
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
47
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
48

    
49

    
50
/**
51
 * Base class for remoting tests, responsible for
52
 * - starting / stop the cdm server
53
 * - running some basic connection tests
54
 * - setting up the remote configuration.
55
 *
56
 */
57
@Transactional(TransactionMode.DISABLED)
58
@SpringApplicationContext("file:./target/classes/eu/etaxonomy/cdm/testRemotingApplicationContext.xml")
59
public class BaseRemotingTest extends UnitilsJUnit4 {
60
    private static final Logger logger = Logger.getLogger(BaseRemotingTest.class);
61

    
62

    
63
    public static final Resource SERVER_PROPERTIES_FILE =
64
            new ClassPathResource("server.properties");
65

    
66
    public static final Resource EDITOR_DATASOURCES_FILE =
67
            new ClassPathResource(".cdmLibrary/writableResources/cdm.datasources.xml");
68

    
69
    private static CdmApplicationRemoteController remoteApplicationController;
70
    private static ICdmRemoteSource cdmRemoteSource;
71
    private static CdmPersistentRemoteSource remotePersistentSource;
72

    
73
    public static boolean useManagedServer = true;
74

    
75

    
76
    protected static ICdmEntitySessionManager cdmEntitySessionManager;
77

    
78
    private final static String DEFAULT_USER = "admin";
79
    private final static String DEFAULT_PASSWORD = "00000";
80

    
81
    private static String userHomeKey = "user.home";
82

    
83
    private static String user = DEFAULT_USER;
84
    private static String password = DEFAULT_PASSWORD;
85

    
86
    @BeforeClass
87
    public static void initializeBaseRemotingTest() {
88

    
89
        //NOTE: Run this the cdmTest H2 DB whenever it needs to be
90
        //      recreated e.g. after a model change
91
        DatabaseUnitils.disableConstraints();
92

    
93
        try {
94
            String userHomeDirPath;
95
            Bundle bundle = Platform.getBundle("eu.etaxonomy.taxeditor.test");
96
            // if bundle is null then we are running as junit test in eclipse
97
            if(bundle == null) {
98
                userHomeDirPath = EDITOR_DATASOURCES_FILE.getFile().getParentFile().getParentFile().getParent();
99
            } else {
100
                // ... else we are running in maven which requires loading the dir via the
101
                // classpath bundle
102
                URL userHomeDirURL = bundle.getEntry("src/test/resources");
103
                File userHomeDir = new File(FileLocator.resolve(userHomeDirURL).toURI());
104
                userHomeDirPath = userHomeDir.getAbsolutePath();
105
            }
106

    
107

    
108
            logger.info("Setting user.home to " + userHomeDirPath);
109
            System.setProperty(userHomeKey, userHomeDirPath);
110

    
111
            CDMServer cdmServer = CDMServer.getInstance();
112

    
113

    
114
            Properties prop = new Properties();
115
            InputStream inputStream = SERVER_PROPERTIES_FILE.getInputStream();
116

    
117
            if (inputStream != null) {
118
                prop.load(inputStream);
119
                inputStream.close();
120
            }
121

    
122
            if(prop.getProperty("httpPort") != null) {
123
                cdmServer.setHttpPort(Integer.valueOf(prop.getProperty("httpPort")));
124
            }
125

    
126
            if(prop.getProperty("stopPort") != null) {
127
                cdmServer.setStopPort(Integer.valueOf(prop.getProperty("stopPort")));
128
            }
129

    
130
            if(prop.getProperty("stopKey") != null) {
131
                cdmServer.setStopKey(prop.getProperty("stopKey"));
132
            }
133

    
134
            if(prop.getProperty("user") != null) {
135
                user = prop.getProperty("user");
136
            }
137

    
138
            if(prop.getProperty("password") != null) {
139
                password = prop.getProperty("password");
140
            }
141

    
142
            cdmServer.start();
143
            initializeController(CDMServer.getInstance().getName(),
144
                    CDMServer.getInstance().getHost(),
145
                    CDMServer.getInstance().getPort(),
146
                    CDMServer.getInstance().getContextPath(),
147
                    NomenclaturalCode.ICNAFP,
148
                    user,
149
                    password);
150
        } catch (Exception e) {
151
            e.printStackTrace();
152
           // Assert.fail("Server failed to start. Reason : " + e.getMessage());
153
        }
154
    }
155

    
156

    
157
    @Test
158
    public void disableConstraints() {
159
        // To be run on the src/test/resources/h2/cdmTest h2 db after
160
        // updating the unitils.properties 'database.url' property
161
        DatabaseUnitils.disableConstraints();
162
    }
163

    
164

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

    
167
        if(remoteApplicationController != null) {
168
            return;
169
        }
170
        cdmRemoteSource = CdmRemoteSource.NewInstance(sourceName, host, port, contextPath, ncode);
171
        remoteApplicationController =
172
                CdmApplicationRemoteController.NewInstance(cdmRemoteSource,
173
                        null,
174
                        null);
175

    
176
        try {
177
            remotePersistentSource = CdmPersistentRemoteSource.NewInstance(sourceName);
178
        } catch (CdmRemoteSourceException e) {
179
            Assert.fail("Default Remote Persistent Source failed to load. Reason : " + e.getMessage());
180
        }
181
        cdmEntitySessionManager = getRemoteApplicationController().getCdmEntitySessionManager();
182

    
183
        SecurityContextHolder.clearContext();
184
        SecurityContextImpl sc = new SecurityContextImpl();
185
        Authentication token = new UsernamePasswordAuthenticationToken(username,password);
186
        Authentication authentication = getRemoteApplicationController().getAuthenticationManager().authenticate(token);
187
        authentication = new UsernamePasswordAuthenticationToken(username,password, authentication.getAuthorities());
188
        sc.setAuthentication(authentication);
189

    
190
        SecurityContextHolder.setContext(sc);
191
    }
192

    
193

    
194

    
195
    protected static CdmApplicationRemoteController getRemoteApplicationController() {
196
        return remoteApplicationController;
197
    }
198

    
199
    protected static ICdmRemoteSource getCdmRemoteSource() {
200
        return cdmRemoteSource;
201
    }
202

    
203
    protected static CdmPersistentRemoteSource getCdmPersistentRemoteSource() {
204
        return remotePersistentSource;
205
    }
206

    
207
    protected static ICdmEntitySessionManager getCdmEntitySessionManager() {
208
        return cdmEntitySessionManager;
209
    }
210

    
211

    
212
    protected static CdmEntitySession getSession(ICdmEntitySessionEnabled sessionOwner) {
213
        Map<ICdmEntitySessionEnabled, CdmEntitySession> ownerSessionMap =
214
                (Map<ICdmEntitySessionEnabled, CdmEntitySession>) getFieldValueViaReflection(cdmEntitySessionManager, "ownerSessionMap");
215
        return ownerSessionMap.get(sessionOwner);
216
    }
217

    
218
    protected static CdmTransientEntityCacher getActiveSession() {
219
        return (CdmTransientEntityCacher) getFieldValueViaReflection(cdmEntitySessionManager, "activeSession");
220
    }
221

    
222
    protected static CdmTransientEntityCacher getCacher(ICdmEntitySessionEnabled sessionOwner) {
223
        return (CdmTransientEntityCacher) getFieldValueViaReflection(getSession(sessionOwner), "cdmTransientEntityCacher");
224
    }
225

    
226
    @AfterClass
227
    public static void cleanup() {
228
        try {
229
            CDMServer.getInstance().stop();
230
        } catch (Exception e) {
231
            Assert.fail("Server could not be stopped. Reason : " + e.getMessage());
232
        }
233
    }
234

    
235

    
236
    protected static Object getFieldValueViaReflection(Object object, String fieldName) {
237
        Class<?> clazz = object.getClass();
238
        try {
239
            Field field = clazz.getDeclaredField(fieldName);
240
            field.setAccessible(true);
241
            return field.get(object);
242
        } catch (NoSuchFieldException e) {
243
            e.printStackTrace();
244
        } catch (SecurityException e) {
245
            e.printStackTrace();
246
        } catch (IllegalArgumentException e) {
247
            e.printStackTrace();
248
        } catch (IllegalAccessException e) {
249
            e.printStackTrace();
250
        }
251
        return null;
252
    }
253

    
254

    
255

    
256

    
257
}
(1-1/7)