-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathKeyValueConsulClientTest.java
More file actions
135 lines (108 loc) · 4.45 KB
/
Copy pathKeyValueConsulClientTest.java
File metadata and controls
135 lines (108 loc) · 4.45 KB
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
package com.ecwid.consul.v1.kv;
import com.ecwid.consul.ConsulTestConstants;
import com.ecwid.consul.v1.kv.model.DeleteParams;
import com.ecwid.consul.v1.kv.model.GetValue;
import com.pszymczyk.consul.ConsulProcess;
import com.pszymczyk.consul.ConsulStarterBuilder;
import com.pszymczyk.consul.infrastructure.Ports;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Random;
class KeyValueConsulClientTest {
private static final Random rnd = new Random();
private ConsulProcess consul;
private int port = Ports.nextAvailable();
private KeyValueConsulClient consulClient = new KeyValueConsulClient("localhost", port);
@BeforeEach
void setUp() {
consul = ConsulStarterBuilder.consulStarter()
.withConsulVersion(ConsulTestConstants.CONSUL_VERSION)
.withHttpPort(port)
.build()
.start();
}
@AfterEach
void tearDown() {
consul.close();
}
@Test
void testSetKVBinaryValue() throws Exception {
final String testKey = "test_key";
final byte[] testValue = new byte[100];
rnd.nextBytes(testValue);
// Make sure there is no such key before test running
Assertions.assertNull(consulClient.getKVValue(testKey).getValue());
// Set the key
consulClient.setKVBinaryValue(testKey, testValue);
// Make sure test key exists
Assertions.assertArrayEquals(consulClient.getKVBinaryValue(testKey).getValue().getValue(), testValue);
}
@Test
void testDeleteKvValue() throws Exception {
final String testKey = "test_key";
final String testValue = "test_value";
// Make sure there is no such key before test running
Assertions.assertNull(consulClient.getKVValue(testKey).getValue());
// Set the key
consulClient.setKVValue(testKey, testValue);
// Make sure test key exists
Assertions.assertEquals(consulClient.getKVValue(testKey).getValue().getDecodedValue(), testValue);
// Delete key
consulClient.deleteKVValue(testKey);
// Make sure there is no such key before test running
Assertions.assertNull(consulClient.getKVValue(testKey).getValue());
}
@Test
void testDeleteKvValueCas() {
final String testKey = "test_key";
final String testValue = "test_value";
final String testModifiedValue = "test_value2";
// Make sure there is no such key before test running
Assertions.assertNull(consulClient.getKVValue(testKey).getValue());
// Set the key
consulClient.setKVValue(testKey, testValue);
// Make sure test key exists
GetValue getValue = consulClient.getKVValue(testKey).getValue();
Assertions.assertEquals(testValue, getValue.getDecodedValue());
// Modify the value
consulClient.setKVValue(testKey, testModifiedValue);
// Make sure it returns modified value
GetValue modifiedGetValue = consulClient.getKVValue(testKey).getValue();
Assertions.assertEquals(testModifiedValue, modifiedGetValue.getDecodedValue());
// Verify that consul changed the modified index
Assertions.assertNotEquals(getValue.getModifyIndex(), modifiedGetValue.getModifyIndex());
// Delete key with the old modify index
DeleteParams deleteOldIndex = new DeleteParams();
deleteOldIndex.setCas(getValue.getModifyIndex());
consulClient.deleteKVValue(testKey, deleteOldIndex);
// Make sure the key is still there
Assertions.assertEquals(testModifiedValue, consulClient.getKVValue(testKey).getValue().getDecodedValue());
// Delete the key with the new modify index
DeleteParams deleteNewIndex = new DeleteParams();
deleteNewIndex.setCas(modifiedGetValue.getModifyIndex());
consulClient.deleteKVValue(testKey, deleteNewIndex);
// Make sure the key is gone
Assertions.assertNull(consulClient.getKVValue(testKey).getValue());
}
@Test
void testDeleteKvValues() throws Exception {
final String testKeyPrefix = "test_key";
final String testValue = "test_value";
// Prepare test keys under testKeyPrefix prefix
for (int i = 0; i < 10; i++) {
final String testKey = testKeyPrefix + "/" + i;
// Make sure there is no such key before test running
Assertions.assertNull(consulClient.getKVValue(testKey).getValue());
// Set the key
consulClient.setKVValue(testKey, testValue);
// Make sure test key exists
Assertions.assertEquals(consulClient.getKVValue(testKey).getValue().getDecodedValue(), testValue);
}
// Delete all keys in single shot
consulClient.deleteKVValues(testKeyPrefix);
// Make sure all keys have been deleted
Assertions.assertNull(consulClient.getKVKeysOnly(testKeyPrefix).getValue());
}
}