This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
ZanataRestTest.java
195 lines (172 loc) · 7.14 KB
/
ZanataRestTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package org.zanata;
import java.net.URI;
import java.util.HashSet;
import java.util.Set;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import org.hibernate.Session;
import org.jboss.resteasy.client.ClientRequestFactory;
import org.jboss.resteasy.client.core.executors.InMemoryClientExecutor;
import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.mock.MockDispatcherFactory;
import org.jboss.resteasy.spi.ResourceFactory;
import org.jboss.seam.security.management.JpaIdentityStore;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.zanata.config.JndiBackedConfig;
import org.zanata.model.HAccount;
import org.zanata.model.HPerson;
import org.zanata.rest.AuthorizationExceptionMapper;
import org.zanata.rest.HibernateExceptionMapper;
import org.zanata.rest.HibernateValidationInterceptor;
import org.zanata.rest.ConstraintViolationExceptionMapper;
import org.zanata.rest.NoSuchEntityExceptionMapper;
import org.zanata.rest.NotLoggedInExceptionMapper;
import org.zanata.limits.RateLimitManager;
import org.zanata.rest.ZanataServiceExceptionMapper;
import org.zanata.rest.client.TraceDebugInterceptor;
import org.zanata.seam.SeamAutowire;
import org.zanata.security.AuthenticationType;
import com.google.gwt.thirdparty.guava.common.collect.Sets;
import static org.mockito.Mockito.when;
public abstract class ZanataRestTest extends ZanataDbunitJpaTest {
protected static final URI MOCK_BASE_URI = URI.create("http://mockhost");
private ClientRequestFactory clientRequestFactory;
private final SeamAutowire seamAutowire = SeamAutowire.instance();
protected final Set<Class<? extends ExceptionMapper<? extends Throwable>>> exceptionMappers =
new HashSet<Class<? extends ExceptionMapper<? extends Throwable>>>();
protected final Set<Object> resources = new HashSet<Object>();
protected final Set<Class<?>> providers = new HashSet<Class<?>>();
protected final Set<Object> providerInstances = new HashSet<Object>();
@Mock
private JndiBackedConfig jndiBackedConfig;
@BeforeMethod
public final void prepareRestEasyFramework() {
MockitoAnnotations.initMocks(this);
when(jndiBackedConfig.getEnabledAuthenticationPolicies()).thenReturn(
Sets.newHashSet(AuthenticationType.INTERNAL.name()));
Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
prepareSeamAutowire();
prepareAccount();
prepareResources();
prepareExceptionMappers();
prepareProviders();
// register resources
for (Object obj : resources) {
ResourceFactory factory = new MockResourceFactory(obj);
dispatcher.getRegistry().addResourceFactory(factory);
}
// register Exception Mappers
for (Class<? extends ExceptionMapper<? extends Throwable>> mapper : exceptionMappers) {
dispatcher.getProviderFactory().addExceptionMapper(mapper);
}
// register Providers
for (Class<?> provider : providers) {
dispatcher.getProviderFactory().registerProvider(provider);
}
// register Provider instances
for (Object providerInstance : providerInstances) {
dispatcher.getProviderFactory().registerProviderInstance(
providerInstance);
}
InMemoryClientExecutor executor =
new InMemoryClientExecutor(dispatcher);
executor.setBaseUri(MOCK_BASE_URI);
clientRequestFactory =
new ClientRequestFactory(executor, MOCK_BASE_URI);
}
private void prepareAccount() {
Session session = getSession();
String username = "anAuthenticatedAccount";
HAccount account = (HAccount) session.byNaturalId(HAccount.class).using("username", username).load();
if (account == null) {
account = new HAccount();
account.setUsername(username);
HPerson person = new HPerson();
person.setEmail("email@example.com");
person.setName("aPerson");
person = (HPerson) session.merge(person);
account.setPerson(person);
account = (HAccount) session.merge(account);
}
seamAutowire.use(JpaIdentityStore.AUTHENTICATED_USER, account);
}
@AfterMethod
public final void cleanUpRestEasyFramework() {
exceptionMappers.clear();
resources.clear();
providers.clear();
providerInstances.clear();
}
/**
* Clients should add instances of the tested server side Resource to the
* protected resources set within this method.
*/
protected abstract void prepareResources();
/**
* Override this to add custom server-side ExceptionMappers for the test, by
* configuring the protected exceptionMappers set.
*/
protected void prepareExceptionMappers() {
exceptionMappers.add(AuthorizationExceptionMapper.class);
exceptionMappers.add(HibernateExceptionMapper.class);
exceptionMappers.add(ConstraintViolationExceptionMapper.class);
exceptionMappers.add(NoSuchEntityExceptionMapper.class);
exceptionMappers.add(NotLoggedInExceptionMapper.class);
exceptionMappers.add(ZanataServiceExceptionMapper.class);
}
/**
* Override this method to add custom server-side Providers for the test.
* Note: Provider classes should be annotated with the {@link Provider} and
* any other relevant annotations.
*/
protected void prepareProviders() {
ValidatorFactory validatorFactory =
Validation.buildDefaultValidatorFactory();
seamAutowire.use("validatorFactory", validatorFactory).use("validator",
validatorFactory.getValidator());
providerInstances.add(seamAutowire
.autowire(HibernateValidationInterceptor.class));
providers.add(TraceDebugInterceptor.class);
}
/**
* Override this method to add custom Seam autowire preparations.
*/
protected void prepareSeamAutowire() {
seamAutowire
.reset()
.ignoreNonResolvable();
}
/**
* Retrieve the configured request factory
*
* @return a ClientRequestFactory configured for your environment.
*/
protected final ClientRequestFactory getClientRequestFactory() {
return clientRequestFactory;
}
/**
* Creates a URI suitable for passing to ClientRequestFactory for a given
* resource.
*
* @param resourcePath
* the class-level @Path structure for the resource being tested.
* @return a URI suitable for passing to ClientRequestFactory for the
* resource being tested.
*/
protected final URI createBaseURI(String resourcePath) {
return MOCK_BASE_URI.resolve(resourcePath);
}
/**
* Returns this tests SeamAutowire instance.
*
* @return This test's SeamAutowire instance to be used in tests.
*/
protected SeamAutowire getSeamAutowire() {
return seamAutowire;
}
}