Skip to content
This repository has been archived by the owner on Mar 31, 2022. It is now read-only.

Commit

Permalink
Bulk Update Entities: when a single JSON is in the request HTTP 500 i…
Browse files Browse the repository at this point in the history
…s returned #46
  • Loading branch information
Desire456 committed Aug 12, 2021
1 parent 351e154 commit 31d0dca
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,14 @@ public ResponseInfo updateEntities(String entitiesJson,
responseFetchPlan = restControllerUtils.getView(metaClass, responseView);
}

JsonArray entitiesJsonArray = new JsonParser().parse(entitiesJson).getAsJsonArray();
JsonElement entitiesJsonElement = new JsonParser().parse(entitiesJson);
if (!entitiesJsonElement.isJsonArray()) {
throw new RestAPIException("The body of bulk update request should be an array",
"The body of bulk update request should be an array",
HttpStatus.BAD_REQUEST);
}

JsonArray entitiesJsonArray = entitiesJsonElement.getAsJsonArray();
Collection<Object> entities = new ArrayList<>();
if (restProperties.isResponseFetchPlanEnabled() && responseFetchPlan != null) {
for (JsonElement jsonElement : entitiesJsonArray) {
Expand Down
44 changes: 44 additions & 0 deletions sample-rest/src/test/groovy/entities/EntitiesControllerTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package entities


import test_support.RestSpec

import static test_support.DataUtils.createGroup
Expand All @@ -29,6 +30,8 @@ class EntitiesControllerTest extends RestSpec {

createUser(dirtyData, sql, "admin", groupId)
createUser(dirtyData, sql, "anonymous", groupId)
createUser(dirtyData, sql, "login1", "testFirstName", groupId)
createUser(dirtyData, sql, "login2", "testFirstName", groupId)
}

def "GET-request with filter for obtaining the count of entities"() {
Expand Down Expand Up @@ -71,4 +74,45 @@ class EntitiesControllerTest extends RestSpec {
response.statusCode() == 200
response.body.as(Integer) == 1
}

def "PUT-request to bulk update (handling case of body containing one object instead of array)"() {
def body =
[
'firstName': 'Some name'
]
when:
def request = createRequest(userToken).body(body)
def response = request.with().put(baseUrl + "/entities/sec\$User")
then:
noExceptionThrown()
response.statusCode == 400
}
def "PUT-request to bulk update"() {
def userRows = sql.rows("select * from SAMPLE_REST_SEC_USER where FIRST_NAME = 'testFirstName'")
def body =
[
[
'id' : userRows[0].id,
'firstName': 'Some name'
],
[
'id' : userRows[1].id,
'firstName': 'Some name'
]
]
when:
def request = createRequest(userToken).body(body)
def response = request.with().put(baseUrl + "/entities/sec\$User")
then:
response.statusCode == 200
sql.rows("select * from SAMPLE_REST_SEC_USER where FIRST_NAME = 'testFirstName'").size() == 0
sql.rows("select * from SAMPLE_REST_SEC_USER where FIRST_NAME = 'Some name'").size() == 2
}
}
16 changes: 16 additions & 0 deletions sample-rest/src/test/groovy/test_support/DataUtils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ class DataUtils {
return userId
}

static UUID createUser(DataSet dataSet, Sql sql,
String login,
String firstName,
UUID groupId) {
def userId = dataSet.createUserUuid()
sql.dataSet('sample_rest_sec_user').add(
id: userId,
version: 1,
login: login,
login_lc: login.toLowerCase(),
first_name: firstName,
group_id: groupId
)
return userId
}

static UUID createRole(DataSet dataSet, Sql sql, String name, String securityScope) {
def roleId = dataSet.createRoleUuid()
sql.dataSet('sample_rest_sec_role').add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package test_support;

import test_support.RestTestUtils;
import io.restassured.RestAssured;
import io.restassured.specification.RequestSpecification;

Expand Down

0 comments on commit 31d0dca

Please sign in to comment.