-
Notifications
You must be signed in to change notification settings - Fork 612
/
BaseJsonTest.java
171 lines (136 loc) · 6.36 KB
/
BaseJsonTest.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
package org.infinispan.it.compatibility;
import static org.infinispan.client.hotrod.test.HotRodClientTestingUtil.killServers;
import static org.infinispan.client.hotrod.test.HotRodClientTestingUtil.startHotRodServer;
import static org.infinispan.rest.JSONConstants.TYPE;
import static org.infinispan.server.core.test.ServerTestingUtil.findFreePort;
import static org.infinispan.test.TestingUtil.killCacheManagers;
import static org.testng.Assert.assertEquals;
import static org.testng.AssertJUnit.assertTrue;
import java.io.IOException;
import java.util.List;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ObjectNode;
import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.Search;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.query.dsl.Query;
import org.infinispan.rest.RestServer;
import org.infinispan.rest.configuration.RestServerConfigurationBuilder;
import org.infinispan.server.hotrod.HotRodServer;
import org.infinispan.test.AbstractInfinispanTest;
import org.infinispan.test.fwk.TestCacheManagerFactory;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
/**
* Base class for Json reading/writing/querying across multiple endpoints.
*
* @since 9.2
*/
@Test(groups = "functional")
public abstract class BaseJsonTest extends AbstractInfinispanTest {
RestServer restServer;
HttpClient restClient;
private EmbeddedCacheManager cacheManager;
private RemoteCacheManager remoteCacheManager;
private RemoteCache<String, CryptoCurrency> remoteCache;
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final String CACHE_NAME = "indexed";
HotRodServer hotRodServer;
private String restEndpoint;
abstract ConfigurationBuilder getIndexCacheConfiguration();
abstract RemoteCacheManager createRemoteCacheManager() throws Exception;
@BeforeClass
protected void setup() throws Exception {
cacheManager = TestCacheManagerFactory.createServerModeCacheManager();
cacheManager.defineConfiguration(CACHE_NAME, getIndexCacheConfiguration().build());
RestServerConfigurationBuilder builder = new RestServerConfigurationBuilder();
int restPort = findFreePort();
builder.port(restPort);
restServer = new RestServer();
restServer.start(builder.build(), cacheManager);
restClient = new HttpClient();
hotRodServer = startHotRodServer(cacheManager);
remoteCacheManager = createRemoteCacheManager();
remoteCache = remoteCacheManager.getCache(CACHE_NAME);
restEndpoint = String.format("http://localhost:%s/rest/%s", restServer.getPort(), CACHE_NAME);
}
abstract String getEntityName();
private void writeCurrencyViaJson(String key, String description, int rank) throws IOException {
EntityEnclosingMethod put = new PutMethod(restEndpoint + "/" + key);
ObjectNode currency = MAPPER.createObjectNode();
currency.put(TYPE, getEntityName());
currency.put("description", description);
currency.put("rank", rank);
put.setRequestEntity(new StringRequestEntity(currency.toString(), "application/json", "UTF-8"));
restClient.executeMethod(put);
System.out.println(put.getResponseBodyAsString());
assertEquals(put.getStatusCode(), HttpStatus.SC_OK);
}
private CryptoCurrency readCurrencyViaJson(String key) throws IOException {
HttpMethod get = new GetMethod(restEndpoint + "/" + key);
get.setRequestHeader("Accept", "application/json");
restClient.executeMethod(get);
String json = get.getResponseBodyAsString();
JsonNode jsonNode = new ObjectMapper().readTree(json);
JsonNode description = jsonNode.get("description");
JsonNode rank = jsonNode.get("rank");
return new CryptoCurrency(description.asText(), rank.getIntValue());
}
@Test
public void testRestOnly() throws Exception {
writeCurrencyViaJson("DASH", "Dash", 7);
writeCurrencyViaJson("IOTA", "Iota", 8);
writeCurrencyViaJson("XMR", "Monero", 9);
CryptoCurrency xmr = readCurrencyViaJson("XMR");
assertEquals(xmr.getRank(), Integer.valueOf(9));
assertEquals(xmr.getDescription(), "Monero");
}
@Test
public void testHotRodInteroperability() throws Exception {
remoteCache.clear();
// Put object via Hot Rod
remoteCache.put("BTC", new CryptoCurrency("Bitcoin", 1));
remoteCache.put("ETH", new CryptoCurrency("Ethereum", 2));
remoteCache.put("XRP", new CryptoCurrency("Ripple", 3));
remoteCache.put("CAT", new CryptoCurrency("Catcoin", 618));
assertEquals(remoteCache.get("CAT").getDescription(), "Catcoin");
assertEquals(remoteCache.size(), 4);
Query query = Search.getQueryFactory(remoteCache).create("FROM " + getEntityName() + " c where c.rank < 10");
List<CryptoCurrency> highRankCoins = query.list();
assertEquals(highRankCoins.size(), 3);
// Read as Json
CryptoCurrency btc = readCurrencyViaJson("BTC");
assertEquals("Bitcoin", btc.getDescription());
assertEquals(Integer.valueOf(1), btc.getRank());
// Write as Json
writeCurrencyViaJson("LTC", "Litecoin", 4);
// Assert inserted entity is searchable
query = Search.getQueryFactory(remoteCache).create("FROM " + getEntityName() + " c where c.description = 'Litecoin'");
CryptoCurrency litecoin = (CryptoCurrency) query.list().iterator().next();
assertEquals(litecoin.getDescription(), "Litecoin");
assertTrue(litecoin.getRank() == 4);
}
@AfterClass
protected void teardown() {
remoteCacheManager.stop();
if (restServer != null) {
try {
restServer.stop();
} catch (Exception ignored) {
}
}
killCacheManagers(cacheManager);
killServers(hotRodServer);
}
}