Skip to content

Commit 4b80003

Browse files
authored
Merge pull request #23 from vikadata/fix-uri-variables
Fix uri variables
2 parents e26f90e + 02f4066 commit 4b80003

File tree

9 files changed

+182
-69
lines changed

9 files changed

+182
-69
lines changed

client/src/main/java/cn/vika/client/api/Constants.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,15 @@
2323
package cn.vika.client.api;
2424

2525
/**
26-
*
2726
* @author Shawn Deng
28-
* @date 2021-02-05 18:22:10
2927
*/
3028
public interface Constants {
3129

3230
String PAGE_NUM = "pageNum";
3331

3432
String PAGE_SIZE = "pageSize";
3533

36-
String SORT = "sort";
34+
String SORT = "sort[]";
3735

3836
String VIEW_ID = "viewId";
3937

client/src/main/java/cn/vika/client/api/model/ApiQueryParam.java

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222

2323
package cn.vika.client.api.model;
2424

25-
import java.util.Collections;
25+
import java.util.ArrayList;
2626
import java.util.HashMap;
27-
import java.util.LinkedHashMap;
2827
import java.util.List;
2928
import java.util.Map;
3029

3130
import cn.vika.client.api.util.UrlEncoder;
31+
import com.fasterxml.jackson.core.JsonProcessingException;
32+
import com.fasterxml.jackson.databind.ObjectMapper;
3233

3334
import static cn.vika.client.api.Constants.CELL_FORMAT;
3435
import static cn.vika.client.api.Constants.FIELDS;
@@ -42,35 +43,31 @@
4243
import static cn.vika.client.api.Constants.VIEW_ID;
4344

4445
/**
45-
*
4646
* @author Shawn Deng
47-
* @date 2021-02-05 20:20:49
4847
*/
4948
public class ApiQueryParam extends HashMap<String, List<String>> {
5049

51-
private Map<String, Order> orderByMap;
52-
53-
public static ApiQueryParam EMPTY = new ApiQueryParam();
50+
public static ApiQueryParam newInstance() {
51+
return new ApiQueryParam();
52+
}
5453

5554
public ApiQueryParam() {
5655
super(16);
57-
orderByMap = new LinkedHashMap<>(16);
5856
}
5957

6058
public ApiQueryParam(HashMap<String, List<String>> map) {
6159
super(map);
62-
orderByMap = new LinkedHashMap<>(16);
6360
}
6461

6562
public ApiQueryParam(int page, int pageSize) {
6663
super(16);
67-
orderByMap = new LinkedHashMap<>(16);
6864
withParam(PAGE_NUM, Integer.toString(page));
6965
withParam(PAGE_SIZE, Integer.toString(pageSize));
7066
}
7167

7268
public ApiQueryParam withSort(String fieldName, Order order) {
73-
orderByMap.put(fieldName, order);
69+
List<String> values = computeIfAbsent(SORT, k -> new ArrayList<>(1));
70+
values.add(formatSort(fieldName, order));
7471
return this;
7572
}
7673

@@ -103,21 +100,41 @@ public ApiQueryParam withFieldKey(FieldKey fieldKey) {
103100
}
104101

105102
public ApiQueryParam withParam(String key, String value) {
106-
if (value == null) {
107-
return this;
103+
if (value != null && !value.isEmpty()) {
104+
add(key, value);
108105
}
109-
put(key, Collections.singletonList(value));
110106
return this;
111107
}
112108

113109
public ApiQueryParam withParam(String key, List<String> value) {
114-
if (value == null) {
115-
return this;
110+
if (value != null && !value.isEmpty()) {
111+
addAll(key, value);
116112
}
117-
put(key, value);
118113
return this;
119114
}
120115

116+
private void addAll(String key, List<String> values) {
117+
List<String> currentValues = this.computeIfAbsent(key, k -> new ArrayList<>(1));
118+
currentValues.addAll(values);
119+
}
120+
121+
private void add(String key, String value) {
122+
List<String> values = this.computeIfAbsent(key, k -> new ArrayList<>(1));
123+
values.add(value);
124+
}
125+
126+
private String formatSort(String fieldName, Order order) {
127+
Map<String, String> sortMap = new HashMap<>(2);
128+
sortMap.put("field", fieldName);
129+
sortMap.put("order", order.name().toLowerCase());
130+
try {
131+
return new ObjectMapper().writeValueAsString(sortMap);
132+
}
133+
catch (JsonProcessingException e) {
134+
throw new IllegalArgumentException("can't format sort parameter", e);
135+
}
136+
}
137+
121138
public Map<String, String> toMap() {
122139
Map<String, String> queryMap = new HashMap<>();
123140
for (Entry<String, List<String>> entry : this.entrySet()) {
@@ -130,14 +147,6 @@ else if (entry.getValue().size() > 1) {
130147
}
131148
}
132149
}
133-
if (!orderByMap.isEmpty()) {
134-
int i = 0;
135-
for (Entry<String, Order> entry : orderByMap.entrySet()) {
136-
queryMap.put(SORT + "." + i + ".field", entry.getKey());
137-
queryMap.put(SORT + "." + i + ".order", entry.getValue().name().toLowerCase());
138-
i++;
139-
}
140-
}
141150
return queryMap;
142151
}
143152
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cn.vika.client.api;
2+
3+
import cn.vika.client.api.http.ApiCredential;
4+
5+
import static cn.vika.client.api.ConstantKey.TEST_API_KEY;
6+
import static cn.vika.client.api.ConstantKey.TEST_DATASHEET_ID;
7+
import static cn.vika.client.api.ConstantKey.TEST_HOST_URL;
8+
9+
/**
10+
* @author Shawn Deng
11+
*/
12+
public abstract class AbstractBaseTest {
13+
14+
protected final String DATASHEET_ID = TEST_DATASHEET_ID.get();
15+
16+
private static final String HOST_URL = TEST_HOST_URL.get();
17+
18+
private static final String API_KEY = TEST_API_KEY.get();
19+
20+
protected VikaApiClient vikaApiClient = new VikaApiClient(HOST_URL, new ApiCredential(API_KEY));
21+
}

client/src/test/java/cn/vika/client/api/RecordOperationTest.java

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@
5959
import static org.assertj.core.api.Assertions.assertThat;
6060

6161
/**
62-
*
6362
* @author Shawn Deng
64-
* @date 2021-02-24 16:36:26
6563
*/
6664
@TestMethodOrder(OrderAnnotation.class)
6765
public class RecordOperationTest {
@@ -92,7 +90,7 @@ void testCreateRecordByNameFromFile() throws IOException, ApiException {
9290
assertThat(inputStream).isNotNull();
9391
List<RecordMap> recordMaps = JacksonJsonUtil.unmarshalInputStreamToList(RecordMap.class, inputStream);
9492
CreateRecordRequest recordRequest = new CreateRecordRequest()
95-
.withRecords(recordMaps);
93+
.withRecords(recordMaps);
9694
List<Record> newRecords = vikaApiClient.getRecordApi().addRecords(DATASHEET_ID, recordRequest);
9795
assertThat(newRecords).isNotNull();
9896
assertThat(newRecords).isNotEmpty();
@@ -119,8 +117,8 @@ void testCreateRecordByIdFromFile() throws IOException, ApiException {
119117
assertThat(inputStream).isNotNull();
120118
List<RecordMap> recordMaps = JacksonJsonUtil.unmarshalInputStreamToList(RecordMap.class, inputStream);
121119
CreateRecordRequest recordRequest = new CreateRecordRequest()
122-
.withRecords(recordMaps)
123-
.withFieldKey(FieldKey.ID);
120+
.withRecords(recordMaps)
121+
.withFieldKey(FieldKey.ID);
124122
List<Record> newRecords = vikaApiClient.getRecordApi().addRecords(DATASHEET_ID, recordRequest);
125123
assertThat(newRecords).isNotNull();
126124
assertThat(newRecords).isNotEmpty();
@@ -143,16 +141,16 @@ void testCreateRecordByIdFromFile() throws IOException, ApiException {
143141
@Order(20)
144142
void testCreateRecordFromJson() throws IOException, ApiException {
145143
ObjectNode fieldMap = JsonNodeFactory.instance.objectNode()
146-
.put("ShortText", "Json manual builder")
147-
.put("LongText", "Json manual builder")
148-
.set("Options", JsonNodeFactory.instance.arrayNode().add("A"));
144+
.put("ShortText", "Json manual builder")
145+
.put("LongText", "Json manual builder")
146+
.set("Options", JsonNodeFactory.instance.arrayNode().add("A"));
149147
fieldMap.set("Options", JsonNodeFactory.instance.arrayNode().add("A"));
150148
fieldMap.set("MultiSelect", JsonNodeFactory.instance.arrayNode().add("GG"));
151149
ObjectNode fields = JsonNodeFactory.instance.objectNode().set("fields", fieldMap);
152150
ArrayNode arrayNode = JsonNodeFactory.instance.arrayNode().add(fields);
153151
List<RecordMap> recordMaps = JacksonJsonUtil.unmarshalJsonNodeToList(RecordMap.class, arrayNode);
154152
CreateRecordRequest recordRequest = new CreateRecordRequest()
155-
.withRecords(recordMaps);
153+
.withRecords(recordMaps);
156154
List<Record> newRecords = vikaApiClient.getRecordApi().addRecords(DATASHEET_ID, recordRequest);
157155
assertThat(newRecords).isNotNull();
158156
assertThat(newRecords).isNotEmpty();
@@ -182,7 +180,7 @@ void testCreateRecordFromBean() throws ApiException {
182180

183181
List<RecordMap> recordMaps = Collections.singletonList(new RecordMap().withFields(JacksonConverter.toMap(fieldDTO)));
184182
CreateRecordRequest recordRequest = new CreateRecordRequest()
185-
.withRecords(recordMaps);
183+
.withRecords(recordMaps);
186184
List<Record> newRecords = vikaApiClient.getRecordApi().addRecords(DATASHEET_ID, recordRequest);
187185
assertThat(newRecords).isNotNull();
188186
assertThat(newRecords).isNotEmpty();
@@ -209,7 +207,7 @@ void testUpdateRecord() throws IOException, ApiException {
209207
assertThat(inputStream).isNotNull();
210208
List<RecordMap> recordMaps = JacksonJsonUtil.unmarshalInputStreamToList(RecordMap.class, inputStream);
211209
CreateRecordRequest createRecordRequest = new CreateRecordRequest()
212-
.withRecords(recordMaps);
210+
.withRecords(recordMaps);
213211
List<Record> newRecords = vikaApiClient.getRecordApi().addRecords(DATASHEET_ID, createRecordRequest);
214212
assertThat(newRecords).isNotNull();
215213
assertThat(newRecords).isNotEmpty();
@@ -221,14 +219,14 @@ void testUpdateRecord() throws IOException, ApiException {
221219
String recordId = recordResult.getRecordId();
222220

223221
UpdateRecord record = new UpdateRecord()
224-
.withRecordId(recordId)
225-
.withField("ShortText", "Ps: Test Update, content is 'This is from unit Test update record' before")
226-
// select can be set null or empty array if you want to clear field value
227-
.withField("Options", Collections.emptyList())
228-
.withField("MultiSelect", Arrays.asList("LL", "NN"));
222+
.withRecordId(recordId)
223+
.withField("ShortText", "Ps: Test Update, content is 'This is from unit Test update record' before")
224+
// select can be set null or empty array if you want to clear field value
225+
.withField("Options", Collections.emptyList())
226+
.withField("MultiSelect", Arrays.asList("LL", "NN"));
229227

230228
UpdateRecordRequest updateRecordRequest = new UpdateRecordRequest()
231-
.withRecords(Collections.singletonList(record));
229+
.withRecords(Collections.singletonList(record));
232230

233231
List<Record> updateRecords = vikaApiClient.getRecordApi().updateRecords(DATASHEET_ID, updateRecordRequest);
234232
assertThat(updateRecords).isNotNull();
@@ -252,8 +250,8 @@ void testUpdateRecordByFieldId() throws IOException, ApiException {
252250
assertThat(inputStream).isNotNull();
253251
List<RecordMap> recordMaps = JacksonJsonUtil.unmarshalInputStreamToList(RecordMap.class, inputStream);
254252
CreateRecordRequest createRecordRequest = new CreateRecordRequest()
255-
.withRecords(recordMaps)
256-
.withFieldKey(FieldKey.ID);
253+
.withRecords(recordMaps)
254+
.withFieldKey(FieldKey.ID);
257255
List<Record> newRecords = vikaApiClient.getRecordApi().addRecords(DATASHEET_ID, createRecordRequest);
258256
assertThat(newRecords).isNotNull();
259257
assertThat(newRecords).isNotEmpty();
@@ -265,15 +263,15 @@ void testUpdateRecordByFieldId() throws IOException, ApiException {
265263
String recordId = recordResult.getRecordId();
266264

267265
UpdateRecord record = new UpdateRecord()
268-
.withRecordId(recordId)
269-
.withField("fldjoQSlHfV2z", "Ps: Test Update, content is 'This is from unit Test update record' before")
270-
// select can be set null or empty array if you want to clear field value
271-
.withField("fldAY9mW7MUdW", Collections.emptyList())
272-
.withField("fld8w4C5fwDBl", Arrays.asList("LL", "NN"));
266+
.withRecordId(recordId)
267+
.withField("fldjoQSlHfV2z", "Ps: Test Update, content is 'This is from unit Test update record' before")
268+
// select can be set null or empty array if you want to clear field value
269+
.withField("fldAY9mW7MUdW", Collections.emptyList())
270+
.withField("fld8w4C5fwDBl", Arrays.asList("LL", "NN"));
273271

274272
UpdateRecordRequest updateRecordRequest = new UpdateRecordRequest()
275-
.withRecords(Collections.singletonList(record))
276-
.withFieldKey(FieldKey.ID);
273+
.withRecords(Collections.singletonList(record))
274+
.withFieldKey(FieldKey.ID);
277275

278276
List<Record> updateRecords = vikaApiClient.getRecordApi().updateRecords(DATASHEET_ID, updateRecordRequest);
279277
assertThat(updateRecords).isNotNull();
@@ -297,7 +295,7 @@ void testDeleteRecord() throws IOException, ApiException {
297295
assertThat(inputStream).isNotNull();
298296
List<RecordMap> recordMaps = JacksonJsonUtil.unmarshalInputStreamToList(RecordMap.class, inputStream);
299297
CreateRecordRequest createRecordRequest = new CreateRecordRequest()
300-
.withRecords(recordMaps);
298+
.withRecords(recordMaps);
301299
List<Record> newRecords = vikaApiClient.getRecordApi().addRecords(DATASHEET_ID, createRecordRequest);
302300
assertThat(newRecords).isNotNull();
303301
assertThat(newRecords).isNotEmpty();
@@ -311,7 +309,7 @@ void testDeleteRecord() throws IOException, ApiException {
311309
vikaApiClient.getRecordApi().deleteRecord(DATASHEET_ID, recordId);
312310

313311
// assert query whether record exist
314-
ApiQueryParam queryParam = ApiQueryParam.EMPTY.withRecordIds(Collections.singletonList(recordId));
312+
ApiQueryParam queryParam = ApiQueryParam.newInstance().withRecordIds(Collections.singletonList(recordId));
315313
Pager<Record> pager = vikaApiClient.getRecordApi().getRecords(DATASHEET_ID, queryParam);
316314
assertThat(pager).isNotNull();
317315
assertThat(pager.getTotalItems()).isZero();
@@ -337,7 +335,7 @@ void testDeleteRecordBatch() throws IOException, ApiException {
337335
vikaApiClient.getRecordApi().deleteRecords(DATASHEET_ID, recordIds);
338336

339337
// assert query whether record exist
340-
ApiQueryParam queryParam = ApiQueryParam.EMPTY.withRecordIds(recordIds);
338+
ApiQueryParam queryParam = ApiQueryParam.newInstance().withRecordIds(recordIds);
341339
Pager<Record> pager = vikaApiClient.getRecordApi().getRecords(DATASHEET_ID, queryParam);
342340
assertThat(pager).isNotNull();
343341
assertThat(pager.getTotalItems()).isZero();

client/src/test/java/cn/vika/client/api/RecordPagerTest.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@
5454
import static org.assertj.core.api.Assertions.assertThat;
5555

5656
/**
57-
*
5857
* @author Shawn Deng
59-
* @date 2021-02-24 13:18:26
6058
*/
6159
@TestMethodOrder(OrderAnnotation.class)
6260
public class RecordPagerTest {
@@ -164,7 +162,7 @@ void testPageWithPageSize() throws ApiException, JsonProcessingException, Interr
164162
@Test
165163
@Order(105)
166164
void testPageWithView() throws ApiException, JsonProcessingException, InterruptedException {
167-
ApiQueryParam queryParam = ApiQueryParam.EMPTY.withView(ConstantKey.TEST_VIEW_ID.get());
165+
ApiQueryParam queryParam = ApiQueryParam.newInstance().withView(ConstantKey.TEST_VIEW_ID.get());
168166
Pager<Record> pager = vikaApiClient.getRecordApi().getRecords(DATASHEET_ID, queryParam);
169167
assertThat(pager).isNotNull();
170168
int pageIndex = 0;
@@ -185,7 +183,7 @@ void testPageWithView() throws ApiException, JsonProcessingException, Interrupte
185183
@Order(106)
186184
void testPageWithSort() throws ApiException, JsonProcessingException, InterruptedException {
187185
String[] sorts = TEST_SORT.get().split(",");
188-
ApiQueryParam queryParam = ApiQueryParam.EMPTY;
186+
ApiQueryParam queryParam = ApiQueryParam.newInstance();
189187
for (String s : sorts) {
190188
String[] sort = s.split(":");
191189
queryParam.withSort(sort[0], of(sort[1]));
@@ -210,7 +208,7 @@ void testPageWithSort() throws ApiException, JsonProcessingException, Interrupte
210208
@Order(107)
211209
void testPageWithFields() throws ApiException, JsonProcessingException {
212210
String[] fields = ConstantKey.TEST_FIELDS.get().split(",");
213-
ApiQueryParam queryParam = ApiQueryParam.EMPTY.withFields(Arrays.asList(fields));
211+
ApiQueryParam queryParam = ApiQueryParam.newInstance().withFields(Arrays.asList(fields));
214212
Pager<Record> pager = vikaApiClient.getRecordApi().getRecords(DATASHEET_ID, queryParam);
215213
assertThat(pager).isNotNull();
216214
int pageIndex = 0;
@@ -236,7 +234,7 @@ void testPagesWithRecordIds() throws ApiException, JsonProcessingException, Inte
236234
randomRecordIds.add(initRecordIds.get(random.nextInt(initRecordIds.size())));
237235
}
238236
System.out.format("Record Id Filter List: %s", randomRecordIds);
239-
ApiQueryParam queryParam = ApiQueryParam.EMPTY.withRecordIds(randomRecordIds);
237+
ApiQueryParam queryParam = ApiQueryParam.newInstance().withRecordIds(randomRecordIds);
240238
Pager<Record> pager = vikaApiClient.getRecordApi().getRecords(DATASHEET_ID, queryParam);
241239
assertThat(pager).isNotNull();
242240
int pageIndex = 0;
@@ -256,7 +254,7 @@ void testPagesWithRecordIds() throws ApiException, JsonProcessingException, Inte
256254
@Test
257255
@Order(109)
258256
void testPagesWithFormulaFilter() throws ApiException, JsonProcessingException, InterruptedException {
259-
ApiQueryParam queryParam = ApiQueryParam.EMPTY.withFilter(ConstantKey.TEST_FILTER_FORMULA.get());
257+
ApiQueryParam queryParam = ApiQueryParam.newInstance().withFilter(ConstantKey.TEST_FILTER_FORMULA.get());
260258
Pager<Record> pager = vikaApiClient.getRecordApi().getRecords(DATASHEET_ID, queryParam);
261259
assertThat(pager).isNotNull();
262260
int pageIndex = 0;

0 commit comments

Comments
 (0)