Skip to content

Commit

Permalink
creacted a utitlity classand a dataset postgres failures
Browse files Browse the repository at this point in the history
  • Loading branch information
josephbate committed Apr 9, 2024
1 parent c500ccd commit 44e52e9
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Expand Up @@ -460,6 +460,13 @@
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.dbunit/dbunit -->
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.7.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
Expand Down Expand Up @@ -580,3 +587,4 @@
</plugins>
</build>
</project>

40 changes: 40 additions & 0 deletions src/test/java/org/openelisglobal/DbUnitUtil.java
@@ -0,0 +1,40 @@
package org.openelisglobal;


import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.dbunit.DBTestCase;
import org.dbunit.PropertiesBasedJdbcDatabaseTester;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;

public class DbUnitUtil extends DBTestCase {

private final String datasetName;
private final String dbDriverClass="org.postgresql.Driver";
private final String dbConnetionURL="jdbc:postgresql://localhost:5432/clinlims";
private final String userName="clinlims";
private final String password="clinlims";

public DbUnitUtil(String datasetName)
{
super();

this.datasetName = datasetName;

System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, dbDriverClass);
System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, dbConnetionURL );
System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, userName );
System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, password );
// System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_SCHEMA, "" );
}

public IDataSet getDataSet() throws Exception {
try {
return new FlatXmlDataSetBuilder().build(new FileInputStream(datasetName));
} catch (FileNotFoundException e) {
throw new RuntimeException("Failed to load dataset: " + datasetName, e);
}
}
}
19 changes: 19 additions & 0 deletions src/test/java/org/openelisglobal/person/PersonServiceTest.java
@@ -1,10 +1,13 @@
package org.openelisglobal.person;

import org.dbunit.IDatabaseTester;
import org.dbunit.PropertiesBasedJdbcDatabaseTester;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openelisglobal.BaseTestConfig;
import org.openelisglobal.DbUnitUtil;
import org.openelisglobal.patient.PatientTestConfig;
import org.openelisglobal.person.service.PersonService;
import org.openelisglobal.person.valueholder.Person;
Expand All @@ -23,10 +26,26 @@ public class PersonServiceTest {
@Autowired
PersonService personService;

DbUnitUtil dbUnitUtil;

@Before
public void init() throws Exception {
dbUnitUtil = new DbUnitUtil("src/test/resources/dataSetFiles/personDataSet.Xml");

// Set up the database connection and load dataset using DbUnitUtil
IDatabaseTester databaseTester = new PropertiesBasedJdbcDatabaseTester();
databaseTester.setDataSet(dbUnitUtil.getDataSet());
databaseTester.onSetup();
}

@Test
public void testingDataDataIsPopulated() {

int result =personService.getAllPersons().size() ;

// Perform assertions on the result
Assert.assertEquals(4, result); // Example assertion
}
@Test
public void createPerson_shouldCreateNewPerson() throws Exception {
String firstName = "John";
Expand Down
8 changes: 8 additions & 0 deletions src/test/resources/Data-Set-file/PersonDataSet.xml
@@ -0,0 +1,8 @@
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<person id="1" last_name= "Tom" first_name= "Mulalu" middle_name= "Ian" multiple_unit="" street_address="Lumu" city="Kampala" state="" zip_code="256" country="Uganda" work_phone="12345" home_phone="112233" cell_phone="776655" fax="6654" email="12sd@gmail.com" primary_phone="12345"/>
<person id="2" last_name= "Sanny" first_name= "Bijambiya" middle_name= "Von" multiple_unit="" street_address="Kasige" city="Kabale" state="" zip_code="256" country="Uganda" work_phone="67890" home_phone="445566" cell_phone="332211" fax="9881" email="weds@gmail.com" primary_phone="67890"/>
<person id="3" last_name= "Job" first_name= "Asiimwe" middle_name= "Laban" multiple_unit="" street_address="Bulindo" city="" state="Jinja" zip_code="256" country="Uganda" work_phone="09876" home_phone="778899" cell_phone="443377" fax="0055" email="qwee@gmail.com" primary_phone="09876"/>
<person id="4" last_name= "Jackie" first_name= "Nakazzi" middle_name= "Pretty" multiple_unit="" street_address="Zanna" city="" state="Masaka" zip_code="256" country="Uganda" work_phone="54321" home_phone="009988" cell_phone="112233" fax="2233" email="23@gmail.com" primary_phone="54321"/>

</dataset>

0 comments on commit 44e52e9

Please sign in to comment.