Skip to content

Commit

Permalink
Merge pull request #1450 from Agaba-derrick/RegionServiceTests
Browse files Browse the repository at this point in the history
regionTestwith DataSets
  • Loading branch information
mozzy11 authored Jan 30, 2025
2 parents 442f9ca + 83951c3 commit 6cb1f00
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/test/java/org/openelisglobal/AppTestConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@
"org.openelisglobal.sampleitem.", "org.openelisglobal.analysis", "org.openelisglobal.result.service",
"org.openelisglobal.result.daoimpl", "org.openelisglobal.resultlimit", "org.openelisglobal.resultlimits",
"org.openelisglobal.typeoftestresult", "org.openelisglobal.samplehuman", "org.openelisglobal.provider",
"org.openelisglobal.role", "org.openelisglobal.organization" }, excludeFilters = {
"org.openelisglobal.role", "org.openelisglobal.organization", "org.openelisglobal.region.service",
"org.openelisglobal.region.dao" }, excludeFilters = {
@ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.openelisglobal.patient.controller.*"),
@ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.openelisglobal.provider.controller.*"),
@ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.openelisglobal.organization.controller.*"),
Expand Down
87 changes: 87 additions & 0 deletions src/test/java/org/openelisglobal/region/RegionServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.openelisglobal.region;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openelisglobal.BaseWebContextSensitiveTest;
import org.openelisglobal.region.service.RegionService;
import org.openelisglobal.region.valueholder.Region;
import org.springframework.beans.factory.annotation.Autowired;

public class RegionServiceTest extends BaseWebContextSensitiveTest {

@Autowired
RegionService regionService;

private Map<String, String> testRegions;

@Before
public void init() throws Exception {
executeDataSetWithStateManagement("testdata/region.xml");
}

@After
public void tearDown() {
List<Region> regions = regionService.getAll();
for (Region region : regions) {
if (region.getLastupdated() == null) {
region.setLastupdated(new Timestamp(System.currentTimeMillis()));
}
}
regionService.deleteAll(regions);

}

@Test
public void createRegion_shouldCreateNewRegion() throws Exception {
String regionName = "Midwest";
String regionId = "1";
Region region = createRegion(regionName, regionId);

String savedRegionId = regionService.insert(region);

Region savedRegion = regionService.get(savedRegionId);
Assert.assertEquals(regionName, savedRegion.getRegion());
Assert.assertNotNull(savedRegion.getId());
}

@Test
public void updateRegion_shouldUpdateExistingRegion() throws Exception {
Region existingRegion = regionService.get("2");
Assert.assertNotNull(existingRegion);

existingRegion.setRegion("Updated Northeast");
regionService.update(existingRegion);

Region updatedRegion = regionService.get("2");
Assert.assertEquals("Updated Northeast", updatedRegion.getRegion());
}

@Test
public void getAllRegions_shouldReturnAllRegions() throws Exception {
List<Region> regions = regionService.getAll();
Assert.assertEquals(5, regions.size());
}

@Test
public void deleteRegion_shouldRemoveAnExistingRegion() throws Exception {
Region region = regionService.get("1");
Assert.assertNotNull(region);

Assert.assertEquals(5, regionService.getAll().size());

regionService.delete(region);
Assert.assertEquals(4, regionService.getAll().size());
}

private Region createRegion(String regionName, String regionId) {
Region region = new Region();
region.setRegion(regionName);
region.setLastupdated(new Timestamp(System.currentTimeMillis()));
return region;
}
}
25 changes: 25 additions & 0 deletions src/test/resources/testdata/region.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version='1.0' encoding='UTF-8'?>
<!--
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations under
* the License.
*
* The Original Code is OpenELIS code.
*
* Copyright (C) ITECH, University of Washington, Seattle WA. All Rights Reserved.
-->
<dataset>
<!-- Region Entries -->

<region id="1" region="Midwest" lastupdated="2023-10-01 12:00:00.000" />
<region id="2" region="Northeast" lastupdated="2023-10-01 12:00:00.000" />
<region id="3" region="Southeast" lastupdated="2023-10-01 12:00:00.000" />
<region id="4" region="Southwest" lastupdated="2023-10-01 12:00:00.000" />
<region id="5" region="Northwest" lastupdated="2023-10-01 12:00:00.000" />
</dataset>

0 comments on commit 6cb1f00

Please sign in to comment.