root/trunk/test/unit/dbnp/studycapturing/AssayServiceTests.groovy @ 1456

Revision 1456, 8.4 KB (checked in by business@…, 3 years ago)

moved gdt package to org.dbnp, moved tests for RelTime?, TemplateEntity?, Template etc. to gdt plugin

Line 
1package dbnp.studycapturing
2
3import grails.test.*
4import org.apache.poi.ss.usermodel.Workbook
5import org.apache.poi.ss.usermodel.WorkbookFactory
6import org.apache.poi.ss.usermodel.Cell
7import grails.converters.JSON
8import org.dbnp.gdt.Term
9import org.dbnp.gdt.TemplateField
10import org.dbnp.gdt.Template
11import org.dbnp.gdt.TemplateFieldType
12
13/**
14 * AssayServiceTests Test
15 *
16 * @author  s.h.sikkema@gmail.com
17 * @since       20101218
18 * @package     dbnp.studycapturing
19 *
20 * Revision information:
21 * $Rev$
22 * $Author$
23 * $Date$
24 */
25class AssayServiceTests extends GrailsUnitTestCase {
26
27    def service = new AssayService()
28
29    protected void setUp() {
30        super.setUp()
31
32
33        mockDomain(Term,          [ new Term(id: 1, name: 'Human')])
34
35        mockDomain(TemplateField, [ new TemplateField(id: 1, name: 'tf1', type: TemplateFieldType.STRING),
36                                    new TemplateField(id: 2, name: 'tf2', type: TemplateFieldType.STRING),
37                                    new TemplateField(id: 3, name: 'tf3', type: TemplateFieldType.STRING)])
38
39        mockDomain(Template,      [ new Template(id: 1, fields: [TemplateField.get(1), TemplateField.get(2)]),
40                                    new Template(id: 2, fields: [TemplateField.get(3)])])
41
42        mockDomain(Subject,       [ new Subject(id: 1, name:'subject1', template: Template.get(1), species: Term.get(1)),
43                                    new Subject(id: 2, name:'subject2', template: Template.get(2), species: Term.get(1))])
44
45        mockDomain(SamplingEvent, [ new SamplingEvent(id:1, startTime: 2, duration: 5, sampleTemplate: new Template()),
46                                    new SamplingEvent(id:2, startTime: 12, duration: 15, sampleTemplate: new Template())])
47
48        mockDomain(Event,         [ new Event(id: 1, startTime: 6, endTime: 7), new Event(id: 2, startTime: 8, endTime: 9)])//, new Event(id: 2, startTime: 8, endTime: 9)])
49
50        mockDomain(EventGroup,    [ new EventGroup(id:1, name: 'EventGroup1', events: [Event.get(1)], samplingEvents: [SamplingEvent.get(1)]),
51                                    new EventGroup(id:2, name: 'EventGroup2', events: [Event.get(2)], samplingEvents: [SamplingEvent.get(2)])])
52
53        mockDomain(Sample,        [ new Sample(id: 1, name:'sample1', parentSubject: Subject.get(1), parentEvent: SamplingEvent.get(1), parentEventGroup: EventGroup.get(1)),
54                                    new Sample(id: 2, name:'sample2', parentSubject: Subject.get(2), parentEvent: SamplingEvent.get(2), parentEventGroup: EventGroup.get(2))])
55
56        mockDomain(Assay,         [ new Assay(id: 1, externalAssayID: 'assay1', samples:[Sample.get(1),Sample.get(2)]),
57                                    new Assay(id: 2, externalAssayID: 'assay1', samples:[])])
58
59        Subject.get(1).setFieldValue('tf1', 'tfv1')
60        Subject.get(1).setFieldValue('tf2', 'tfv2')
61        Subject.get(2).setFieldValue('tf3', 'tfv3')
62
63        // mock authentication service
64        service.authenticationService = [
65                isLoggedIn: { true },
66                logInRemotely: { a, b, c -> },
67                logOffRemotely: { a, b -> },
68                getLoggedInUser: { null }
69        ]
70
71    }
72
73    protected void tearDown() {
74        super.tearDown()
75    }
76
77    void testExportColumnWiseDataAsExcelFile() {
78
79        def columnData = [
80                Category1: [Column1: [1,2,3], Column2: [4,5,6]],
81                Category2: [Column3: [7,8,9], Column4: [10,11,12], Column5: [13,14,15]],
82                EmptyCategory: [:]
83        ]
84
85        def rowData = [
86                ['Category1','','Category2','',''],
87                ['Column1','Column2','Column3','Column4','Column5'],
88                [1,4,7,10,13],
89                [2,5,8,11,14],
90                [3,6,9,12,15]]
91
92        ByteArrayOutputStream   baos    = new ByteArrayOutputStream(1024)
93        DataOutputStream        dos     = new DataOutputStream(baos)
94
95        service.exportColumnWiseDataToExcelFile(columnData, dos, true)
96
97        ByteArrayInputStream    bais    = new ByteArrayInputStream(baos.toByteArray())
98
99        assertEquals 'Expected Excel contents', rowData, readExcelIntoArray(bais)
100
101    }
102
103    def readExcelIntoArray = { inputStream ->
104
105        Workbook wb = WorkbookFactory.create(inputStream)
106
107        def sheet = wb.getSheetAt(0)
108
109        def readData = []
110
111        sheet.eachWithIndex { row, ri ->
112
113            readData[ri] = []
114
115            row.eachWithIndex { cell, ci ->
116
117                readData[ri][ci] = (cell.cellType == Cell.CELL_TYPE_NUMERIC) ? cell.numericCellValue : cell.stringCellValue
118
119            }
120
121        }
122
123        readData
124
125    }
126
127    // class to test writing non number/string values to excel
128    class SomeCustomType { String toString() {'13'} }
129
130    void testExportRowWiseDataToExcelFile() {
131
132        SomeCustomType someCustomType = new SomeCustomType()
133
134        def rowData = [
135                ['Category1','','Category2','',''],
136                ['Column1','Column2','Column3','Column4','Column5'],
137                [1,4,7,10,someCustomType],
138                [2,5,8,11,null],
139                [3,6,9,12,15]]
140
141        ByteArrayOutputStream   baos    = new ByteArrayOutputStream(1024)
142        DataOutputStream        dos     = new DataOutputStream(baos)
143
144        service.exportRowWiseDataToExcelFile(rowData, dos, false)
145
146        ByteArrayInputStream    bais    = new ByteArrayInputStream(baos.toByteArray())
147
148        // replace custom type with expected written value
149        rowData[2][4] = '13'
150        rowData[3][4] = ''
151
152        def result = readExcelIntoArray(bais)
153
154        assertEquals 'Excel contents', rowData, result
155
156    }
157   
158    void testExportRowWiseDataToExcelFileWithRealisticData() {
159
160        def rowData = [
161                ['Subject Data', '', '', 'Sampling Event Data', '', '', 'Sample Data', '', ''],
162                ['name', 'species', 'Gender', 'startTime', 'sampleTemplate', 'Sample volume', 'name', 'material', 'Text on vial'],
163                [11, 'Homo Sapiens', 'Male', 367200, 'Human blood sample', 4.5, '11_A', 'blood plasma', 'T8.93650593495392']]
164
165        ByteArrayOutputStream   baos    = new ByteArrayOutputStream(1024)
166        DataOutputStream        dos     = new DataOutputStream(baos)
167
168        service.exportRowWiseDataToExcelFile(rowData, dos, false)
169
170        ByteArrayInputStream    bais    = new ByteArrayInputStream(baos.toByteArray())
171
172        def result = readExcelIntoArray(bais)
173
174        assertEquals 'Excel contents', rowData, result
175    }
176
177    void testTemplateFieldsAreCollected() {
178
179        def assay = Assay.get(1)
180
181        def consumer = 'http://metabolomics.nmcdsp.nl'
182
183        // mock URL's getText to be able to mock a http request
184        URL.metaClass.getText = {
185            new JSON ([['sample1', 'sample2', 'sample3'],
186              ['measurement1','measurement2','measurement3','measurement4'],
187              [1,2,3,4,5,6,7,8,9,10,11,12] ]).toString()
188        }
189
190        def assayData = service.collectAssayData(assay, consumer)
191
192        def sample1index = assayData.'Sample Data'.'name'.findIndexOf{it == 'sample1'}
193        def sample2index = assayData.'Sample Data'.'name'.findIndexOf{it == 'sample2'}
194
195        assertEquals 'Subject template field', ['tfv1',''], assayData.'Subject Data'.tf1[sample1index, sample2index]
196        assertEquals 'Subject template field', ['tfv2',''], assayData.'Subject Data'.tf2[sample1index, sample2index]
197        assertEquals 'Subject template field', ['','tfv3'], assayData.'Subject Data'.tf3[sample1index, sample2index]
198        assertEquals 'Subject species template field', ['Human', 'Human'], assayData.'Subject Data'.species*.toString()
199        assertEquals 'Subject name template field', ['subject1','subject2'], assayData.'Subject Data'.name[sample1index, sample2index]
200
201        assertEquals 'Sampling event template fields', [2,12], assayData.'Sampling Event Data'.startTime[sample1index, sample2index]
202        assertEquals 'Sampling event template fields', [5,15], assayData.'Sampling Event Data'.duration[sample1index, sample2index]
203        assertEquals 'Sampling event template fields', '[null, null]', assayData.'Sampling Event Data'.sampleTemplate.toString()
204        assertEquals 'Sample template fields', ['sample1', 'sample2'], assayData.'Sample Data'.name[sample1index, sample2index]
205
206        assertEquals 'Event group names', ['EventGroup1', 'EventGroup2'], assayData.'Event Group'.name[sample1index, sample2index]
207
208        assertEquals 'Module Measurement Data', ['measurement1': [1,5,9], 'measurement2': [2,6,10], 'measurement3': [3,7,11], 'measurement4': [4,8,12]], assayData.'Module Measurement Data'
209    }
210
211}
Note: See TracBrowser for help on using the browser.