/
ConfigOpTest.java
288 lines (231 loc) · 10.9 KB
/
ConfigOpTest.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* Copyright (c) 2012-2016 Boundless and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/edl-v10.html
*
* Contributors:
* Michael Fawcett (LMN Solutions) - initial implementation
*/
package org.locationtech.geogig.test.integration;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.locationtech.geogig.porcelain.ConfigOp;
import org.locationtech.geogig.porcelain.ConfigOp.ConfigAction;
import org.locationtech.geogig.porcelain.ConfigOp.ConfigScope;
import org.locationtech.geogig.storage.ConfigDatabase;
import org.locationtech.geogig.storage.ConfigException;
import org.locationtech.geogig.storage.ConfigException.StatusCode;
// TODO: Not sure if this belongs in porcelain or integration
public class ConfigOpTest extends RepositoryTestCase {
@Rule
public ExpectedException exception = ExpectedException.none();
@Before
public final void setUpInternal() {
}
@After
public final void tearDownInternal() {
}
private void test(ConfigOp.ConfigScope scope) {
final ConfigOp config = geogig.command(ConfigOp.class);
config.setScope(scope);
config.setAction(ConfigAction.CONFIG_SET).setName("section.string").setValue("1").call();
Map<String, String> result = config.setAction(ConfigAction.CONFIG_GET)
.setName("section.string").setValue(null).call().orElseGet(HashMap::new);
assertEquals("1", result.get("section.string"));
// Test overwriting a value that already exists
config.setAction(ConfigAction.CONFIG_SET).setName("section.string").setValue("2").call();
result = config.setAction(ConfigAction.CONFIG_GET).setName("section.string").setValue(null)
.call().orElseGet(HashMap::new);
assertEquals("2", result.get("section.string"));
// Test unsetting a value that exists
config.setAction(ConfigAction.CONFIG_UNSET).setName("section.string").setValue(null).call();
result = config.setAction(ConfigAction.CONFIG_GET).setName("section.string").setValue(null)
.call().orElseGet(HashMap::new);
assertNull(result.get("section.string"));
// Test unsetting a value that doesn't exist
config.setAction(ConfigAction.CONFIG_UNSET).setName("section.string").setValue(null).call();
result = config.setAction(ConfigAction.CONFIG_GET).setName("section.string").setValue(null)
.call().orElseGet(HashMap::new);
assertNull(result.get("section.string"));
// Test removing a section that exists
config.setAction(ConfigAction.CONFIG_SET).setName("section.string").setValue("1").call();
config.setAction(ConfigAction.CONFIG_SET).setName("section.string2").setValue("2").call();
config.setAction(ConfigAction.CONFIG_REMOVE_SECTION).setName("section").setValue(null)
.call();
result = config.setAction(ConfigAction.CONFIG_GET).setName("section.string").setValue(null)
.call().orElseGet(HashMap::new);
assertNull(result.get("section.string"));
result = config.setAction(ConfigAction.CONFIG_GET).setName("section.string2").setValue(null)
.call().orElseGet(HashMap::new);
assertNull(result.get("section.string2"));
// Try listing the config file
config.setAction(ConfigAction.CONFIG_SET).setName("section.string").setValue("1").call();
config.setAction(ConfigAction.CONFIG_SET).setName("section.string2").setValue("2").call();
result = config.setAction(ConfigAction.CONFIG_LIST).call().orElseGet(HashMap::new);
assertEquals("1", result.get("section.string"));
assertEquals("2", result.get("section.string2"));
}
@Test
public void testLocal() {
test(ConfigScope.LOCAL);
}
@Test
public void testGlobal() {
test(ConfigScope.GLOBAL);
}
@Test
public void testDefault() {
test(ConfigScope.DEFAULT);
}
@Test
public void testListDefaultWithNoLocalRepository() {
ConfigDatabase database = mock(ConfigDatabase.class);
when(database.getAll()).thenThrow(new ConfigException(StatusCode.INVALID_LOCATION));
ConfigOp config = new ConfigOp(database);
config.setScope(ConfigScope.DEFAULT).setAction(ConfigAction.CONFIG_LIST).setName(null)
.setValue(null).call();
}
@Test
public void testGetDefaultWithNoLocalRepository() {
ConfigDatabase database = mock(ConfigDatabase.class);
when(database.get(anyString())).thenThrow(new ConfigException(StatusCode.INVALID_LOCATION));
when(database.getGlobal(anyString())).thenReturn(Optional.of("value"));
ConfigOp config = new ConfigOp(database);
config.setScope(ConfigScope.DEFAULT).setAction(ConfigAction.CONFIG_GET)
.setName("section.key").setValue(null).call();
}
@Test
public void testListLocalWithNoLocalRepository() {
ConfigDatabase database = mock(ConfigDatabase.class);
when(database.getAll()).thenThrow(new ConfigException(StatusCode.INVALID_LOCATION));
ConfigOp config = new ConfigOp(database);
exception.expect(ConfigException.class);
config.setScope(ConfigScope.LOCAL).setAction(ConfigAction.CONFIG_LIST).setName(null)
.setValue(null).call();
}
@Test
public void testGetLocalWithNoLocalRepository() {
ConfigDatabase database = mock(ConfigDatabase.class);
when(database.get(anyString())).thenThrow(new ConfigException(StatusCode.INVALID_LOCATION));
ConfigOp config = new ConfigOp(database);
exception.expect(ConfigException.class);
config.setScope(ConfigScope.LOCAL).setAction(ConfigAction.CONFIG_GET).setName("section.key")
.setValue(null).call();
}
@Test
public void testNullNameValuePairForGet() {
final ConfigOp config = geogig.command(ConfigOp.class);
exception.expect(ConfigException.class);
config.setScope(ConfigScope.GLOBAL).setAction(ConfigAction.CONFIG_GET).setName(null)
.setValue(null).call();
}
@Test
public void testEmptyNameAndValueForGet() {
final ConfigOp config = geogig.command(ConfigOp.class);
exception.expect(ConfigException.class);
config.setScope(ConfigScope.GLOBAL).setAction(ConfigAction.CONFIG_GET).setName("")
.setValue("").call();
}
@Test
public void testEmptyNameAndValueForSet() {
final ConfigOp config = geogig.command(ConfigOp.class);
exception.expect(ConfigException.class);
config.setScope(ConfigScope.GLOBAL).setAction(ConfigAction.CONFIG_SET).setName("")
.setValue("").call();
}
@Test
public void testEmptyNameForUnset() {
final ConfigOp config = geogig.command(ConfigOp.class);
exception.expect(ConfigException.class);
config.setScope(ConfigScope.GLOBAL).setAction(ConfigAction.CONFIG_UNSET).setName("")
.setValue(null).call();
}
@Test
public void testEmptyNameForRemoveSection() {
final ConfigOp config = geogig.command(ConfigOp.class);
exception.expect(ConfigException.class);
config.setScope(ConfigScope.GLOBAL).setAction(ConfigAction.CONFIG_REMOVE_SECTION)
.setName("").call();
}
@Test
public void testNoNameForSet() {
final ConfigOp config = geogig.command(ConfigOp.class);
exception.expect(ConfigException.class);
config.setScope(ConfigScope.GLOBAL).setAction(ConfigAction.CONFIG_SET).setName(null)
.setValue(null).call();
}
@Test
public void testNoNameForUnset() {
final ConfigOp config = geogig.command(ConfigOp.class);
exception.expect(ConfigException.class);
config.setScope(ConfigScope.GLOBAL).setAction(ConfigAction.CONFIG_UNSET).setName(null)
.setValue(null).call();
}
@Test
public void testNoNameForRemoveSection() {
final ConfigOp config = geogig.command(ConfigOp.class);
exception.expect(ConfigException.class);
config.setScope(ConfigScope.GLOBAL).setAction(ConfigAction.CONFIG_REMOVE_SECTION)
.setName(null).setValue(null).call();
}
@Test
public void testRemovingMissingSection() {
final ConfigOp config = geogig.command(ConfigOp.class);
exception.expect(ConfigException.class);
config.setScope(ConfigScope.GLOBAL).setAction(ConfigAction.CONFIG_REMOVE_SECTION)
.setName("unusedsectionname").setValue(null).call();
}
@Test
public void testInvalidSectionKey() {
final ConfigOp config = geogig.command(ConfigOp.class);
Optional<Map<String, String>> result = config.setScope(ConfigScope.GLOBAL)
.setAction(ConfigAction.CONFIG_GET).setName("doesnt.exist").setValue(null).call();
assertFalse(result.isPresent());
}
@Test
public void testTooManyArguments() {
final ConfigOp config = geogig.command(ConfigOp.class);
exception.expect(ConfigException.class);
config.setScope(ConfigScope.GLOBAL).setAction(ConfigAction.CONFIG_GET).setName("too.many")
.setValue("arguments").call();
}
@Test
public void testEnum() {
ConfigAction.values();
assertEquals(ConfigAction.CONFIG_GET, ConfigAction.valueOf("CONFIG_GET"));
}
@Test
public void testNoAction() {
final ConfigOp config = geogig.command(ConfigOp.class);
exception.expect(ConfigException.class);
config.setAction(ConfigAction.CONFIG_NO_ACTION).setName("section.key").setValue(null)
.call();
}
@Test
public void testFallback() {
final ConfigOp config = geogig.command(ConfigOp.class);
// Set a value in global config, then try to get value from local even though
// we're not in a valid repository
config.setAction(ConfigAction.CONFIG_SET).setScope(ConfigScope.GLOBAL)
.setName("section.key").setValue("1").call();
Optional<Map<String, String>> value = config.setAction(ConfigAction.CONFIG_GET)
.setScope(ConfigScope.LOCAL).setName("section.key").setValue(null).call();
assertTrue(value.isPresent());
assertEquals("1", value.get().get("section.key"));
value = Optional.empty();
value = config.setAction(ConfigAction.CONFIG_GET).setScope(ConfigScope.LOCAL)
.setName("section.key").setValue("").call();
assertTrue(value.isPresent());
assertEquals("1", value.get().get("section.key"));
}
}