Skip to content

Commit

Permalink
Merge pull request #1104 from DenverCoder544/junit_5_upgrade
Browse files Browse the repository at this point in the history
Junit 5 upgrade
  • Loading branch information
ZakarFin authored Jan 21, 2025
2 parents 95fe342 + 4b19c5f commit 22f0ea1
Show file tree
Hide file tree
Showing 195 changed files with 3,252 additions and 3,367 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
package org.oskari.helpers;

import org.json.JSONObject;
import org.junit.Test;

import static org.junit.Assert.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class AppSetupHelperTest {

@Test
public void readViewFile() throws Exception {
// direct from root as the new way with no expectation
assertEquals("root", AppSetupHelper.readViewFile("/test.json").optString("name"));
assertEquals("apps", AppSetupHelper.readViewFile("/json/apps/custom/appsetup.json").optString("name"));
Assertions.assertEquals("root", AppSetupHelper.readViewFile("/test.json").optString("name"));
Assertions.assertEquals("apps", AppSetupHelper.readViewFile("/json/apps/custom/appsetup.json").optString("name"));

// new "assumed path" /json/apps (should return the same custom apps as direct path to /json/apps)
JSONObject apps = AppSetupHelper.readViewFile("custom/appsetup.json");
assertEquals("apps", apps.optString("name"));
Assertions.assertEquals("apps", apps.optString("name"));

// for older "assumed path" /json/views
JSONObject views = AppSetupHelper.readViewFile("appsetup.json");
assertEquals("views", views.optString("name"));
Assertions.assertEquals("views", views.optString("name"));

// Because the search path only has test.json at root
assertEquals("root", AppSetupHelper.readViewFile("test.json").optString("name"));
Assertions.assertEquals("root", AppSetupHelper.readViewFile("test.json").optString("name"));

}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package org.oskari.helpers;

import org.json.JSONObject;
import org.junit.Test;

import static org.junit.Assert.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class LayerHelperTest {

@Test
public void readLayerFile() throws Exception {
// direct from root as the new way with no expectation
assertEquals("layers", readFileAsJSON("/json/layers/layer.json").optString("name"));
assertEquals("root", readFileAsJSON("/test.json").optString("name"));
Assertions.assertEquals("layers", readFileAsJSON("/json/layers/layer.json").optString("name"));
Assertions.assertEquals("root", readFileAsJSON("/test.json").optString("name"));

// for older "assumed path" /json/layers
JSONObject layer = readFileAsJSON("layer.json");
assertEquals("layers", layer.optString("name"));
Assertions.assertEquals("layers", layer.optString("name"));
}

private JSONObject readFileAsJSON(String file) throws Exception {
Expand Down
24 changes: 16 additions & 8 deletions control-admin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,6 @@
<artifactId>metrics-jvm</artifactId>
</dependency>


<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.oskari</groupId>
<artifactId>shared-test-resources</artifactId>
Expand All @@ -111,17 +104,32 @@
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>


<!-- PowerMock can be used to mock static/private methods -->
<!--
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<scope>test</scope>
</dependency>
-->
<!--
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
-->
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,48 @@
import fi.nls.test.control.JSONActionRouteTest;
import org.json.JSONException;
import org.json.JSONObject;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.oskari.permissions.PermissionService;
import org.powermock.api.mockito.PowerMockito;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import static org.mockito.Mockito.*;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public abstract class AbstractLayerAdminHandlerTest extends JSONActionRouteTest {

protected final static Integer WMS_LAYER_ID = 1;
protected final static Integer SECOND_WMS_LAYER_ID = 2;
protected final static Integer THIRD_WMS_LAYER_ID = 3;
protected final static Integer METADATA_LAYER_ID = 10;
private static MockedStatic<OskariComponentManager> oskariComponentManagerMockedStatic;

protected static void setupMocks() throws Exception {
PropertyUtil.addProperty("oskari.user.service", DummyUserService.class.getCanonicalName(), true);
PermissionService permissionService = mock(PermissionService.class);
OskariLayerService mapLayerService = mock(OskariLayerService.class);
PowerMockito.mockStatic(OskariComponentManager.class);
// org.mockito.exceptions.base.MockitoException:
// For fi.nls.oskari.service.OskariComponentManager, static mocking is already registered in the current thread
oskariComponentManagerMockedStatic = Mockito.mockStatic(OskariComponentManager.class);
when(OskariComponentManager.getComponentOfType(PermissionService.class)).thenReturn(permissionService);
when(OskariComponentManager.getComponentOfType(OskariLayerService.class)).thenReturn(mapLayerService);
doReturn(getTestLayers()).when(mapLayerService).findAll();
doNothing().when(mapLayerService).update(any());
doReturn(new HashSet<String>()).when(permissionService).getAdditionalPermissions();
doAnswer(invocation -> invocation.getArgument(0)).when(permissionService).getPermissionName(anyString(),anyString());
Mockito.lenient().doReturn(getTestLayers()).when(mapLayerService).findAll();
Mockito.lenient().doNothing().when(mapLayerService).update(any());
Mockito.lenient().doReturn(new HashSet<String>()).when(permissionService).getAdditionalPermissions();
Mockito.lenient().doAnswer(invocation -> invocation.getArgument(0)).when(permissionService).getPermissionName(anyString(),anyString());

}

protected static void tearDownMocks() {
if (oskariComponentManagerMockedStatic != null) {
oskariComponentManagerMockedStatic.close();
}
OskariComponentManager.teardown();;
PropertyUtil.clearProperties();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
package fi.nls.oskari.control.admin;

import fi.nls.oskari.domain.map.OskariLayer;
import fi.nls.oskari.service.OskariComponentManager;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.*;
import java.util.List;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertNull;

@RunWith(PowerMockRunner.class)
@PrepareForTest({OskariComponentManager.class})
@ExtendWith(MockitoExtension.class)
public class LayerAdminHandlerTest extends AbstractLayerAdminHandlerTest {

private static final LayerAdminHandler handler = new LayerAdminHandler();

@BeforeClass
public static void setup() throws Exception {
@BeforeEach
public void setup() throws Exception {
setupMocks();
handler.init();
}

@AfterClass
public static void tearDown() {
@AfterEach
public void tearDown() {
tearDownMocks();
}

@Test
// For fi.nls.oskari.service.OskariComponentManager, static mocking is already registered in the current thread
public void testCleanupLayerReferences() throws Exception {
List<OskariLayer> layers = handler.cleanupLayerReferences(METADATA_LAYER_ID);
for (OskariLayer layer : layers) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package fi.nls.oskari.control.admin;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import fi.nls.oskari.domain.map.OskariLayer;
Expand All @@ -18,7 +16,7 @@ public void testNullOptions() {

OskariLayer mock = Mockito.mock(OskariLayer.class);

assertFalse(LayerAdminHelper.isReferencedByTimeseriesMetadata(layerId, mock));
Assertions.assertFalse(LayerAdminHelper.isReferencedByTimeseriesMetadata(layerId, mock));
}

@Test
Expand All @@ -30,7 +28,7 @@ public void testEmptyOptions() {
OskariLayer mock = Mockito.mock(OskariLayer.class);
Mockito.when(mock.getOptions()).thenReturn(options);

assertFalse(LayerAdminHelper.isReferencedByTimeseriesMetadata(layerId, mock));
Assertions.assertFalse(LayerAdminHelper.isReferencedByTimeseriesMetadata(layerId, mock));
}

@Test
Expand All @@ -52,7 +50,7 @@ public void testLayerIsEmptyString() throws JSONException {
OskariLayer mock = Mockito.mock(OskariLayer.class);
Mockito.when(mock.getOptions()).thenReturn(options);

assertFalse(LayerAdminHelper.isReferencedByTimeseriesMetadata(layerId, mock));
Assertions.assertFalse(LayerAdminHelper.isReferencedByTimeseriesMetadata(layerId, mock));
}

@Test
Expand All @@ -74,7 +72,7 @@ public void testValidOptionsButDifferentLayerId() throws JSONException {
OskariLayer mock = Mockito.mock(OskariLayer.class);
Mockito.when(mock.getOptions()).thenReturn(options);

assertFalse(LayerAdminHelper.isReferencedByTimeseriesMetadata(layerId, mock));
Assertions.assertFalse(LayerAdminHelper.isReferencedByTimeseriesMetadata(layerId, mock));
}

@Test
Expand All @@ -96,7 +94,7 @@ public void testHappyCase() throws JSONException {
OskariLayer mock = Mockito.mock(OskariLayer.class);
Mockito.when(mock.getOptions()).thenReturn(options);

assertTrue(LayerAdminHelper.isReferencedByTimeseriesMetadata(layerId, mock));
Assertions.assertTrue(LayerAdminHelper.isReferencedByTimeseriesMetadata(layerId, mock));
}

}
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@
package fi.nls.oskari.control.admin;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;

import java.util.HashSet;

import fi.nls.oskari.service.DummyUserService;
import fi.nls.oskari.util.PropertyUtil;
import fi.nls.test.util.TestHelper;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.oskari.permissions.PermissionService;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import fi.nls.oskari.control.ActionDeniedException;
import fi.nls.oskari.control.ActionParameters;
import fi.nls.oskari.domain.Role;
import fi.nls.oskari.service.DummyUserService;
import fi.nls.oskari.service.OskariComponentManager;
import fi.nls.oskari.service.ServiceException;
import fi.nls.oskari.service.UserService;
import fi.nls.oskari.util.PropertyUtil;
import fi.nls.test.control.JSONActionRouteTest;
import fi.nls.test.util.ResourceHelper;
import fi.nls.test.util.TestHelper;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.oskari.permissions.PermissionService;

import javax.sql.DataSource;
import java.util.HashSet;

@RunWith(PowerMockRunner.class)
@PrepareForTest({OskariComponentManager.class})
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public class LayerAdminMetadataHandlerTest extends JSONActionRouteTest {

final private static LayerAdminMetadataHandler handler = new LayerAdminMetadataHandler();

@BeforeClass
private static MockedStatic<OskariComponentManager> oskariComponentManagerMockedStatic;
@BeforeAll
public static void setup() throws Exception {
PropertyUtil.addProperty("oskari.user.service", DummyUserService.class.getCanonicalName(), true);
// So ViewsHandler doesn't try to connect to db
Expand All @@ -44,15 +40,18 @@ public static void setup() throws Exception {
TestHelper.registerTestDataSource();
handler.init();
}
@AfterClass
@AfterAll
public static void tearDown() {
PropertyUtil.clearProperties();
TestHelper.teardown();
if (oskariComponentManagerMockedStatic != null) {
oskariComponentManagerMockedStatic.close();
}
}

private static void setupPermissionsServiceMock() {
PermissionService permissionService = mock(PermissionService.class);
PowerMockito.mockStatic(OskariComponentManager.class);
oskariComponentManagerMockedStatic = Mockito.mockStatic(OskariComponentManager.class);
when(OskariComponentManager.getComponentOfType(PermissionService.class)).thenReturn(permissionService);

doReturn(new HashSet<String>()).when(permissionService).getAdditionalPermissions();
Expand All @@ -67,15 +66,19 @@ public void testGetWithAdmin() throws Exception {
}


@Test(expected = ActionDeniedException.class)
@Test()
public void testGetWithGuest() throws Exception {
final ActionParameters params = createActionParams(getGuestUser());
handler.handleAction(params);
assertThrows(ActionDeniedException.class, () -> {
final ActionParameters params = createActionParams(getGuestUser());
handler.handleAction(params);
});
}

@Test(expected = ActionDeniedException.class)
@Test()
public void testGetWithLoggedInNonAdminUser() throws Exception {
final ActionParameters params = createActionParams(getLoggedInUser());
handler.handleAction(params);
assertThrows(ActionDeniedException.class, () -> {
final ActionParameters params = createActionParams(getLoggedInUser());
handler.handleAction(params);
});
}
}
Loading

0 comments on commit 22f0ea1

Please sign in to comment.