Skip to content

Commit 2728e27

Browse files
bsboddensazzad16gkorland
authored
Improves Test Coverage and refactors deprecations out of the tests (#28)
* test: add test for JReJSON no-args constructor * test: refactor deprecated method calls * refactor: add @SupressWarnings("unused") to test object w//o getters/setters * test: set command without a path defaults to root path * test: fix delException test * test: add assertions to usage example test * test: refactor deprecated method calls in static client test * refactor: add @SupressWarnings(unused) to test object w/o getters/setters * refactor: use ROOT_PATH over deprecated RootPath method * refactor: organize imports * refactor: inline calls to the API in assertions Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> * style: remove extra indentation Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> * style: remove extra spaces Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Co-authored-by: Guy Korland <gkorland@gmail.com>
1 parent b78c99e commit 2728e27

File tree

4 files changed

+103
-56
lines changed

4 files changed

+103
-56
lines changed

src/main/java/com/redislabs/modules/rejson/JReJSON.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -81,27 +81,27 @@ public byte[] getRaw() {
8181
}
8282

8383
private Pool<Jedis> client;
84-
84+
8585
/**
8686
* Creates a client to the local machine
8787
*/
8888
public JReJSON() {
8989
this("localhost", 6379);
9090
}
91-
91+
9292
/**
9393
* Creates a client to the specific host/post
94-
*
94+
*
9595
* @param host Redis host
9696
* @param port Redis port
9797
*/
9898
public JReJSON(String host, int port) {
9999
this(new JedisPool(host, port));
100100
}
101-
101+
102102
/**
103103
* Creates a client using provided Jedis pool
104-
*
104+
*
105105
* @param jedis bring your own Jedis pool
106106
*/
107107
public JReJSON(Pool<Jedis> jedis) {
@@ -135,12 +135,12 @@ private static void assertReplyOK(final String str) {
135135
private static Path getSingleOptionalPath(Path... path) {
136136
// check for 0, 1 or more paths
137137
if (1 > path.length) { // default to root
138-
return Path.RootPath();
138+
return Path.ROOT_PATH;
139139
}
140140
if (1 == path.length) { // take 1
141141
return path[0];
142142
}
143-
143+
144144
// throw out the baby with the water
145145
throw new RuntimeException("Only a single optional path is allowed");
146146
}
@@ -154,7 +154,7 @@ public Long del(String key) {
154154
return del(key, Path.ROOT_PATH);
155155
}
156156

157-
157+
158158
/**
159159
* Deletes a path
160160
* @param key the key name
@@ -171,7 +171,7 @@ public Long del(String key, Path path) {
171171
return conn.getClient().getIntegerReply();
172172
}
173173
}
174-
174+
175175
/**
176176
* Gets an object at the root path
177177
* @param key the key name
@@ -180,7 +180,7 @@ public Long del(String key, Path path) {
180180
public <T> T get(String key) {
181181
return get(key, Path.ROOT_PATH);
182182
}
183-
183+
184184
/**
185185
* Gets an object
186186
* @param key the key name
@@ -192,11 +192,11 @@ public <T> T get(String key) {
192192
public <T> T get(String key, Path... paths) {
193193
return (T)this.get(key, Object.class, paths);
194194
}
195-
195+
196196
/**
197197
* Gets an object
198198
* @param key the key name
199-
* @param clazz
199+
* @param clazz
200200
* @param paths optional one ore more paths in the object
201201
* @return the requested object
202202
*/
@@ -226,16 +226,16 @@ public <T> T get(String key, Class<T> clazz, Path... paths) {
226226
public void set(String key, Object object, ExistenceModifier flag) {
227227
set(key, object, flag, Path.ROOT_PATH);
228228
}
229-
229+
230230
/**
231231
* Sets an object in the root path
232232
* @param key the key name
233233
* @param object the Java object to store
234234
*/
235235
public void set(String key, Object object) {
236236
set(key, object, ExistenceModifier.DEFAULT, Path.ROOT_PATH);
237-
}
238-
237+
}
238+
239239
/**
240240
* Sets an object without caring about target path existing
241241
* @param key the key name
@@ -245,7 +245,7 @@ public void set(String key, Object object) {
245245
public void set(String key, Object object, Path path) {
246246
set(key, object, ExistenceModifier.DEFAULT, path);
247247
}
248-
248+
249249
/**
250250
* Sets an object
251251
* @param key the key name
@@ -272,7 +272,7 @@ public void set(String key, Object object, ExistenceModifier flag, Path path) {
272272
}
273273
assertReplyOK(status);
274274
}
275-
275+
276276
/**
277277
* Gets the class of an object at the root path
278278
* @param key the key name
@@ -281,7 +281,7 @@ public void set(String key, Object object, ExistenceModifier flag, Path path) {
281281
public Class<?> type(String key) {
282282
return type(key, Path.ROOT_PATH);
283283
}
284-
284+
285285
/**
286286
* Gets the class of an object
287287
* @param key the key name
@@ -323,15 +323,15 @@ public Class<?> type(String key, Path path) {
323323
throw new java.lang.RuntimeException(rep);
324324
}
325325
}
326-
326+
327327

328328
/**
329329
* Deletes a path
330330
* @param conn the Jedis connection
331331
* @param key the key name
332332
* @param path optional single path in the object, defaults to root
333333
* @return the number of paths deleted (0 or 1)
334-
* @deprecated use {@link #del(String, Path)} instead
334+
* @deprecated use {@link #del(String, Path)} instead
335335
*/
336336
@Deprecated
337337
public static Long del(Jedis conn, String key, Path... path) {
@@ -417,7 +417,7 @@ public static void set(Jedis conn, String key, Object object, ExistenceModifier
417417
public static void set(Jedis conn, String key, Object object, Path... path) {
418418
set(conn,key, object, ExistenceModifier.DEFAULT, path);
419419
}
420-
420+
421421
/**
422422
* Gets the class of an object
423423
* @param conn the Jedis connection
@@ -460,7 +460,7 @@ public static Class<?> type(Jedis conn, String key, Path... path) {
460460
throw new java.lang.RuntimeException(rep);
461461
}
462462
}
463-
463+
464464
private Jedis getConnection() {
465465
return this.client.getResource();
466466
}

src/test/java/com/redislabs/modules/rejson/ClientTest.java

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,28 @@
2828

2929
package com.redislabs.modules.rejson;
3030

31-
import com.google.gson.Gson;
31+
import static junit.framework.TestCase.assertEquals;
32+
import static junit.framework.TestCase.assertFalse;
33+
import static junit.framework.TestCase.assertNull;
34+
import static junit.framework.TestCase.assertSame;
35+
import static junit.framework.TestCase.assertTrue;
36+
import static junit.framework.TestCase.fail;
37+
import static org.junit.Assert.assertThrows;
38+
39+
import java.util.List;
3240

3341
import org.junit.Before;
3442
import org.junit.Test;
35-
import redis.clients.jedis.Jedis;
3643

37-
import static junit.framework.TestCase.*;
44+
import com.google.gson.Gson;
3845

39-
import java.util.List;
46+
import redis.clients.jedis.Jedis;
47+
import redis.clients.jedis.exceptions.JedisDataException;
4048

4149
public class ClientTest {
4250

4351
/* A simple class that represents an object in real life */
52+
@SuppressWarnings("unused")
4453
private static class IRLObject {
4554
public String str;
4655
public boolean bTrue;
@@ -51,12 +60,13 @@ public IRLObject() {
5160
}
5261
}
5362

63+
@SuppressWarnings("unused")
5464
private static class FooBarObject {
5565
public String foo;
5666
public boolean fooB;
5767
public int fooI;
5868
public float fooF;
59-
public String[] fooArr;
69+
public String[] fooArr;
6070

6171
public FooBarObject() {
6272
this.foo = "bar";
@@ -70,18 +80,28 @@ public FooBarObject() {
7080
private final Gson g = new Gson();
7181
private final JReJSON client = new JReJSON("localhost",6379);
7282
private final Jedis jedis = new Jedis("localhost",6379);
73-
83+
7484
@Before
7585
public void cleanup() {
7686
jedis.flushDB();
7787
}
78-
88+
89+
@Test
90+
public void noArgsConstructorReturnsClientToLocalMachine() {
91+
final JReJSON defaultClient = new JReJSON();
92+
final JReJSON explicitLocalClient = new JReJSON("localhost", 6379);
93+
94+
// naive set with a path
95+
defaultClient.set("null", null, Path.ROOT_PATH);
96+
assertNull(explicitLocalClient.get("null", String.class, Path.ROOT_PATH));
97+
}
98+
7999
@Test
80100
public void basicSetGetShouldSucceed() throws Exception {
81101

82102
// naive set with a path
83103
client.set("null", null, Path.ROOT_PATH);
84-
assertNull(client.get("null", Path.ROOT_PATH));
104+
assertNull(client.get("null", String.class, Path.ROOT_PATH));
85105

86106
// real scalar value and no path
87107
client.set( "str", "strong");
@@ -96,23 +116,30 @@ public void basicSetGetShouldSucceed() throws Exception {
96116
// check an update
97117
Path p = new Path(".str");
98118
client.set( "obj", "strung", p);
99-
assertEquals("strung", client.get( "obj", p));
119+
assertEquals("strung", client.get( "obj", String.class, p));
100120
}
101121

102122
@Test
103123
public void setExistingPathOnlyIfExistsShouldSucceed() throws Exception {
104124
client.set( "obj", new IRLObject());
105125
Path p = new Path(".str");
106126
client.set( "obj", "strangle", JReJSON.ExistenceModifier.MUST_EXIST, p);
107-
assertEquals("strangle", client.get( "obj", p));
127+
assertEquals("strangle", client.get( "obj", String.class, p));
108128
}
109129

110130
@Test
111131
public void setNonExistingOnlyIfNotExistsShouldSucceed() throws Exception {
112132
client.set( "obj", new IRLObject());
113133
Path p = new Path(".none");
114134
client.set( "obj", "strangle", JReJSON.ExistenceModifier.NOT_EXISTS, p);
115-
assertEquals("strangle", client.get( "obj", p));
135+
assertEquals("strangle", client.get( "obj", String.class, p));
136+
}
137+
138+
@Test
139+
public void setWithoutAPathDefaultsToRootPath() throws Exception {
140+
client.set( "obj1", new IRLObject());
141+
client.set( "obj1", "strangle", JReJSON.ExistenceModifier.MUST_EXIST);
142+
assertEquals("strangle", client.get( "obj1", String.class, Path.ROOT_PATH));
116143
}
117144

118145
@Test(expected = Exception.class)
@@ -141,14 +168,14 @@ public void getMultiplePathsShouldSucceed() throws Exception {
141168
IRLObject obj = new IRLObject();
142169
client.set( "obj", obj);
143170
Object expected = g.fromJson(g.toJson(obj), Object.class);
144-
assertTrue(expected.equals(client.get( "obj", new Path("bTrue"), new Path("str"))));
171+
assertTrue(expected.equals(client.get( "obj", Object.class, new Path("bTrue"), new Path("str"))));
145172

146173
}
147174

148175
@Test(expected = Exception.class)
149176
public void getException() throws Exception {
150177
client.set( "test", "foo", Path.ROOT_PATH);
151-
client.get( "test", new Path(".bar"));
178+
client.get( "test", String.class, new Path(".bar"));
152179
}
153180

154181
@Test
@@ -163,9 +190,14 @@ public void delValidShouldSucceed() throws Exception {
163190
assertFalse(jedis.exists("obj"));
164191
}
165192

193+
@Test
166194
public void delException() throws Exception {
167-
client.set( "foobar", new FooBarObject(), Path.ROOT_PATH);
168-
assertEquals(0L, client.del( "foobar", new Path(".foo[1]")).longValue());
195+
Exception ex = assertThrows(JedisDataException.class, () -> {
196+
client.set( "foobar", new FooBarObject(), Path.ROOT_PATH);
197+
client.del( "foobar", new Path(".foo[1]")).longValue();
198+
});
199+
200+
assertTrue(ex.getMessage().contains("ERR invalid index '[1]' at level 1 in path"));
169201
}
170202

171203
@Test
@@ -178,7 +210,7 @@ public void typeChecksShouldSucceed() throws Exception {
178210
assertSame(float.class, client.type( "foobar", new Path(".fooF")));
179211
assertSame(List.class, client.type( "foobar", new Path(".fooArr")));
180212
assertSame(boolean.class, client.type( "foobar", new Path(".fooB")));
181-
213+
182214
try {
183215
client.type( "foobar", new Path(".fooErr"));
184216
fail();
@@ -195,5 +227,6 @@ public void typeException() throws Exception {
195227
public void type1Exception() throws Exception {
196228
client.set( "foobar", new FooBarObject(), Path.ROOT_PATH);
197229
client.type( "foobar", new Path(".foo[1]"));
198-
}
199-
}
230+
}
231+
232+
}

0 commit comments

Comments
 (0)