-
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.
- Loading branch information
1 parent
60d20af
commit 8d7a3b2
Showing
4 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
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,10 @@ | ||
[ | ||
{ | ||
"Name": "Users", | ||
"Description": "The users of the system" | ||
}, | ||
{ | ||
"Name": "Moderators", | ||
"Description": "The mods of the system" | ||
} | ||
] |
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
43 changes: 43 additions & 0 deletions
43
modules/global/src/de/diedavids/cuba/dataimport/converter/JsonImportDataConverter.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,43 @@ | ||
package de.diedavids.cuba.dataimport.converter | ||
|
||
import de.diedavids.cuba.dataimport.dto.DataRow | ||
import de.diedavids.cuba.dataimport.dto.DataRowImpl | ||
import de.diedavids.cuba.dataimport.dto.ImportData | ||
import de.diedavids.cuba.dataimport.dto.ImportDataImpl | ||
import groovy.json.JsonSlurper | ||
|
||
class JsonImportDataConverter implements ImportDataConverter { | ||
@Override | ||
ImportData convert(String content) { | ||
def result = new ImportDataImpl() | ||
|
||
def json = parseJson(content) | ||
|
||
json.each { | ||
result.columns = getColumns(it) | ||
addToTableData(result, it) | ||
} | ||
|
||
result | ||
} | ||
|
||
private Object parseJson(String content) { | ||
new JsonSlurper().parseText(content) | ||
} | ||
|
||
private List<String> getColumns(it) { | ||
new ArrayList(it.keySet()) | ||
} | ||
|
||
private DataRow addToTableData(ImportDataImpl importData, Map row) { | ||
def dataRow = DataRowImpl.ofMap(row) | ||
importData.rows << dataRow | ||
dataRow | ||
} | ||
|
||
@Override | ||
ImportData convert(File file) { | ||
convert(file.text) | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
...les/global/test/de/diedavids/cuba/dataimport/converter/JsonImportDataConverterSpec.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,46 @@ | ||
package de.diedavids.cuba.dataimport.converter | ||
|
||
import de.diedavids.cuba.dataimport.dto.ImportData | ||
import spock.lang.Specification | ||
|
||
class JsonImportDataConverterSpec extends Specification { | ||
private sut | ||
private String JSON_STRING = '''[ | ||
{ | ||
"Name": "Mark", | ||
"Lastname": "Andersson" | ||
}, | ||
{ | ||
"Name": "Pete", | ||
"Lastname": "Hansen" | ||
} | ||
]''' | ||
|
||
|
||
void setup() { | ||
sut = new JsonImportDataConverter() | ||
} | ||
|
||
def "convert contains the correct amount of columns"() { | ||
|
||
when: | ||
ImportData result = sut.convert(JSON_STRING) | ||
then: | ||
result.columns.size() == 2 | ||
} | ||
|
||
def "convert contains two DataRows"() { | ||
when: | ||
ImportData result = sut.convert(JSON_STRING) | ||
then: | ||
result.rows.size() == 2 | ||
} | ||
|
||
def "convert contains the correct values for the DataRows"() { | ||
when: | ||
ImportData result = sut.convert(JSON_STRING) | ||
then: | ||
result.rows[0].Name == 'Mark' | ||
result.rows[0].Lastname == 'Andersson' | ||
} | ||
} |