Skip to content

Commit

Permalink
Added the possibility to send an empty json list in rest steps and fi…
Browse files Browse the repository at this point in the history
…xed 3 SonarLint warnings
  • Loading branch information
Roman.Stubna authored and romcooo committed Mar 28, 2020
1 parent 56a90d3 commit a127e1e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
10 changes: 10 additions & 0 deletions jbehave-support-core/docs/Rest-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ When [POST] request to [TEST]/[user/] is sent with data:
| addresses[2].city | Graz 2 | ADDRESS_2_CITY |
```

It's possible to send an empty list by using empty brackets.
In the following example, an empty list of documents will be sent:
```
When [POST] request to [TEST]/[user/] is sent with data:
| name | data | contextAlias |
| firstName | Ricky | FIRST_NAME |
| lastName | Gruber | LAST_NAME |
| documents[] | | |
```

To send a list at the root level use the following syntax:

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public class RestServiceHandler {
private static final String STATUS_HEADER = HEADER_START + "Status";
private static Pattern indexedKeyPattern = Pattern.compile("(.*)\\[(\\d+)\\]");
private static Pattern indexedKeyPattern2 = Pattern.compile("^\\[(\\d+)\\]\\.(.*)");
private static Pattern indexedKeyPattern3 = Pattern.compile("(.*)\\[(\\d*)\\]");
private static final String PERIOD_REGEX = "\\.(\\d+)(\\.)?";

private String url;
Expand Down Expand Up @@ -320,8 +321,8 @@ public int compare(final String o1, final String o2) {
if (result != 0) {
return result;
}
int o1Index = Integer.valueOf(o1Matcher.group(grpNumber + 1));
int o2Index = Integer.valueOf(o2Matcher.group(grpNumber + 1));
int o1Index = Integer.parseInt(o1Matcher.group(grpNumber + 1));
int o2Index = Integer.parseInt(o2Matcher.group(grpNumber + 1));
result = o1Index - o2Index;
if (result != 0) {
return result;
Expand All @@ -341,7 +342,7 @@ private void addEntryToMap(Map<String, Object> requestMap, String key, Object va
Matcher matcher = indexedKeyPattern.matcher(k1);
if (matcher.matches()) {
k1 = matcher.group(1);
index = Integer.valueOf(matcher.group(2));
index = Integer.parseInt(matcher.group(2));
}

Map<String, Object> map;
Expand Down Expand Up @@ -376,22 +377,24 @@ private void addEntryToMap(Map<String, Object> requestMap, String key, Object va
} else {
requestMap.put(k1, map);
}
} else if (key.matches(".*\\[\\d+\\].*")) { // not nested, collection element
Matcher matcher = indexedKeyPattern.matcher(key);
} else if (key.matches(".*\\[\\d*\\].*")) { // not nested, collection element
Matcher matcher = indexedKeyPattern3.matcher(key);
if (matcher.matches()) {
String k1 = matcher.group(1);
int index = Integer.valueOf(matcher.group(2));
List list;
if (requestMap.containsKey(k1)) {
list = (List) requestMap.get(k1);
} else {
list = new ArrayList<>();
}
if (list.size() > index) {
list.set(index, value);
} else {
list.add(value);
}
if (!matcher.group(2).isEmpty()) {
int index = Integer.parseInt(matcher.group(2));
if (list.size() > index) {
list.set(index, value);
} else {
list.add(value);
}
} // else, puts empty list
requestMap.put(k1, list);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,19 @@ When [POST] request to [TEST]/[user/] is sent with data:
| powerups[1] | {NULL} | string |

Then response from [TEST] REST API has status [200]

Scenario: json with empty list
When [POST] request to [TEST]/[user/] is sent with data:
| name | data | type |
| firstName | Mario | string |
| age | 24 | number |
| height | 1.56 | number |
| plumber | true | boolean |
| brother | Luigi | |
| princess | {NULL} | |
| weapons[] | | |
| cats[] | {NULL} | |
| hats[] | {NULL} | |
| hats[0] | topHat | |

Then response from [TEST] REST API has status [200]

0 comments on commit a127e1e

Please sign in to comment.