Skip to content

Commit 12348c5

Browse files
committed
Added test, refactored function name improved update message #3466
1 parent ee7c702 commit 12348c5

File tree

6 files changed

+94
-8
lines changed

6 files changed

+94
-8
lines changed

grails-app/controllers/au/org/ala/merit/AdminController.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ class AdminController {
418418
session.status = status
419419
def fileIn = new FileInputStream(file)
420420
try {
421-
def result = importService.gmsImport(fileIn, status.projects, preview, update)
421+
def result = importService.projectImport(fileIn, status.projects, preview, update)
422422
status.finished = true
423423
status.error = result.error
424424
}

grails-app/services/au/org/ala/merit/ImportService.groovy

+3-3
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ class ImportService {
297297
organisations += metadataService.organisationList()?.list
298298
}
299299

300-
Map gmsImport(InputStream csv, List status, Boolean preview, Boolean update, String charEncoding = 'Cp1252') {
300+
Map projectImport(InputStream csv, List status, Boolean preview, Boolean update, String charEncoding = 'Cp1252') {
301301

302302
Map programs = [:].withDefault{name ->
303303
programService.getByName(name)
@@ -379,7 +379,7 @@ class ImportService {
379379

380380
void importAll(projectRows, List status, GmsMapper mapper, Boolean update) {
381381

382-
def projectDetails = mapper.mapProject(projectRows)
382+
def projectDetails = mapper.mapProject(projectRows, update)
383383

384384
def grantId = projectDetails.project.grantId?:'<not mapped>'
385385
def externalId = projectDetails.project.externalId?:'<not mapped>'
@@ -405,7 +405,7 @@ class ImportService {
405405
}
406406
}
407407

408-
def result = importProject(projectDetails.project, update) // Do not overwrite existing projects because of the impacts to sites / activities etc.
408+
def result = importProject(projectDetails.project, update)
409409

410410
if (result.project == 'existing' && !update) {
411411
status << [grantId:grantId, externalId:externalId, success:false, errors:['Project already exists in MERIT, skipping']]

src/integration-test/groovy/au/org/ala/fieldcapture/ImportProjectsSpec.groovy

+59
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class ImportProjectsSpec extends StubbedCasSpec {
4949
and:
5050
List rows2 = projectResults()
5151
rows2.size() == 2
52+
rows2[1].success == 'Yes'
5253

5354
when:
5455
to Organisation, 'test_organisation'
@@ -75,6 +76,8 @@ class ImportProjectsSpec extends StubbedCasSpec {
7576
and: "The data is relevant to the projects loaded"
7677
List rows = projectResults()
7778
rows.size() == 3
79+
rows[1].success == 'Yes'
80+
rows[2].success == 'Yes'
7881

7982
when:
8083
importProjects()
@@ -125,6 +128,7 @@ class ImportProjectsSpec extends StubbedCasSpec {
125128
and:
126129
List rows2 = projectResults()
127130
rows2.size() == 2
131+
rows2[1].success == 'Yes'
128132

129133
when: "We navigate to the program page to find the new imported project, then open it"
130134
to ProgramPage, 'configurable_meri_plan'
@@ -152,4 +156,59 @@ class ImportProjectsSpec extends StubbedCasSpec {
152156
adminContent.meriPlan.budget[0].budgetAmounts()*.value() == ["20000", "10000"]
153157

154158
}
159+
160+
def "Projects can be updated via import with only the fields provided in the spreadsheet being updated"() {
161+
162+
setup:
163+
File csv = new File(getClass().getResource("/grants-hub-update-data.csv").toURI())
164+
loginAsMeritAdmin(browser)
165+
166+
when:
167+
to ProjectImport
168+
checkUpdateCheckbox()
169+
attachFile(csv)
170+
171+
then: "The projects are validated and the validation results are displayed"
172+
waitFor{validateComplete()}
173+
174+
and: "The data is relevant to the projects loaded"
175+
projectResults().size() == 2
176+
177+
when:
178+
importProjects()
179+
180+
then:
181+
waitFor{loadComplete()}
182+
and:
183+
List rows2 = projectResults()
184+
rows2.size() == 2
185+
rows2[1].success == 'Yes'
186+
187+
when: "We navigate to the program page to find the new imported project, then open it"
188+
to ProgramPage, 'configurable_meri_plan'
189+
openProjectByGrantId('cep-1')
190+
191+
then:
192+
at RlpProjectPage
193+
194+
when:
195+
displayOverview()
196+
197+
then:
198+
overview.program.text() == "Configurable MERI Plan Program"
199+
overview.projectId.text() == "cep-1"
200+
overview.status.text().equalsIgnoreCase("Active")
201+
overview.externalIds*.text() == ["1234"]
202+
overview.description.text() == "Grants project description - updated"
203+
204+
when:
205+
openMeriPlanEditTab()
206+
207+
then:
208+
adminContent.meriPlan.budget.size() == 1
209+
adminContent.meriPlan.budget[0].description.value() == "Project funding"
210+
adminContent.meriPlan.budget[0].budgetAmounts()*.value() == ["1", "2"]
211+
212+
}
213+
155214
}

src/integration-test/groovy/pages/ProjectImport.groovy

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package pages
22

33
import geb.Page
4+
import geb.module.Checkbox
45

56
class ProjectImport extends Page {
67

@@ -12,6 +13,7 @@ class ProjectImport extends Page {
1213
fileInput { $('#fileUpload') }
1314
importButton { $('button[data-bind*=doImport]')}
1415
progressSummary { $('span[data-bind*=progressSummary]') }
16+
updateCheckbox { $('#update').module(Checkbox) }
1517
}
1618

1719
def attachFile(File file) {
@@ -22,15 +24,25 @@ class ProjectImport extends Page {
2224
importButton.click()
2325
}
2426

27+
def checkUpdateCheckbox() {
28+
updateCheckbox.check()
29+
}
30+
2531
List<List> projectResults() {
32+
33+
List columns = ["grantId", "externalId", "success", "errors", "messages"]
2634
List rows = []
2735
def progressTable = $('table.table')
2836
progressTable.find("tbody tr").each {
2937
List cols = []
3038
it.find("td").each { col ->
3139
cols << col.text()
3240
}
33-
rows << cols
41+
Map row = [:]
42+
columns.eachWithIndex{ col, index ->
43+
row[col] = cols[index]
44+
}
45+
rows << row
3446
}
3547

3648
rows

src/main/groovy/au/org/ala/merit/GmsMapper.groovy

+16-1
Original file line numberDiff line numberDiff line change
@@ -267,15 +267,30 @@ class GmsMapper {
267267
}
268268

269269
if (update) {
270-
Set propertiesToUpdate = new HashSet(project.keySet())
270+
Map flatProperties = flattenMap(project)
271+
Set propertiesToUpdate = flatProperties.keySet()
271272
propertiesToUpdate.remove('grantId') // This identifies the project and won't be updated.
272273
messages << "Update: ${propertiesToUpdate}"
274+
273275
}
274276

275277
[project:project, sites:sites, activities:activities, errors:errors, messages:messages, organisation:organisation]
276278

277279
}
278280

281+
private Map flattenMap(Map map, String separator = '.') {
282+
map.collectEntries { k, v ->
283+
if (v instanceof Map) {
284+
flattenMap(v, separator).collectEntries { k1, v1 ->
285+
String newKey = k+separator+k1
286+
[(newKey): v1]
287+
}
288+
} else {
289+
[(k): v]
290+
}
291+
}
292+
}
293+
279294
private Map mapProgram(LinkedHashMap<Object, Object> project, boolean update, ArrayList errors) {
280295
String programName = project.associatedSubProgram ?: project.associatedProgram
281296
if (!programName) {

src/test/groovy/au/org/ala/merit/ImportServiceSpec.groovy

+2-2
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class ImportServiceSpec extends Specification implements ServiceUnitTest<ImportS
165165

166166
when:
167167
List status = []
168-
Map result = service.gmsImport(csv, status, true, false)
168+
Map result = service.projectImport(csv, status, true, false)
169169

170170
then:
171171
1 * metadataService.organisationList() >> [list:[[name:"Test Organisation 2", organisationId:'org2Id', abn:'12345678901']]]
@@ -190,7 +190,7 @@ class ImportServiceSpec extends Specification implements ServiceUnitTest<ImportS
190190

191191
when:
192192
List status = []
193-
Map result = service.gmsImport(csv, status, false, false)
193+
Map result = service.projectImport(csv, status, false, false)
194194

195195
then:
196196
1 * metadataService.organisationList() >> [list:[[name:"Test Organisation 2", organisationId:'org2Id', abn:'12345678901']]]

0 commit comments

Comments
 (0)