/
TestThriftServiceExporter.java
164 lines (131 loc) · 5.17 KB
/
TestThriftServiceExporter.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
package org.springframework.remoting.thrift;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.thrift.TException;
import org.apache.thrift.transport.THttpClient;
import org.apache.thrift.transport.TTransport;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mortbay.jetty.Server;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.obm.thrift.crm.Crm;
import org.springframework.obm.thrift.crm.Customer;
import org.springframework.util.DispatcherServletJettyConfigurationCallback;
import org.springframework.util.EndpointTestUtils;
import org.springframework.util.IntegrationTestUtils;
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
/**
* Tests the {@link ThriftServiceExporter} which is a Spring Web {@link org.springframework.web.HttpRequestHandler}
* that can satisfy requests over HTTP in the native Thrift RPC format.
* <p/>
* This is <EM>not</EM> a REST service, however. It just uses HTTP as a transport.
*
* @author Josh Long
*/
public class TestThriftServiceExporter {
private Log log = LogFactory.getLog(getClass());
private Server jettyServer;
@Before
public void before() throws Throwable {
jettyServer = EndpointTestUtils.serve(new DispatcherServletJettyConfigurationCallback(ExporterConfiguration.class));
jettyServer.start();
if (log.isDebugEnabled()) {
log.debug("started jetty server");
}
}
@After
public void after() throws Throwable {
IntegrationTestUtils.stopServerQuietly(this.jettyServer);
if (log.isDebugEnabled()) {
log.debug("stopped jetty server");
}
}
@Test
public void testCreateCustomerOnRpcClient() throws Throwable {
String fn = "fn", ln = "ln", email = "email@email.com";
ApplicationContext clientContext = new AnnotationConfigApplicationContext(ThriftProxyClientConfiguration.class);
Crm.Iface clientCrm = clientContext.getBean(Crm.Iface.class);
Customer customer = clientCrm.createCustomer(fn, ln, email);
if (log.isDebugEnabled()) {
log.debug("the response from the server is " + ToStringBuilder.reflectionToString(customer));
}
Assert.assertNotNull(customer);
Assert.assertEquals(customer.getFirstName(), fn);
Assert.assertEquals(customer.getLastName(), ln);
Assert.assertEquals(customer.getEmail(), email);
Assert.assertTrue(customer.getId() > 0);
}
}
/**
* The server
*/
@Configuration
class ExporterConfiguration {
static public final String SERVLET = "/crm";
@Bean
public Crm.Iface crmServiceImpl() {
return new CrmService();
}
@Bean
public BeanNameUrlHandlerMapping beanNameUrlHandlerMapping() {
return new BeanNameUrlHandlerMapping();
}
@Bean(name = SERVLET)
public ThriftServiceExporter crm() {
ThriftServiceExporter exporter = new ThriftServiceExporter();
exporter.setService(crmServiceImpl());
exporter.setServiceInterface(Crm.Iface.class);
return exporter;
}
}
/**
* the client
*/
@Configuration
class ThriftProxyClientConfiguration {
@Bean
public ThriftProxyFactoryBean client() {
// demonstrates how to use the protocol over HTTP
THttpClient.Factory httpClientFactory = new THttpClient.Factory("http://localhost:8080" + ExporterConfiguration.SERVLET);
TTransport tTransport = httpClientFactory.getTransport(null);
ThriftProxyFactoryBean proxy = new ThriftProxyFactoryBean();
proxy.setTransport(tTransport);
proxy.setServiceInterface(Crm.class);
return proxy;
}
}
class CrmService implements Crm.Iface {
private List<String> firstNames = Arrays.asList("Josh", "Oliver", "Costin", "Juergen", "Rod", "Mark", "Dave", "Arjen", "Keith", "Adam", "Mike", "Mario");
private List<String> lastNames = Arrays.asList("Lee", "Loo", "Wi", "Li", "Humble", "Duong", "Kuo");
private final Random lnRandom = new Random();
private final Random fnRandom = new Random();
private String lastName() {
int i = lnRandom.nextInt(lastNames.size());
return lastNames.get(i);
}
private String firstName() {
int i = fnRandom.nextInt(firstNames.size());
return firstNames.get(i);
}
public Customer createCustomer(String fn, String ln, String email, int id) {
return new Customer(fn, ln, email, id);
}
@Override
public Customer createCustomer(String fn, String ln, String email) throws TException {
return createCustomer(fn, ln, email, (int) (Math.random() * 2002));
}
public Customer getCustomerById(int customerId) {
String fn = firstName();
String ln = lastName();
return new Customer(fn, ln, fn + "@email.com", customerId);
}
}