/
ConfigTest.java
167 lines (132 loc) · 5.81 KB
/
ConfigTest.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
/* Copyright (c) 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:
* Johnathan Garrett (Prominent Edge) - initial implementation
*/
package org.locationtech.geogig.web.api.commands;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.locationtech.geogig.web.api.JsonUtils.jsonEquals;
import static org.locationtech.geogig.web.api.JsonUtils.toJSON;
import static org.locationtech.geogig.web.api.JsonUtils.toJSONArray;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import javax.json.JsonArray;
import javax.json.JsonObject;
import org.junit.Test;
import org.locationtech.geogig.rest.repository.TestParams;
import org.locationtech.geogig.storage.ConfigDatabase;
import org.locationtech.geogig.web.api.AbstractWebAPICommand;
import org.locationtech.geogig.web.api.AbstractWebOpTest;
import org.locationtech.geogig.web.api.ParameterSet;
import org.locationtech.geogig.web.api.WebAPICommand;
import org.springframework.web.bind.annotation.RequestMethod;
public class ConfigTest extends AbstractWebOpTest {
@Override
protected String getRoute() {
return "config";
}
@Override
protected Class<? extends AbstractWebAPICommand> getCommandClass() {
return Config.class;
}
@Override
protected boolean requiresTransaction() {
return false;
}
@Test
public void testBuildParameters() {
ParameterSet options = TestParams.of("name", "config.key", "value", "myValue");
Config op = (Config) buildCommand(options);
assertEquals("config.key", op.name);
assertEquals("myValue", op.value);
}
@Test
public void testConfigList() throws Exception {
ConfigDatabase configDb = testContext.get().getRepository().configDatabase();
configDb.put("config.key1", "value1");
configDb.put("config.key2", "value2");
Map<String, String> currentConfig = configDb.getAll();
StringBuilder sb = new StringBuilder();
sb.append("[");
for (Entry<String, String> entry : currentConfig.entrySet()) {
sb.append("{\"name\":\"");
sb.append(entry.getKey());
sb.append("\", \"value\":");
try {
Double.parseDouble(entry.getValue());
sb.append(entry.getValue());
} catch (NumberFormatException e) {
sb.append("\"").append(entry.getValue()).append("\"");
}
sb.append("},");
}
sb.deleteCharAt(sb.length() - 1);
sb.append("]");
String expected = sb.toString();
ParameterSet options = TestParams.of();
WebAPICommand cmd = buildCommand(options);
cmd.run(testContext.get());
JsonObject response = getJSONResponse().getJsonObject("response");
JsonArray config = response.getJsonArray("config");
assertTrue(jsonEquals(toJSONArray(expected), config, false));
}
@Test
public void testConfigGet() throws Exception {
ConfigDatabase configDb = testContext.get().getRepository().configDatabase();
configDb.put("config.key1", "value1");
ParameterSet options = TestParams.of("name", "config.key1");
WebAPICommand cmd = buildCommand(options);
cmd.run(testContext.get());
JsonObject response = getJSONResponse().getJsonObject("response");
assertTrue(jsonEquals(toJSON("{\"value\":\"value1\"}"), response, false));
}
@Test
public void testConfigGetNonexistent() throws Exception {
ConfigDatabase configDb = testContext.get().getRepository().configDatabase();
configDb.put("config.key1", "value1");
ParameterSet options = TestParams.of("name", "config.nonexistent");
WebAPICommand cmd = buildCommand(options);
cmd.run(testContext.get());
JsonObject response = getJSONResponse().getJsonObject("response");
assertTrue(jsonEquals(toJSON("{\"success\":true}"), response, true));
}
@Test
public void testConfigSet() throws Exception {
ConfigDatabase configDb = testContext.get().getRepository().configDatabase();
assertFalse(configDb.get("config.key1").isPresent());
ParameterSet options = TestParams.of("name", "config.key1", "value", "myTestValue");
WebAPICommand cmd = buildCommand(options);
testContext.setRequestMethod(RequestMethod.POST);
cmd.run(testContext.get());
Optional<String> value = configDb.get("config.key1");
assertTrue(value.isPresent());
assertEquals("myTestValue", value.get());
JsonObject response = getJSONResponse().getJsonObject("response");
assertTrue(jsonEquals(toJSON("{\"success\":true}"), response, true));
}
@Test
public void testConfigSetNoValue() throws Exception {
ParameterSet options = TestParams.of("name", "config.key1");
WebAPICommand cmd = buildCommand(options);
testContext.setRequestMethod(RequestMethod.POST);
ex.expect(IllegalArgumentException.class);
ex.expectMessage("You must specify the value when setting a config key.");
cmd.run(testContext.get());
}
@Test
public void testConfigSetNoName() throws Exception {
ParameterSet options = TestParams.of("value", "myTestValue");
WebAPICommand cmd = buildCommand(options);
testContext.setRequestMethod(RequestMethod.POST);
ex.expect(IllegalArgumentException.class);
ex.expectMessage("You must specify the key when setting a config key.");
cmd.run(testContext.get());
}
}