=== added directory 'dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/datavalueset' === added file 'dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/datavalueset/DataValue.java' --- dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/datavalueset/DataValue.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/datavalueset/DataValue.java 2011-02-15 06:46:27 +0000 @@ -0,0 +1,78 @@ +package org.hisp.dhis.importexport.datavalueset; + +/* + * Copyright (c) 2011, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlValue; + +@XmlAccessorType( XmlAccessType.FIELD ) +public class DataValue +{ + + @XmlAttribute( name = "dataElement", required = true ) + private String dataElementUuid; + + @XmlAttribute( name = "categoryOptionCombo" ) + private String categoryOptionComboUuid; + + @XmlValue + private String value; + + public String getDataElementUuid() + { + return dataElementUuid; + } + + public void setDataElementUuid( String dataElementUuid ) + { + this.dataElementUuid = dataElementUuid; + } + + public String getCategoryOptionComboUuid() + { + return categoryOptionComboUuid; + } + + public void setCategoryOptionComboUuid( String categoryOptionComboUuid ) + { + this.categoryOptionComboUuid = categoryOptionComboUuid; + } + + public String getValue() + { + return value; + } + + public void setValue( String value ) + { + this.value = value; + } + +} \ No newline at end of file === added file 'dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/datavalueset/DataValueSet.java' --- dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/datavalueset/DataValueSet.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/datavalueset/DataValueSet.java 2011-02-15 06:46:27 +0000 @@ -0,0 +1,108 @@ +package org.hisp.dhis.importexport.datavalueset; + +/* + * Copyright (c) 2011, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import java.util.List; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlAccessorType( XmlAccessType.FIELD ) +@XmlRootElement +public class DataValueSet +{ + + @XmlAttribute( name = "dataSet", required = true ) + private String dataSetUuid; + + @XmlAttribute( name = "orgUnit", required = true ) + private String organisationUnitUuid; + + @XmlAttribute( name = "period", required = true ) + private String periodIsoDate; + + @XmlAttribute + private String storedBy; + + @XmlElement( name = "dataValue" ) + private List dataValues; + + public String getDataSetUuid() + { + return dataSetUuid; + } + + public void setDataSetUuid( String dataSetUuid ) + { + this.dataSetUuid = dataSetUuid; + } + + public String getOrganisationUnitUuid() + { + return organisationUnitUuid; + } + + public void setOrganisationUnitUuid( String organisationUnitUuid ) + { + this.organisationUnitUuid = organisationUnitUuid; + } + + public String getPeriodIsoDate() + { + return periodIsoDate; + } + + public void setPeriodIsoDate( String periodIsoDate ) + { + this.periodIsoDate = periodIsoDate; + } + + public String getStoredBy() + { + return storedBy; + } + + public void setStoredBy( String storedBy ) + { + this.storedBy = storedBy; + } + + public List getDataValues() + { + return dataValues; + } + + public void setDataValues( List dataValues ) + { + this.dataValues = dataValues; + } + +} === added file 'dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/datavalueset/DataValueSetMapper.java' --- dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/datavalueset/DataValueSetMapper.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/datavalueset/DataValueSetMapper.java 2011-02-15 06:46:27 +0000 @@ -0,0 +1,178 @@ +package org.hisp.dhis.importexport.datavalueset; + +/* + * Copyright (c) 2011, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.hisp.dhis.dataelement.DataElement; +import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo; +import org.hisp.dhis.dataelement.DataElementCategoryService; +import org.hisp.dhis.dataelement.DataElementService; +import org.hisp.dhis.dataset.DataSet; +import org.hisp.dhis.dataset.DataSetService; +import org.hisp.dhis.datavalue.DataValue; +import org.hisp.dhis.organisationunit.OrganisationUnit; +import org.hisp.dhis.organisationunit.OrganisationUnitService; +import org.hisp.dhis.period.Period; + +public class DataValueSetMapper +{ + + private OrganisationUnitService organisationUnitService; + + private DataSetService dataSetService; + + private DataElementCategoryService categoryService; + + private DataElementService dataElementService; + + public List getDataValues( DataValueSet dataValueSet ) + { + Date timestamp = new Date(); + + DataSet dataSet = dataSetService.getDataSet( dataValueSet.getDataSetUuid() ); + + if ( dataSet == null ) + { + throw new IllegalArgumentException( "Data set with UUID " + dataValueSet.getDataSetUuid() + + " does not exist" ); + } + + OrganisationUnit unit = organisationUnitService.getOrganisationUnit( dataValueSet.getOrganisationUnitUuid() ); + + if ( unit == null ) + { + throw new IllegalArgumentException( "Org unit with UUID " + dataValueSet.getOrganisationUnitUuid() + + " does not exist" ); + } + + if ( !dataSet.getSources().contains( unit ) ) + { + throw new IllegalArgumentException( "Org unit with UUID " + dataValueSet.getOrganisationUnitUuid() + + " does not report data set with UUID " + dataSet.getUuid() ); + } + + Period period; + + try + { + period = dataSet.getPeriodType().createPeriod( dataValueSet.getPeriodIsoDate() ); + } + catch ( Exception e ) + { + throw new IllegalArgumentException( "Period " + dataValueSet.getPeriodIsoDate() + + " is not valid period of type " + dataSet.getPeriodType().getName() ); + } + + List dxfDataValues = dataValueSet.getDataValues(); + List dataValues = new ArrayList( dxfDataValues.size() ); + + for ( org.hisp.dhis.importexport.datavalueset.DataValue dxfValue : dxfDataValues ) + { + DataValue dataValue = getDataValue( dxfValue, dataSet ); + dataValue.setSource( unit ); + dataValue.setTimestamp( timestamp ); + dataValue.setStoredBy( dataValueSet.getStoredBy() ); + dataValue.setPeriod( period ); + + dataValues.add( dataValue ); + + } + + return dataValues; + } + + public DataValue getDataValue( org.hisp.dhis.importexport.datavalueset.DataValue dxfValue, DataSet dataSet ) + { + DataElement dataElement = dataElementService.getDataElement( dxfValue.getDataElementUuid() ); + + if ( dataElement == null ) + { + throw new IllegalArgumentException( "Data value with UUID " + dxfValue.getDataElementUuid() + + " does not exist" ); + } + + if ( !dataSet.getDataElements().contains( dataElement ) ) + { + throw new IllegalArgumentException( "Data element " + dataElement.getUuid() + " isn't in data set " + + dataSet.getUuid() ); + } + + DataValue dv = new DataValue(); + + dv.setDataElement( dataElement ); + + dv.setValue( dxfValue.getValue() ); + + DataElementCategoryOptionCombo combo; + + String comboId = dxfValue.getCategoryOptionComboUuid(); + + if ( comboId != null ) + { + combo = categoryService.getDataElementCategoryOptionCombo( Integer.parseInt( comboId ) ); + + if ( combo == null ) + { + throw new IllegalArgumentException( "DataElementCategoryOptionCombo with UUID " + comboId + + " does not exist" ); + } + } + else + { + combo = categoryService.getDefaultDataElementCategoryOptionCombo(); + } + + dv.setOptionCombo( combo ); + + return dv; + } + + public void setOrganisationUnitService( OrganisationUnitService organisationUnitService ) + { + this.organisationUnitService = organisationUnitService; + } + + public void setDataSetService( DataSetService dataSetService ) + { + this.dataSetService = dataSetService; + } + + public void setCategoryService( DataElementCategoryService categoryService ) + { + this.categoryService = categoryService; + } + + public void setDataElementService( DataElementService dataElementService ) + { + this.dataElementService = dataElementService; + } + +} === added file 'dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/datavalueset/package-info.java' --- dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/datavalueset/package-info.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/datavalueset/package-info.java 2011-02-15 06:46:27 +0000 @@ -0,0 +1,32 @@ +@javax.xml.bind.annotation.XmlSchema( + namespace = "http://dhis2.org/schema/dataValueSet/0.1", + xmlns = { @javax.xml.bind.annotation.XmlNs( prefix = "xs", namespaceURI = "http://www.w3.org/2001/XMLSchema" ) }, + elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED ) +package org.hisp.dhis.importexport.datavalueset; + +/* + * Copyright (c) 2011, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ === modified file 'dhis-2/dhis-services/dhis-service-importexport/src/main/resources/META-INF/dhis/beans.xml' --- dhis-2/dhis-services/dhis-service-importexport/src/main/resources/META-INF/dhis/beans.xml 2011-02-02 17:56:01 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/main/resources/META-INF/dhis/beans.xml 2011-02-15 06:46:27 +0000 @@ -405,4 +405,11 @@ + + + + + + + === added directory 'dhis-2/dhis-services/dhis-service-importexport/src/test/java/org/hisp/dhis/importexport/datavalueset' === added file 'dhis-2/dhis-services/dhis-service-importexport/src/test/java/org/hisp/dhis/importexport/datavalueset/DataValueSetMapperTest.java' --- dhis-2/dhis-services/dhis-service-importexport/src/test/java/org/hisp/dhis/importexport/datavalueset/DataValueSetMapperTest.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/test/java/org/hisp/dhis/importexport/datavalueset/DataValueSetMapperTest.java 2011-02-15 06:46:27 +0000 @@ -0,0 +1,238 @@ +package org.hisp.dhis.importexport.datavalueset; + +/* + * Copyright (c) 2011, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; +import java.util.List; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; + +import org.hisp.dhis.DhisTest; +import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo; +import org.hisp.dhis.dataelement.DataElementCategoryService; +import org.hisp.dhis.dataelement.DataElementService; +import org.hisp.dhis.dataset.DataSetService; +import org.hisp.dhis.datavalue.DataValue; +import org.hisp.dhis.datavalue.DataValueService; +import org.hisp.dhis.importexport.ImportException; +import org.hisp.dhis.importexport.ImportParams; +import org.hisp.dhis.importexport.ImportService; +import org.hisp.dhis.importexport.ImportStrategy; +import org.hisp.dhis.importexport.datavalueset.DataValueSet; +import org.hisp.dhis.importexport.datavalueset.DataValueSetMapper; +import org.hisp.dhis.importexport.util.ImportExportUtils; +import org.junit.Test; + +/** + * Messy test class checking that jaxb produces the expected java @link{DataValueSet data value set} structure, that + * the set is converted into a correct list of {@link DataValues data values} and also checks that it is stored.. + */ +public class DataValueSetMapperTest + extends DhisTest +{ + + private static final String ORGANISATION_UNIT_UUID = "9C1B1B5E-3D65-48F2-8D1D-D36C60DD7344"; + + private static final String DATA_SET_UUID = "16B2299E-ECD6-46CF-A61F-817D350C180D"; + + private static final String DATA_ELEMENT_UUID = "56B2299E-ECD6-46CF-A61F-817D350C180D"; + + private static final String DATA_ELEMENT_NOT_IN_SET_UUID = "96B2299E-ECD6-46CF-A61F-817D350C180D"; + + private DataValueSetMapper mapper; + + private DataValueSet dataValueSet; + + private ImportService importService; + + private ClassLoader classLoader; + + private DataElementCategoryOptionCombo defaultCombo; + + // ------------------------------------------------------------------------- + // Fixture + // ------------------------------------------------------------------------- + + @SuppressWarnings( "serial" ) + @Override + public void setUpTest() + throws JAXBException, IOException, ImportException + { + importService = (ImportService) getBean( "org.hisp.dhis.importexport.ImportService" ); + categoryService = (DataElementCategoryService) getBean( DataElementCategoryService.ID ); + dataElementService = (DataElementService) getBean( DataElementService.ID ); + dataSetService = (DataSetService) getBean( DataSetService.ID ); + dataValueService = (DataValueService) getBean( DataValueService.ID ); + + mapper = (DataValueSetMapper) getBean( "org.hisp.dhis.importexport.datavalueset.DataValueSetMapper" ); + + classLoader = Thread.currentThread().getContextClassLoader(); + + InputStream is = classLoader.getResourceAsStream( "datavalueset/base.xml" ); + ImportParams importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, false, false, + false ); + importService.importData( importParams, is ); + is.close(); + + dataValueSet = new DataValueSet(); + dataValueSet.setDataSetUuid( DATA_SET_UUID ); + dataValueSet.setPeriodIsoDate( "2011W5" ); + dataValueSet.setOrganisationUnitUuid( ORGANISATION_UNIT_UUID ); + dataValueSet.setStoredBy( "misterindia" ); + + final org.hisp.dhis.importexport.datavalueset.DataValue dv = new org.hisp.dhis.importexport.datavalueset.DataValue(); + dv.setDataElementUuid( DATA_ELEMENT_UUID ); + dv.setValue( "11" ); + + dataValueSet.setDataValues( new ArrayList() {{ add( dv ); }} ); + + defaultCombo = categoryService.getDefaultDataElementCategoryOptionCombo(); + + } + + // ------------------------------------------------------------------------- + // Tests + // ------------------------------------------------------------------------- + + @Test + public void testJaxb() + throws JAXBException, IOException + { + JAXBContext jc = JAXBContext.newInstance( DataValueSet.class, org.hisp.dhis.importexport.datavalueset.DataValue.class ); + Unmarshaller u = jc.createUnmarshaller(); + InputStream is = classLoader.getResourceAsStream( "datavalueset/dataValueSet.xml" ); + + DataValueSet dxfDataValueSet = (DataValueSet) u.unmarshal( is ); + is.close(); + + assertEquals( dataValueSet.getDataSetUuid(), dxfDataValueSet.getDataSetUuid() ); + assertEquals( dataValueSet.getPeriodIsoDate(), dxfDataValueSet.getPeriodIsoDate() ); + assertEquals( dataValueSet.getOrganisationUnitUuid(), dxfDataValueSet.getOrganisationUnitUuid() ); + assertEquals( dataValueSet.getStoredBy(), dxfDataValueSet.getStoredBy() ); + + assertEquals( 1, dxfDataValueSet.getDataValues().size() ); + + org.hisp.dhis.importexport.datavalueset.DataValue dv = dxfDataValueSet.getDataValues().get( 0 ); + + assertEquals( dataValueSet.getDataValues().get( 0 ).getDataElementUuid(), dv.getDataElementUuid() ); + + assertNull( dv.getCategoryOptionComboUuid() ); + } + + @Test + public void simpleMapping() + throws Exception + { + long before = new Date().getTime(); + + List dataValues = mapper.getDataValues( dataValueSet ); + + long after = new Date().getTime(); + + assertEquals( 1, dataValues.size() ); + + DataValue dv = dataValues.get( 0 ); + + verifyDataValue( before, after, dv ); + + dataValueService.addDataValue( dv ); + Collection persistedDataValues = dataValueService.getAllDataValues(); + assertEquals(1, persistedDataValues.size()); + + DataValue persisted = persistedDataValues.iterator().next(); + assertEquals( dv, persisted ); + verifyDataValue( before, after, persisted ); + + } + + @Test + public void missingThingsFromInput() { + + dataValueSet.setDataSetUuid( null ); + try { + mapper.getDataValues( dataValueSet ); + fail("Should miss data set"); + + } catch (IllegalArgumentException e) { + // Expected + } + + dataValueSet.setDataSetUuid( DATA_SET_UUID ); + dataValueSet.setOrganisationUnitUuid( "ladlalad" ); + try { + mapper.getDataValues( dataValueSet ); + fail("Should miss org unit"); + + } catch (IllegalArgumentException e) { + // Expected + } + + dataValueSet.setOrganisationUnitUuid( ORGANISATION_UNIT_UUID ); + + final org.hisp.dhis.importexport.datavalueset.DataValue dv = new org.hisp.dhis.importexport.datavalueset.DataValue(); + dv.setDataElementUuid( DATA_ELEMENT_NOT_IN_SET_UUID ); + dv.setValue( "11" ); + dataValueSet.getDataValues().add( dv ); + + try { + mapper.getDataValues( dataValueSet ); + fail("Should not accept extra data value"); + + } catch (IllegalArgumentException e) { + // Expected + System.out.println(e.getMessage()); + } + + } + + private void verifyDataValue( long before, long after, DataValue dv ) + { + assertEquals( DATA_ELEMENT_UUID, dv.getDataElement().getUuid() ); + assertEquals( ORGANISATION_UNIT_UUID, dv.getSource().getUuid() ); + assertEquals( "misterindia", dv.getStoredBy() ); + assertEquals( "11", dv.getValue() ); + + long time = dv.getTimestamp().getTime(); + assertTrue( time >= before ); + assertTrue( time <= after ); + + assertEquals( defaultCombo, dv.getOptionCombo() ); + } + +} === added directory 'dhis-2/dhis-services/dhis-service-importexport/src/test/resources/datavalueset' === added file 'dhis-2/dhis-services/dhis-service-importexport/src/test/resources/datavalueset/base.xml' --- dhis-2/dhis-services/dhis-service-importexport/src/test/resources/datavalueset/base.xml 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/test/resources/datavalueset/base.xml 2011-02-15 06:46:27 +0000 @@ -0,0 +1,158 @@ + + + + + 9 + default + + + + + + 10 + default + + + + + + 11 + default + 9 + + + + + + 12 + default + + + + + + 13 + + 12 + default + + + + 10 + default + + + + + + + + 11 + 10 + 1 + + + + + + 12 + 11 + 1 + + + + + + 21 + 56B2299E-ECD6-46CF-A61F-817D350C180D + Data element 1 + + data element 1 + ELEM1 + + true + int + sum + 12 + 2011-02-14 + + + 99 + 96B2299E-ECD6-46CF-A61F-817D350C180D + Not in data set + not in + ELEM2 + + true + int + sum + 12 + 2011-02-14 + + + + + + 20 + 16B2299E-ECD6-46CF-A61F-817D350C180D + + + DATASET1 + Weekly + + + + + + + + + 22 + 9C1B1B5E-3D65-48F2-8D1D-D36C60DD7344 + OrgUnit1 + OrgUnit1 + UNIT1 + 1990-01-01 + + true + + + + + 2011-02-14 + + + + + + + + + 14 + Type + Type of organisation unit, examples are PHU, chiefdom and district + false + + + 15 + Ownership + Ownership of organisation unit, examples are private and public + false + + + + + + 0 + 1 + Level 1 + + + + + + 20 + 22 + + + === added file 'dhis-2/dhis-services/dhis-service-importexport/src/test/resources/datavalueset/dataValueSet.xml' --- dhis-2/dhis-services/dhis-service-importexport/src/test/resources/datavalueset/dataValueSet.xml 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/test/resources/datavalueset/dataValueSet.xml 2011-02-15 06:46:27 +0000 @@ -0,0 +1,8 @@ + + + + + 11 +