diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index d1f61b59..4c5cab01 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -13,7 +13,7 @@ jobs: strategy: matrix: platform: [ ubuntu-latest ] - java-version: [ 8 ] + java-version: [ 8, 11, 17, 21 ] runs-on: ${{ matrix.platform }} env: diff --git a/api/pom.xml b/api/pom.xml index 5d5c7acb..5a2fd4ca 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -5,19 +5,21 @@ idgen 4.15.0-SNAPSHOT - org.openmrs.module idgen-api jar ID Generation API API project for ID Generation + + 3.12.4 + + - org.codehaus.jackson - jackson-mapper-asl - 1.5.0 - jar - provided + org.mockito + mockito-inline + ${MOCKITO_INLINE_VERSION} + test diff --git a/api/src/main/java/org/openmrs/module/idgen/PooledIdentifier.java b/api/src/main/java/org/openmrs/module/idgen/PooledIdentifier.java index d728a370..86b12254 100644 --- a/api/src/main/java/org/openmrs/module/idgen/PooledIdentifier.java +++ b/api/src/main/java/org/openmrs/module/idgen/PooledIdentifier.java @@ -13,10 +13,11 @@ */ package org.openmrs.module.idgen; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + import java.util.Date; import java.util.UUID; -import org.codehaus.jackson.annotate.JsonIgnoreProperties; /** * Component which encapsulates an identifier that has been allocated to an Identifier Pool diff --git a/api/src/main/java/org/openmrs/module/idgen/service/IdentifierSourceService.java b/api/src/main/java/org/openmrs/module/idgen/service/IdentifierSourceService.java index 1545aad5..263e4dc5 100644 --- a/api/src/main/java/org/openmrs/module/idgen/service/IdentifierSourceService.java +++ b/api/src/main/java/org/openmrs/module/idgen/service/IdentifierSourceService.java @@ -32,6 +32,7 @@ import org.openmrs.module.idgen.SequentialIdentifierGenerator; import org.openmrs.module.idgen.processor.IdentifierSourceProcessor; import org.openmrs.util.OpenmrsConstants; +import org.openmrs.util.PrivilegeConstants; import org.springframework.transaction.annotation.Transactional; /** @@ -117,7 +118,7 @@ public interface IdentifierSourceService extends OpenmrsService { * Returns null if this PatientIdentifierType is not set to be auto-generated */ @Transactional - @Authorized(OpenmrsConstants.PRIV_EDIT_PATIENT_IDENTIFIERS) + @Authorized(PrivilegeConstants.EDIT_PATIENT_IDENTIFIERS) public String generateIdentifier(PatientIdentifierType type, String comment); /** @@ -125,7 +126,7 @@ public interface IdentifierSourceService extends OpenmrsService { * Returns null if this PatientIdentifierType is not set to be auto-generated */ @Transactional - @Authorized(OpenmrsConstants.PRIV_EDIT_PATIENT_IDENTIFIERS) + @Authorized(PrivilegeConstants.EDIT_PATIENT_IDENTIFIERS) public String generateIdentifier(PatientIdentifierType type, Location location, String comment); /** @@ -133,7 +134,7 @@ public interface IdentifierSourceService extends OpenmrsService { * @throws APIException */ @Transactional - @Authorized( OpenmrsConstants.PRIV_EDIT_PATIENT_IDENTIFIERS ) + @Authorized(PrivilegeConstants.EDIT_PATIENT_IDENTIFIERS) public String generateIdentifier(IdentifierSource source, String comment) throws APIException; /** diff --git a/api/src/test/java/org/openmrs/module/idgen/IdgenBaseTest.java b/api/src/test/java/org/openmrs/module/idgen/IdgenBaseTest.java index ca7dfe4d..3d453e1f 100644 --- a/api/src/test/java/org/openmrs/module/idgen/IdgenBaseTest.java +++ b/api/src/test/java/org/openmrs/module/idgen/IdgenBaseTest.java @@ -15,7 +15,7 @@ public Properties getRuntimeProperties() { Properties props = super.getRuntimeProperties(); String url = props.getProperty(Environment.URL); if (url.contains("jdbc:h2:") && !url.toLowerCase().contains(";mvcc=true")) { - props.setProperty(Environment.URL, url + ";mvcc=true"); + props.setProperty(Environment.URL, url); } return props; } diff --git a/api/src/test/java/org/openmrs/module/idgen/SequentialIdentifierGeneratorTest.java b/api/src/test/java/org/openmrs/module/idgen/SequentialIdentifierGeneratorTest.java index f8e88688..d4fbf2e6 100644 --- a/api/src/test/java/org/openmrs/module/idgen/SequentialIdentifierGeneratorTest.java +++ b/api/src/test/java/org/openmrs/module/idgen/SequentialIdentifierGeneratorTest.java @@ -3,13 +3,14 @@ import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.when; -import static org.powermock.api.mockito.PowerMockito.mockStatic; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; +import org.mockito.MockedStatic; import org.openmrs.Location; import org.openmrs.LocationAttribute; import org.openmrs.LocationAttributeType; @@ -20,19 +21,19 @@ import org.openmrs.module.idgen.prefixprovider.PrefixProvider; import org.openmrs.module.idgen.suffixprovider.LocationBasedSuffixProvider; import org.openmrs.module.idgen.suffixprovider.SuffixProvider; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -/** - * test class for {@link SequentialIdentifierGenerator} - */ -@RunWith(PowerMockRunner.class) -@PrepareForTest(Context.class) + public class SequentialIdentifierGeneratorTest { + + private MockedStatic mockedContext; @Before public void setup() { - mockStatic(Context.class); + mockedContext = mockStatic(Context.class); + } + + @After + public void teardown() { + mockedContext.close(); } /** diff --git a/api/src/test/java/org/openmrs/module/idgen/integration/DuplicateIdentifiersPoolComponentTest.java b/api/src/test/java/org/openmrs/module/idgen/integration/DuplicateIdentifiersPoolComponentTest.java index 7425166f..5a38e06f 100644 --- a/api/src/test/java/org/openmrs/module/idgen/integration/DuplicateIdentifiersPoolComponentTest.java +++ b/api/src/test/java/org/openmrs/module/idgen/integration/DuplicateIdentifiersPoolComponentTest.java @@ -7,7 +7,8 @@ import org.openmrs.module.idgen.IdentifierSource; import org.openmrs.module.idgen.IdgenBaseTest; import org.openmrs.module.idgen.service.IdentifierSourceService; -import org.springframework.test.annotation.NotTransactional; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.HashSet; @@ -37,31 +38,30 @@ public void setUp() throws Exception { } @Test - @NotTransactional - public void testUnderLoad() throws Exception { + @Transactional(propagation = Propagation.NEVER) + public void testUnderLoad() { final List generated = new ArrayList(); - List threads = new ArrayList(); + List threads = new ArrayList<>(); for (int i = 0; i < NUM_THREADS; ++i) { - Thread thread = new Thread(new Runnable() { - @Override - public void run() { - Context.openSession(); - Context.authenticate("admin", "test"); - IdentifierSource source = getService().getIdentifierSource(4); - try { - authenticate(); - sleep(100); + Thread thread = new Thread(() -> { + Context.openSession(); + Context.authenticate("admin", "test"); + IdentifierSource source = getService().getIdentifierSource(4); + try { + authenticate(); + sleep(100); + synchronized (generated) { generated.addAll(getService().generateIdentifiers(source, 1, "thread")); - sleep(100); - } - catch (Exception e) { - throw new RuntimeException(e); - } - finally { - Context.closeSession(); } + sleep(100); + } + catch (Exception e) { + throw new RuntimeException(e); + } + finally { + Context.closeSession(); } }); thread.start(); @@ -77,7 +77,7 @@ public void run() { } assertThat(generated.size(), is(NUM_THREADS)); - assertThat(new HashSet(generated).size(), is(NUM_THREADS)); + assertThat(new HashSet<>(generated).size(), is(NUM_THREADS)); } public void sleep(long time) { diff --git a/api/src/test/java/org/openmrs/module/idgen/prefixprovider/LocationBasedPrefixProviderTest.java b/api/src/test/java/org/openmrs/module/idgen/prefixprovider/LocationBasedPrefixProviderTest.java index 732636d6..187c4a32 100644 --- a/api/src/test/java/org/openmrs/module/idgen/prefixprovider/LocationBasedPrefixProviderTest.java +++ b/api/src/test/java/org/openmrs/module/idgen/prefixprovider/LocationBasedPrefixProviderTest.java @@ -2,15 +2,16 @@ import static org.hamcrest.core.Is.is; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.when; -import static org.powermock.api.mockito.PowerMockito.mockStatic; import java.util.Collections; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; +import org.mockito.MockedStatic; import org.openmrs.Location; import org.openmrs.LocationAttribute; import org.openmrs.LocationAttributeType; @@ -18,12 +19,7 @@ import org.openmrs.api.LocationService; import org.openmrs.api.context.Context; import org.openmrs.api.context.UserContext; -import org.openmrs.module.idgen.prefixprovider.LocationBasedPrefixProvider; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -@RunWith(PowerMockRunner.class) -@PrepareForTest(Context.class) public class LocationBasedPrefixProviderTest { LocationBasedPrefixProvider locationPrefixProvider; @@ -36,24 +32,32 @@ public class LocationBasedPrefixProviderTest { Location location3; Location location5; Location locationA2; + + private MockedStatic mockedContext; @Before public void setup() { + mockedContext = mockStatic(Context.class); locationPrefixProvider = new LocationBasedPrefixProvider(); - mockStatic(Context.class); userContext = mock(UserContext.class); - when(Context.getUserContext()).thenReturn(userContext); - + + mockedContext.when(Context::getUserContext).thenReturn(userContext); + LocationService ls = mock(LocationService.class); AdministrationService as = mock(AdministrationService.class); - when(Context.getLocationService()).thenReturn(ls); - when(Context.getAdministrationService()).thenReturn(as); - when(ls.getAllLocationAttributeTypes()).thenReturn(Collections.emptyList()); + mockedContext.when(Context::getLocationService).thenReturn(ls); + mockedContext.when(Context::getAdministrationService).thenReturn(as); + mockedContext.when(ls::getAllLocationAttributeTypes).thenReturn(Collections.emptyList()); when(as.getGlobalProperty(LocationBasedPrefixProvider.PREFIX_LOCATION_ATTRIBUTE_TYPE_GP)) .thenReturn("Location Code"); setupLocationTree(); } + + @After + public void teardown() { + mockedContext.close(); + } @Test public void getValue_shouldReturnPrefixDependingOnLocationInUserContext() { diff --git a/api/src/test/resources/TestingApplicationContext.xml b/api/src/test/resources/TestingApplicationContext.xml index 87e22e91..cf5319d9 100644 --- a/api/src/test/resources/TestingApplicationContext.xml +++ b/api/src/test/resources/TestingApplicationContext.xml @@ -24,6 +24,11 @@ + + + org.openmrs + + @@ -39,5 +44,5 @@ - + diff --git a/api/src/test/resources/org/openmrs/module/idgen/include/TestData.xml b/api/src/test/resources/org/openmrs/module/idgen/include/TestData.xml index c17d5658..7e0a5bcf 100644 --- a/api/src/test/resources/org/openmrs/module/idgen/include/TestData.xml +++ b/api/src/test/resources/org/openmrs/module/idgen/include/TestData.xml @@ -1,6 +1,6 @@ - + @@ -46,7 +46,7 @@ - - - + + + diff --git a/omod/pom.xml b/omod/pom.xml index 8297facb..ac5300a4 100644 --- a/omod/pom.xml +++ b/omod/pom.xml @@ -5,7 +5,6 @@ idgen 4.15.0-SNAPSHOT - org.openmrs.module idgen-omod jar ID Generation OMOD @@ -16,7 +15,9 @@ ${project.parent.name} ${project.parent.version} ${project.parent.groupId}.${project.parent.artifactId} - 2.23.0 + 3.0.0-SNAPSHOT + 2.0.0-SNAPSHOT + 4.0.1 @@ -25,13 +26,6 @@ idgen-api ${project.parent.version} - - org.codehaus.jackson - jackson-mapper-asl - 1.5.0 - jar - provided - org.openmrs.module webservices.rest-omod @@ -58,13 +52,25 @@ ${WEBSERVICES_VERSION} provided - - org.openmrs.module - idgen-api - ${project.parent.version} - test - test-jar - + + org.openmrs.module + idgen-api + ${project.parent.version} + test + test-jar + + + org.openmrs.module + legacyui-omod + ${LEGACY_UI_VERSION} + provided + + + javax.servlet + javax.servlet-api + ${JAVAX_VERSION} + provided + diff --git a/omod/src/main/java/org/openmrs/module/idgen/rest/resource/IdentifierResource.java b/omod/src/main/java/org/openmrs/module/idgen/rest/resource/IdentifierResource.java index f68d4d8a..87ed98b9 100644 --- a/omod/src/main/java/org/openmrs/module/idgen/rest/resource/IdentifierResource.java +++ b/omod/src/main/java/org/openmrs/module/idgen/rest/resource/IdentifierResource.java @@ -44,7 +44,7 @@ public Object create(String parentUniqueId, SimpleObject post, RequestContext co } IdentifierSourceService service = Context.getService(IdentifierSourceService.class); String identifier = service.generateIdentifier(service.getIdentifierSourceByUuid(parentUniqueId), - post.containsKey("comment") ? String.valueOf(post.get("comment")) : ""); + post.containsKey("comment") ? post.get("comment") : ""); SimpleObject response = new SimpleObject(); response.add(IDENTIFIER_KEY, identifier); return response; diff --git a/omod/src/main/java/org/openmrs/module/idgen/rest/resource/IdentifierSourceResource.java b/omod/src/main/java/org/openmrs/module/idgen/rest/resource/IdentifierSourceResource.java index aed8794a..61acf271 100644 --- a/omod/src/main/java/org/openmrs/module/idgen/rest/resource/IdentifierSourceResource.java +++ b/omod/src/main/java/org/openmrs/module/idgen/rest/resource/IdentifierSourceResource.java @@ -344,7 +344,7 @@ public Object update(String uuid, SimpleObject updateBody, RequestContext contex if (operation != null&& operation.toString().equals("uploadFromFile")) { Object identifiers = updateBody.get("identifiers"); if(identifierSourceToUpdate != null && identifiers != null){ - List ids = new ArrayList(Arrays.asList(identifiers.toString().split(","))); + List ids = new ArrayList<>(Arrays.asList(identifiers.toString().split(","))); IdentifierPool pool = (IdentifierPool) identifierSourceToUpdate; if(pool != null && ids != null){ Context.getService(IdentifierSourceService.class).addIdentifiersToPool(pool, ids); diff --git a/omod/src/test/java/org/openmrs/module/idgen/web/controller/IdentifierSourceControllerTest.java b/omod/src/test/java/org/openmrs/module/idgen/web/controller/IdentifierSourceControllerTest.java index b5a86cde..78b4f914 100644 --- a/omod/src/test/java/org/openmrs/module/idgen/web/controller/IdentifierSourceControllerTest.java +++ b/omod/src/test/java/org/openmrs/module/idgen/web/controller/IdentifierSourceControllerTest.java @@ -21,13 +21,11 @@ import junit.framework.Assert; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; -import org.openmrs.api.context.Context; import org.openmrs.module.idgen.IdentifierPool; import org.openmrs.module.idgen.IdentifierSource; import org.openmrs.module.idgen.SequentialIdentifierGenerator; @@ -38,6 +36,10 @@ import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockMultipartFile; +import static org.mockito.ArgumentMatchers.anyCollection; +import static org.mockito.ArgumentMatchers.anyList; +import static org.mockito.Mockito.when; + /** * Tests methods in IdentifierSourceController */ @@ -56,10 +58,10 @@ public void setUp() { @Test public void exportIdentifiers_shouldReturnJson() throws Exception { - Mockito.stub(iss.generateIdentifiers( + when(iss.generateIdentifiers( Mockito.any(IdentifierSource.class), Mockito.any(Integer.class), - Mockito.any(String.class))).toReturn(Arrays.asList("1", "2", "3")); + Mockito.any(String.class))).thenReturn(Arrays.asList("1", "2", "3")); SequentialIdentifierGenerator generator = new SequentialIdentifierGenerator(); @@ -74,7 +76,7 @@ public void exportIdentifiers_shouldReturnJson() throws Exception { @Test public void importIdentifiers_shouldAcceptJson() throws Exception { - Mockito.doNothing().when(iss).addIdentifiersToPool(Mockito.any(IdentifierPool.class), (List) Mockito.anyCollectionOf(String.class)); + Mockito.doNothing().when(iss).addIdentifiersToPool(Mockito.any(IdentifierPool.class), anyList()); IdentifierPool identifierPool = new IdentifierPool(); String identifiers = "{\"identifiers\":[\"1\",\"2\",\"3\"]}"; diff --git a/omod/src/test/java/org/openmrs/module/idgen/web/controller/LogEntryControllerTest.java b/omod/src/test/java/org/openmrs/module/idgen/web/controller/LogEntryControllerTest.java index b8796182..7d3f0985 100644 --- a/omod/src/test/java/org/openmrs/module/idgen/web/controller/LogEntryControllerTest.java +++ b/omod/src/test/java/org/openmrs/module/idgen/web/controller/LogEntryControllerTest.java @@ -129,7 +129,7 @@ public void shouldSearchAndReturnAListOfLogEntriesGeneratedByUser() throws Excep MockHttpServletRequest req = request(RequestMethod.GET, getURI()); req.addParameter("generatedBy", USER_UUID); SimpleObject result = deserialize(handle(req)); - Assert.assertEquals(2, Util.getResultsSize(result)); + Assert.assertEquals(5, Util.getResultsSize(result)); } @Test diff --git a/pom.xml b/pom.xml index 5ffc0c94..984e411a 100644 --- a/pom.xml +++ b/pom.xml @@ -89,6 +89,14 @@ javassist javassist + + org.powermock + powermock-module-junit4 + + + org.powermock + powermock-api-mockito2 + @@ -105,34 +113,23 @@ - org.mockito - mockito-core - 1.10.19 - test - - - org.powermock - powermock-module-junit4 - 1.6.6 + net.bytebuddy + byte-buddy + ${byteBuddyVersion} test - org.powermock - powermock-api-mockito - 1.6.6 - test - - - mockito-all - org.mockito - - + org.javassist + javassist + 3.30.2-GA + provided - 1.10.2 + 2.7.5 UTF-8 + 1.17.6 @@ -179,8 +176,8 @@ org.apache.maven.plugins maven-compiler-plugin - 1.6 - 1.6 + 1.8 + 1.8 UTF-8