Skip to content

Commit 1b206b8

Browse files
committed
Fix test errors
1 parent 463e090 commit 1b206b8

File tree

15 files changed

+196
-131
lines changed

15 files changed

+196
-131
lines changed

omod/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,22 @@
117117
<artifactId>jsp-api</artifactId>
118118
</dependency>
119119

120+
<dependency>
121+
<groupId>org.mockito</groupId>
122+
<artifactId>mockito-inline</artifactId>
123+
<scope>test</scope>
124+
</dependency>
125+
126+
<dependency>
127+
<groupId>net.bytebuddy</groupId>
128+
<artifactId>byte-buddy</artifactId>
129+
</dependency>
130+
131+
<dependency>
132+
<groupId>net.bytebuddy</groupId>
133+
<artifactId>byte-buddy-agent</artifactId>
134+
</dependency>
135+
120136
</dependencies>
121137

122138
<build>

omod/src/main/java/org/openmrs/web/controller/ForgotPasswordFormController.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,12 @@ protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse
172172

173173
try {
174174
Context.addProxyPrivilege(PrivilegeConstants.EDIT_USER_PASSWORDS);
175+
Context.addProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
175176
Context.getUserService().changePassword(user, randomPassword);
176177
}
177178
finally {
178179
Context.removeProxyPrivilege(PrivilegeConstants.EDIT_USER_PASSWORDS);
180+
Context.removeProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
179181
}
180182

181183
httpSession.setAttribute("resetPassword", randomPassword);
@@ -204,7 +206,15 @@ protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse
204206
public String getRandomPassword() {
205207
//Password should be satisfy the minimum length if any is set, must have 1 upper case letter and 1 number
206208
Integer minLength = 8;
207-
String str = Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GP_PASSWORD_MINIMUM_LENGTH);
209+
String str;
210+
211+
try {
212+
Context.getUserContext().addProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
213+
str = Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GP_PASSWORD_MINIMUM_LENGTH);
214+
} finally {
215+
Context.getUserContext().removeProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
216+
}
217+
208218
if (StringUtils.isNotBlank(str)) {
209219
minLength = Integer.valueOf(str);
210220
}

omod/src/main/java/org/openmrs/web/controller/LoginController.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ public String handleRequest(WebRequest webRequest, ModelMap model) {
121121
//was no exception message that might contain the required privilege
122122

123123
//will be sending the alert via ajax, so we need to escape js special chars
124-
model.put("alertMessage", JavaScriptUtils.javaScriptEscape(alertMessage));
124+
if(alertMessage != null) {
125+
model.put("alertMessage", JavaScriptUtils.javaScriptEscape(alertMessage));
126+
}
127+
125128
model.put("reason", reason);
126129
model.put("refererUrl", refererUrl);
127130
}

omod/src/main/java/org/openmrs/web/controller/person/PersonAttributeTypeListController.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.openmrs.api.context.Context;
2828
import org.openmrs.messagesource.MessageSourceService;
2929
import org.openmrs.util.OpenmrsConstants;
30+
import org.openmrs.util.PrivilegeConstants;
3031
import org.openmrs.web.WebConstants;
3132
import org.springframework.stereotype.Controller;
3233
import org.springframework.ui.ModelMap;
@@ -54,18 +55,23 @@ public class PersonAttributeTypeListController {
5455
public String displayPage(ModelMap modelMap) throws Exception {
5556

5657
AdministrationService as = Context.getAdministrationService();
57-
58-
// some helpful information that gets displayed
59-
modelMap.put("patientListingAttributeTypes",
60-
as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_LISTING_ATTRIBUTES));
61-
modelMap.put("patientViewingAttributeTypes",
62-
as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_VIEWING_ATTRIBUTES));
63-
modelMap.put("patientHeaderAttributeTypes",
64-
as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_HEADER_ATTRIBUTES));
65-
modelMap.put("userListingAttributeTypes",
66-
as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_USER_LISTING_ATTRIBUTES));
67-
modelMap.put("userViewingAttributeTypes",
68-
as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_USER_VIEWING_ATTRIBUTES));
58+
59+
try {
60+
Context.addProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
61+
// some helpful information that gets displayed
62+
modelMap.put("patientListingAttributeTypes",
63+
as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_LISTING_ATTRIBUTES));
64+
modelMap.put("patientViewingAttributeTypes",
65+
as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_VIEWING_ATTRIBUTES));
66+
modelMap.put("patientHeaderAttributeTypes",
67+
as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_HEADER_ATTRIBUTES));
68+
modelMap.put("userListingAttributeTypes",
69+
as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_USER_LISTING_ATTRIBUTES));
70+
modelMap.put("userViewingAttributeTypes",
71+
as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_USER_VIEWING_ATTRIBUTES));
72+
} finally {
73+
Context.removeProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
74+
}
6975

