Skip to content

Commit

Permalink
BXC-4290 replace deprecated MockitoAnnotations.initMocks (#1610)
Browse files Browse the repository at this point in the history
* replace deprecated MockitoAnnotations.initMocks with MockitoAnnotations.openMocks, removed unused imports

* add autocloseable instance variable and close mocks

* switch StoreAccessLevelFilterTest.class to this
  • Loading branch information
krwong authored Sep 12, 2023
1 parent f63ff64 commit 0b4f036
Show file tree
Hide file tree
Showing 122 changed files with 1,098 additions and 384 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import static edu.unc.lib.boxc.auth.api.AccessPrincipalConstants.PUBLIC_PRINC;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.mockito.MockitoAnnotations.openMocks;

import java.util.Calendar;

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.vocabulary.RDF;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -36,6 +37,7 @@ public class ContentObjectAccessRestrictionValidatorTest {
"http://example.com/rest/content/99/9a/01/a2/999a01a2-c836-499a-a72e-57f1039f4f45";

private ContentObjectAccessRestrictionValidator validator;
private AutoCloseable closeable;

@Mock
private PID pid;
Expand All @@ -45,7 +47,7 @@ public class ContentObjectAccessRestrictionValidatorTest {

@BeforeEach
public void init() {
initMocks(this);
closeable = openMocks(this);

validator = new ContentObjectAccessRestrictionValidator();
when(pid.getURI()).thenReturn(PID_URI);
Expand All @@ -54,6 +56,11 @@ public void init() {
resc = model.getResource(PID_URI);
}

@AfterEach
void closeService() throws Exception {
closeable.close();
}

@Test
public void workNoAclsTest() throws Exception {
model.add(resc, RDF.type, Cdr.Work);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.mockito.MockitoAnnotations.openMocks;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -30,15 +31,22 @@ public class GlobalPermissionEvaluatorTest {

private GlobalPermissionEvaluator evaluator;

private AutoCloseable closeable;

@BeforeEach
public void init() {
initMocks(this);
closeable = openMocks(this);

configProperties = new Properties();

principals = new HashSet<>(Arrays.asList(PRINC_GRP1));
}

@AfterEach
void closeService() throws Exception {
closeable.close();
}

@Test
public void hasGlobalPermissionTest() {
addGlobalAssignment(UserRole.administrator, PRINC_GRP1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.mockito.MockitoAnnotations.openMocks;

import java.time.ZonedDateTime;
import java.util.ArrayList;
Expand All @@ -33,6 +33,7 @@
import java.util.UUID;
import java.util.stream.Collectors;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
Expand All @@ -49,6 +50,8 @@ public class InheritedAclFactoryTest {
private static final String OWNER_PRINC = "owner";
private static final String PATRON_GROUP = PATRON_NAMESPACE + "special";

private AutoCloseable closeable;

@Mock
private ContentPathFactory pathFactory;

Expand All @@ -63,7 +66,7 @@ public class InheritedAclFactoryTest {

@BeforeEach
public void init() {
initMocks(this);
closeable = openMocks(this);

aclFactory = new InheritedAclFactory();
aclFactory.setObjectAclFactory(objectAclFactory);
Expand All @@ -77,6 +80,11 @@ public void init() {
pid = PIDs.get(UUID.randomUUID().toString());
}

@AfterEach
void closeService() throws Exception {
closeable.close();
}

@Test
public void unitBasePrincRoles() {
Map<String, Set<String>> princRoles = aclFactory.getPrincipalRoles(pid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.mockito.MockitoAnnotations.openMocks;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -29,6 +29,7 @@
import java.util.UUID;
import java.util.stream.Collectors;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -57,6 +58,8 @@ public class InheritedPermissionEvaluatorTest {
private static final Set<String> PATRON_GROUP_PRINCIPLES = new HashSet<>(
asList(PUBLIC_PRINC, PATRON_GROUP));

private AutoCloseable closeable;

@Mock
private ContentPathFactory pathFactory;

Expand All @@ -71,7 +74,7 @@ public class InheritedPermissionEvaluatorTest {

@BeforeEach
public void init() {
initMocks(this);
closeable = openMocks(this);

evaluator = new InheritedPermissionEvaluator();
evaluator.setPathFactory(pathFactory);
Expand All @@ -85,6 +88,11 @@ public void init() {
pid = PIDs.get(UUID.randomUUID().toString());
}

@AfterEach
void closeService() throws Exception {
closeable.close();
}

@Test
public void unitHasPatronPermissionTest() {
assertTrue(evaluator.hasPermission(pid, PATRON_PRINCIPLES, Permission.viewMetadata));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.mockito.MockitoAnnotations.openMocks;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
Expand All @@ -29,6 +29,7 @@
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.vocabulary.RDF;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -59,6 +60,7 @@ public class ObjectACLFactoryTest {
private static final LocalDateTime TOMORROW = LocalDateTime.now().plusDays(1);

private ObjectAclFactory aclFactory;
private AutoCloseable closeable;

@Mock
private RepositoryObjectLoader repositoryObjectLoader;
Expand All @@ -71,7 +73,7 @@ public class ObjectACLFactoryTest {

@BeforeEach
public void init() {
initMocks(this);
closeable = openMocks(this);

aclFactory = new ObjectAclFactory();
aclFactory.setRepositoryObjectLoader(repositoryObjectLoader);
Expand All @@ -87,6 +89,11 @@ public void init() {
when(repositoryObjectLoader.getRepositoryObject(pid)).thenReturn(repoObj);
}

@AfterEach
void closeService() throws Exception {
closeable.close();
}

@Test
public void getPrincipalRolesTest() throws Exception {
objResc.addLiteral(CdrAcl.canManage, MANAGE_GRP);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.mockito.MockitoAnnotations.openMocks;

import java.io.File;
import java.nio.file.Files;
Expand Down Expand Up @@ -52,6 +52,8 @@ public class AbstractDepositJobTest {
protected static final String FEDORA_BASE = "http://localhost:48085/rest/";
protected static final String TX_URI = "http://localhost:48085/rest/tx:99b58d30-06f5-477b-a44c-d614a9049d38";

private AutoCloseable closeable;

@Mock
protected RepositoryObjectDriver driver;
@Mock
Expand Down Expand Up @@ -94,7 +96,7 @@ public class AbstractDepositJobTest {

@BeforeEach
public void initBase() throws Exception {
initMocks(this);
closeable = openMocks(this);

TestHelper.setContentBase(FEDORA_BASE);

Expand Down Expand Up @@ -144,7 +146,8 @@ public Void answer(InvocationOnMock invocation) throws Throwable {
}

@AfterEach
public void tearDown() {
public void tearDown() throws Exception {
closeable.close();
depositModelManager.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.mockito.MockitoAnnotations.openMocks;

import java.net.URI;
import java.util.ArrayList;
Expand All @@ -19,11 +19,11 @@
import org.fcrepo.client.FcrepoOperationFailedException;
import org.fcrepo.client.FcrepoResponse;
import org.fcrepo.client.HeadBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

import edu.unc.lib.boxc.deposit.validate.VerifyObjectsAreInFedoraService;
import edu.unc.lib.boxc.model.api.ids.PID;
import edu.unc.lib.boxc.model.fcrepo.ids.PIDs;

Expand All @@ -38,6 +38,7 @@ public class VerifyObjectsAreInFedoraServiceTest {
private PID workPid;
private PID filePid;
private Collection<String> pids;
private AutoCloseable closeable;
@Mock
private FcrepoClient client;
@Mock
Expand All @@ -52,7 +53,7 @@ public class VerifyObjectsAreInFedoraServiceTest {

@BeforeEach
public void init() throws Exception {
initMocks(this);
closeable = openMocks(this);
verificationService = new VerifyObjectsAreInFedoraService();
setField(verificationService, "fcrepoClient", client);

Expand All @@ -61,6 +62,11 @@ public void init() throws Exception {
pids = new HashSet<>();
}

@AfterEach
void closeService() throws Exception {
closeable.close();
}

@Test
public void allObjectsInFedoraTest() throws FcrepoOperationFailedException {
when(client.head(any(URI.class))).thenReturn(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import edu.unc.lib.boxc.deposit.api.RedisWorkerConstants.JobField;
import edu.unc.lib.boxc.model.api.exceptions.RepositoryException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -19,12 +20,13 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.mockito.MockitoAnnotations.openMocks;

/**
* @author bbpennel
*/
public class JobStatusFactoryTest {
private AutoCloseable closeable;

@Mock
private JedisPool jedisPool;
Expand All @@ -37,7 +39,7 @@ public class JobStatusFactoryTest {

@BeforeEach
public void setup() {
initMocks(this);
closeable = openMocks(this);

when(jedisPool.getResource()).thenReturn(jedis);

Expand All @@ -49,6 +51,11 @@ public void setup() {
statusFactory.setJedisPool(jedisPool);
}

@AfterEach
void closeService() throws Exception {
closeable.close();
}

@Test
public void incrCompletionSuccess() {
statusFactory.incrCompletion(jobUUID, 1);
Expand Down
Loading

0 comments on commit 0b4f036

Please sign in to comment.