-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #936 from AtlasOfLivingAustralia/feature/issue934
Feature/issue934
- Loading branch information
Showing
7 changed files
with
365 additions
and
57 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
grails-app/controllers/au/org/ala/ecodata/DataSetSummaryController.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package au.org.ala.ecodata | ||
|
||
import org.apache.http.HttpStatus | ||
|
||
class DataSetSummaryController { | ||
|
||
static responseFormats = ['json', 'xml'] | ||
static allowedMethods = [update:['POST', 'PUT'], delete:'DELETE', bulkUpdate: 'POST'] | ||
|
||
ProjectService projectService | ||
|
||
/** Updates a single dataset for a project */ | ||
def update(String projectId) { | ||
Map dataSet = request.JSON | ||
projectId = projectId ?: dataSet.projectId | ||
|
||
if (!projectId) { | ||
render status: HttpStatus.SC_BAD_REQUEST, text: "projectId is required" | ||
return | ||
} | ||
|
||
if (dataSet.projectId && dataSet.projectId != projectId) { | ||
render status: HttpStatus.SC_BAD_REQUEST, text: "projectId must match the data set projectId" | ||
return | ||
} | ||
|
||
respond projectService.updateDataSet(projectId, dataSet) | ||
} | ||
|
||
/** | ||
* Updates multiple data sets for a project. | ||
* This endpoint exists to support the use case of associating multiple data sets with a | ||
* report and updating their publicationStatus when the report is submitted/approved. | ||
* | ||
* This method expects the projectId to be supplied via the URL and the data sets to be supplied in the request | ||
* body as a JSON object with key="dataSets" and value=List of data sets. | ||
*/ | ||
def bulkUpdate(String projectId) { | ||
Map postBody = request.JSON | ||
List dataSets = postBody?.dataSets | ||
|
||
if (!projectId) { | ||
render status: HttpStatus.SC_BAD_REQUEST, text: "projectId is required" | ||
return | ||
} | ||
|
||
for (Map dataSet in dataSets) { | ||
if (dataSet.projectId && dataSet.projectId != projectId) { | ||
render status: HttpStatus.SC_BAD_REQUEST, text: "projectId must match the projectId in all supplied data sets" | ||
return | ||
} | ||
} | ||
|
||
respond projectService.updateDataSets(projectId, dataSets) | ||
} | ||
|
||
def delete(String projectId, String dataSetId) { | ||
if (!projectId || !dataSetId) { | ||
render status: HttpStatus.SC_BAD_REQUEST, text: "projectId and dataSetId are required" | ||
return | ||
} | ||
respond projectService.deleteDataSet(projectId, dataSetId) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/test/groovy/au/org/ala/ecodata/DataSetSummaryControllerSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package au.org.ala.ecodata | ||
|
||
import grails.testing.web.controllers.ControllerUnitTest | ||
import org.apache.http.HttpStatus | ||
import spock.lang.Specification | ||
|
||
class DataSetSummaryControllerSpec extends Specification implements ControllerUnitTest<DataSetSummaryController> { | ||
|
||
ProjectService projectService = Mock(ProjectService) | ||
def setup() { | ||
controller.projectService = projectService | ||
} | ||
|
||
def cleanup() { | ||
} | ||
|
||
void "The update method delegates to the projectService"() { | ||
setup: | ||
String projectId = 'p1' | ||
Map dataSetSummary = [dataSetId:'d1', name:'Data set 1'] | ||
|
||
when: | ||
request.method = 'POST' | ||
request.json = dataSetSummary | ||
controller.update(projectId) | ||
|
||
then: | ||
1 * projectService.updateDataSet(projectId, dataSetSummary) >> [status:'ok'] | ||
response.json == ['status':'ok'] | ||
|
||
} | ||
|
||
void "A project id must be specified either in the path or as part of the data set summary"() { | ||
setup: | ||
Map dataSetSummary = [dataSetId: 'd1', name: 'Data set 1'] | ||
|
||
when: | ||
request.method = 'POST' | ||
request.json = dataSetSummary | ||
controller.update() | ||
|
||
then: | ||
0 * projectService.updateDataSet(_, _) | ||
response.status == HttpStatus.SC_BAD_REQUEST | ||
} | ||
|
||
void "The delete method delegates to the projectService"() { | ||
setup: | ||
String projectId = 'p1' | ||
String dataSetSummaryId = 'd1' | ||
|
||
when: | ||
request.method = 'DELETE' | ||
controller.delete(projectId, dataSetSummaryId) | ||
|
||
then: | ||
1 * projectService.deleteDataSet(projectId, dataSetSummaryId) >> [status:'ok'] | ||
response.json == ['status':'ok'] | ||
} | ||
|
||
void "The bulkUpdate method delegates to the projectService"() { | ||
setup: | ||
String projectId = 'p1' | ||
Map postBody = [dataSets:[[dataSetId:'d1', name:'Data set 1']]] | ||
|
||
when: | ||
request.method = 'POST' | ||
request.json = postBody | ||
controller.bulkUpdate(projectId) | ||
|
||
then: | ||
1 * projectService.updateDataSets(projectId, postBody.dataSets) >> [status:'ok'] | ||
response.json == ['status':'ok'] | ||
} | ||
|
||
void "If a projectId is present in a dataSet it much match the projectId parameter in bulkUpdate"() { | ||
setup: | ||
String projectId = 'p1' | ||
Map postBody = [dataSets:[[dataSetId:'d1', name:'Data set 1', projectId:'p1'], [dataSetId:'d2', name:'Data set 2', projectId:'p2']]] | ||
|
||
when: | ||
request.method = 'POST' | ||
request.json = postBody | ||
controller.bulkUpdate(projectId) | ||
|
||
then: | ||
0 * projectService.updateDataSets(_, _) | ||
response.status == HttpStatus.SC_BAD_REQUEST | ||
} | ||
} |
Oops, something went wrong.