7076
List<PersonAttributeType> attributeTypeList = new Vector<PersonAttributeType>();
7177

omod/src/test/java/org/openmrs/module/web/extension/ExtensionUtilTest.java

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,51 @@
1313
import static org.junit.Assert.assertNotNull;
1414
import static org.junit.Assert.assertTrue;
1515
import static org.mockito.Mockito.mock;
16+
import static org.mockito.Mockito.mockStatic;
1617
import static org.mockito.Mockito.when;
17-
import static org.powermock.api.mockito.PowerMockito.mockStatic;
1818

1919
import java.util.ArrayList;
2020
import java.util.HashSet;
2121
import java.util.List;
2222
import java.util.Set;
2323

2424
import org.junit.Test;
25-
import org.junit.runner.RunWith;
25+
import org.junit.jupiter.api.extension.ExtendWith;
26+
import org.mockito.MockedStatic;
27+
import org.mockito.junit.jupiter.MockitoExtension;
2628
import org.openmrs.module.Extension;
2729
import org.openmrs.module.ModuleFactory;
2830
import org.openmrs.module.web.extension.provider.Link;
29-
import org.powermock.core.classloader.annotations.PrepareForTest;
30-
import org.powermock.modules.junit4.PowerMockRunner;
3131

32-
@RunWith(PowerMockRunner.class)
33-
@PrepareForTest(ModuleFactory.class)
32+
@ExtendWith(MockitoExtension.class)
3433
public class ExtensionUtilTest {
35-
34+
3635
/**
3736
* @see ExtensionUtil#getFormsModulesCanAddEncounterToVisit()
3837
* @verifies return empty set if there is no AddEncounterToVisitExtension
3938
*/
4039
@Test
41-
public void getModulesAddEncounterToVisitLinks_shouldReturnEmptySetIfThereIsNoAddEncounterToVisitExtension()
42-
throws Exception {
40+
public void getModulesAddEncounterToVisitLinks_shouldReturnEmptySetIfThereIsNoAddEncounterToVisitExtension() throws Exception {
4341
//given
44-
mockStatic(ModuleFactory.class);
45-
when(ModuleFactory.getExtensions("org.openmrs.module.web.extension.AddEncounterToVisitExtension")).thenReturn(null);
46-
47-
//when
48-
Set<Link> links = ExtensionUtil.getAllAddEncounterToVisitLinks();
49-
50-
//then
51-
assertNotNull(links);
52-
assertEquals(0, links.size());
42+
try (MockedStatic<ModuleFactory> mockedModuleFactory = mockStatic(ModuleFactory.class)) {
43+
mockedModuleFactory.when(() -> ModuleFactory.getExtensions("org.openmrs.module.web.extension.AddEncounterToVisitExtension"))
44+
.thenReturn(null);
45+
46+
//when
47+
Set<Link> links = ExtensionUtil.getAllAddEncounterToVisitLinks();
48+
49+
//then
50+
assertNotNull(links);
51+
assertEquals(0, links.size());
52+
}
5353
}
54-
54+
5555
/**
5656
* @see ExtensionUtil#getFormsModulesCanAddEncounterToVisit()
5757
* @verifies return forms if there are AddEncounterToVisitExtensions
5858
*/
5959
@Test
60-
public void getFormsModulesCanAddEncounterToVisit_shouldReturnFormsIfThereAreAddEncounterToVisitExtensions()
61-
throws Exception {
60+
public void getFormsModulesCanAddEncounterToVisit_shouldReturnFormsIfThereAreAddEncounterToVisitExtensions() throws Exception {
6261
//given
6362
AddEncounterToVisitExtension ext1 = mock(AddEncounterToVisitExtension.class);
6463
Set<Link> links1 = new HashSet<Link>();
@@ -69,28 +68,29 @@ public void getFormsModulesCanAddEncounterToVisit_shouldReturnFormsIfThereAreAdd
6968
link2.setLabel("b");
7069
links1.add(link2);
7170
when(ext1.getAddEncounterToVisitLinks()).thenReturn(links1);
72-
71+
7372
AddEncounterToVisitExtension ext2 = mock(AddEncounterToVisitExtension.class);
7473
Set<Link> links2 = new HashSet<Link>();
7574
Link link3 = new Link();
7675
link3.setLabel("aa");
7776
links2.add(link3);
7877
when(ext2.getAddEncounterToVisitLinks()).thenReturn(links2);
79-
80-
List<Extension> extensions = new ArrayList<Extension>();
78+
79+
List<Extension> extensions = new ArrayList<>();
8180
extensions.add(ext1);
8281
extensions.add(ext2);
83-
84-
mockStatic(ModuleFactory.class);
85-
when(ModuleFactory.getExtensions("org.openmrs.module.web.extension.AddEncounterToVisitExtension")).thenReturn(
86-
extensions);
87-
88-
//when
89-
Set<Link> allAddEncounterToVisitLinks = ExtensionUtil.getAllAddEncounterToVisitLinks();
90-
91-
//then
92-
assertTrue(allAddEncounterToVisitLinks.contains(link1));
93-
assertTrue(allAddEncounterToVisitLinks.contains(link2));
94-
assertTrue(allAddEncounterToVisitLinks.contains(link3));
82+
83+
try (MockedStatic<ModuleFactory> mockedModuleFactory = mockStatic(ModuleFactory.class)) {
84+
mockedModuleFactory.when(() -> ModuleFactory.getExtensions("org.openmrs.module.web.extension.AddEncounterToVisitExtension"))
85+
.thenReturn(extensions);
86+
87+
//when
88+
Set<Link> allAddEncounterToVisitLinks = ExtensionUtil.getAllAddEncounterToVisitLinks();
89+
90+
//then
91+
assertTrue(allAddEncounterToVisitLinks.contains(link1));
92+
assertTrue(allAddEncounterToVisitLinks.contains(link2));
93+
assertTrue(allAddEncounterToVisitLinks.contains(link3));
94+
}
9595
}
9696
}

omod/src/test/java/org/openmrs/web/attribute/handler/BaseMetadataFieldGenDatatypeHandlerTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@
1717
import static org.mockito.Mockito.when;
1818

1919
import org.junit.Test;
20-
import org.junit.runner.RunWith;
2120
import org.openmrs.Location;
2221
import org.openmrs.customdatatype.CustomDatatype;
2322
import org.openmrs.customdatatype.datatype.MockLocationDatatype;
24-
import org.powermock.modules.junit4.PowerMockRunner;
2523

26-
@RunWith(PowerMockRunner.class)
2724
public class BaseMetadataFieldGenDatatypeHandlerTest {
2825

2926
private BaseMetadataFieldGenDatatypeHandler handler = new MockLocationFieldGenDatatypeHandler();

omod/src/test/java/org/openmrs/web/attribute/handler/RegexValidatedTextDatatypeHandlerTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,20 @@
1919
import org.junit.Rule;
2020
import org.junit.Test;
2121
import org.junit.rules.ExpectedException;
22-
import org.junit.runner.RunWith;
2322
import org.openmrs.customdatatype.CustomDatatype;
2423
import org.openmrs.customdatatype.InvalidCustomValueException;
2524
import org.openmrs.customdatatype.datatype.RegexValidatedTextDatatype;
26-
import org.powermock.modules.junit4.PowerMockRunner;
2725
import org.springframework.mock.web.MockHttpServletRequest;
2826

2927
/**
3028
* Tests {@code RegexValidatedTextDatatypeHandler}.
3129
*/
32-
@RunWith(PowerMockRunner.class)
3330
public class RegexValidatedTextDatatypeHandlerTest {
3431

3532
@Rule
36-
ExpectedException expectedException = ExpectedException.none();
33+
public ExpectedException expectedException = ExpectedException.none();
3734

38-
private RegexValidatedTextDatatypeHandler handler = new RegexValidatedTextDatatypeHandler();
35+
private final RegexValidatedTextDatatypeHandler handler = new RegexValidatedTextDatatypeHandler();
3936

4037
/**
4138
* @see org.openmrs.web.attribute.handler.FieldGenDatatypeHandler#getValue(CustomDatatype,

0 commit comments

Comments
 (0)