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

Revision 1473, 8.9 KB (checked in by s.h.sikkema@…, 3 years ago)

Fixed tests and removed obsolete ones so grails test-app works with all tests passed

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 authenticationService
64        service.authenticationService = [
65                isLoggedIn: { true },
66                logInRemotely: { a, b, c -> },
67                logOffRemotely: { a, b -> },
68                getLoggedInUser: { null }
69        ]
70
71        // mock moduleCommunicationService
72        service.moduleCommunicationService = [
73                isModuleReachable: { a -> true },
74                callModuleRestMethodJSON: { consumer, path ->
75                    [['sample1', 'sample2', 'sample3'],
76                     ['measurement1','measurement2','measurement3','measurement4'],
77                     [1,2,3,4,5,6,7,8,9,10,11,12] ]
78                }
79        ]
80
81    }
82
83    protected void tearDown() {
84        super.tearDown()
85    }
86
87    void testExportColumnWiseDataAsExcelFile() {
88
89        def columnData = [
90                Category1: [Column1: [1,2,3], Column2: [4,5,6]],
91                Category2: [Column3: [7,8,9], Column4: [10,11,12], Column5: [13,14,15]],
92                EmptyCategory: [:]
93        ]
94
95        def rowData = [
96                ['Category1','','Category2','',''],
97                ['Column1','Column2','Column3','Column4','Column5'],
98                [1,4,7,10,13],
99                [2,5,8,11,14],
100                [3,6,9,12,15]]
101
102        ByteArrayOutputStream   baos    = new ByteArrayOutputStream(1024)
103        DataOutputStream        dos     = new DataOutputStream(baos)
104
105        service.exportColumnWiseDataToExcelFile(columnData, dos, true)
106
107        ByteArrayInputStream    bais    = new ByteArrayInputStream(baos.toByteArray())
108
109        assertEquals 'Expected Excel contents', rowData, readExcelIntoArray(bais)
110
111    }
112
113    def readExcelIntoArray = { inputStream ->
114
115        Workbook wb = WorkbookFactory.create(inputStream)
116
117        def sheet = wb.getSheetAt(0)
118
119        def readData = []
120
121        sheet.eachWithIndex { row, ri ->
122
123            readData[ri] = []
124
125            row.eachWithIndex { cell, ci ->
126
127                readData[ri][ci] = (cell.cellType == Cell.CELL_TYPE_NUMERIC) ? cell.numericCellValue : cell.stringCellValue
128
129            }
130
131        }
132
133        readData
134
135    }
136
137    // class to test writing non number/string values to excel
138    class SomeCustomType { String toString() {'13'} }
139
140    void testExportRowWiseDataToExcelFile() {
141
142        SomeCustomType someCustomType = new SomeCustomType()
143
144        def rowData = [
145                ['Category1','','Category2','',''],
146                ['Column1','Column2','Column3','Column4','Column5'],
147                [1,4,7,10,someCustomType],
148                [2,5,8,11,null],
149                [3,6,9,12,15]]
150
151        ByteArrayOutputStream   baos    = new ByteArrayOutputStream(1024)
152        DataOutputStream        dos     = new DataOutputStream(baos)
153
154        service.exportRowWiseDataToExcelFile(rowData, dos, false)
155
156        ByteArrayInputStream    bais    = new ByteArrayInputStream(baos.toByteArray())
157
158        // replace custom type with expected written value
159        rowData[2][4] = '13'
160        rowData[3][4] = ''
161
162        def result = readExcelIntoArray(bais)
163
164        assertEquals 'Excel contents', rowData, result
165
166    }
167   
168    void testExportRowWiseDataToExcelFileWithRealisticData() {
169
170        def rowData = [
171                ['Subject Data', '', '', 'Sampling Event Data', '', '', 'Sample Data', '', ''],
172                ['name', 'species', 'Gender', 'startTime', 'sampleTemplate', 'Sample volume', 'name', 'material', 'Text on vial'],
173                [11, 'Homo Sapiens', 'Male', 367200, 'Human blood sample', 4.5, '11_A', 'blood plasma', 'T8.93650593495392']]
174
175        ByteArrayOutputStream   baos    = new ByteArrayOutputStream(1024)
176        DataOutputStream        dos     = new DataOutputStream(baos)
177
178        service.exportRowWiseDataToExcelFile(rowData, dos, false)
179
180        ByteArrayInputStream    bais    = new ByteArrayInputStream(baos.toByteArray())
181
182        def result = readExcelIntoArray(bais)
183
184        assertEquals 'Excel contents', rowData, result
185    }
186
187    void testTemplateFieldsAreCollected() {
188
189        def assay = Assay.get(1)
190
191        def consumer = 'http://metabolomics.nmcdsp.nl'
192
193//        // mock URL's getText to be able to mock a http request
194//        URL.metaClass.getText = {
195//            new JSON ([['sample1', 'sample2', 'sample3'],
196//              ['measurement1','measurement2','measurement3','measurement4'],
197//              [1,2,3,4,5,6,7,8,9,10,11,12] ]).toString()
198//        }
199
200        def assayData = service.collectAssayData(assay, consumer)
201
202        println assayData
203
204        def sample1index = assayData.'Sample Data'.'name'.findIndexOf{it == 'sample1'}
205        def sample2index = assayData.'Sample Data'.'name'.findIndexOf{it == 'sample2'}
206
207        assertEquals 'Subject template field', ['tfv1',''], assayData.'Subject Data'.tf1[sample1index, sample2index]
208        assertEquals 'Subject template field', ['tfv2',''], assayData.'Subject Data'.tf2[sample1index, sample2index]
209        assertEquals 'Subject template field', ['','tfv3'], assayData.'Subject Data'.tf3[sample1index, sample2index]
210        assertEquals 'Subject species template field', ['Human', 'Human'], assayData.'Subject Data'.species*.toString()
211        assertEquals 'Subject name template field', ['subject1','subject2'], assayData.'Subject Data'.name[sample1index, sample2index]
212
213        assertEquals 'Sampling event template fields', [2,12], assayData.'Sampling Event Data'.startTime[sample1index, sample2index]
214        assertEquals 'Sampling event template fields', [5,15], assayData.'Sampling Event Data'.duration[sample1index, sample2index]
215        assertEquals 'Sampling event template fields', '[null, null]', assayData.'Sampling Event Data'.sampleTemplate.toString()
216        assertEquals 'Sample template fields', ['sample1', 'sample2'], assayData.'Sample Data'.name[sample1index, sample2index]
217
218        assertEquals 'Event group names', ['EventGroup1', 'EventGroup2'], assayData.'Event Group'.name[sample1index, sample2index]
219
220        assertEquals 'Module Measurement Data', ['measurement1': [1,5,9], 'measurement2': [2,6,10], 'measurement3': [3,7,11], 'measurement4': [4,8,12]], assayData.'Module Measurement Data'
221    }
222
223}
Note: See TracBrowser for help on using the browser.