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

Revision 1545, 8.8 KB (checked in by work@…, 3 years ago)

- removed obsolete bgdt imports

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