Skip to content

Commit

Permalink
fix(API.PID.reconcile): Fix the test to not mock that static class
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes-darms committed Jun 4, 2024
1 parent 24d24e4 commit 3984c67
Showing 1 changed file with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ public class ReconcilePIDCommandTest {
IndexServiceBean indexServiceBean;
RoleAssigneeServiceBean roleAssigneeServiceBean;
UserNotificationServiceBean notificationService;
static MockedStatic<PidUtil> pidutil;
@BeforeAll
public static void pre(){
pidutil = mockStatic(PidUtil.class);
}


@BeforeEach
public void setUp() {
Expand Down Expand Up @@ -105,13 +101,14 @@ public void testSucessfullChangeWithoutFilePIDs() throws Exception {
when(pidProvider.getShoulder()).thenReturn("PROTO");
when(pidProvider.getSeparator()).thenReturn("PROTO");
when(pidProvider.getLabel()).thenReturn("PROTO");
when(pidProvider.getId()).thenReturn("PROTO");
when(pidProvider.registerWhenPublished()).thenReturn(false);

// register the provider
pidutil.when(() -> PidUtil.getPidProvider(eq(pidProvider.getLabel()))).thenReturn(pidProvider);
PidUtil.addToProviderList(pidProvider);

GlobalId pid = new GlobalId(pidProvider.getProtocol(),pidProvider.getAuthority(),"OLD-ID", pidProvider.getSeparator(),pidProvider.getUrlPrefix(), pidProvider.getLabel());
pidutil.when(() -> PidUtil.parseAsGlobalID(eq("PROTO"),eq("PROTO"),eq("OLD-ID"))).thenReturn(pid);

when(pidProvider.parsePersistentId(eq("PROTO"),eq("PROTO"),eq("OLD-ID"))).thenReturn(pid);
// given a dataset linked with the PIDprovider
Dataset ds = MocksFactory.makeDataset();
ds.setGlobalId(pid);
Expand All @@ -129,11 +126,17 @@ public void testSucessfullChangeWithoutFilePIDs() throws Exception {
when(newPIDProviderMock.getAuthority()).thenReturn("PROTO1");
when(newPIDProviderMock.getShoulder()).thenReturn("PROTO1");
when(newPIDProviderMock.getSeparator()).thenReturn("PROTO1");
when(newPIDProviderMock.getLabel()).thenReturn("PROTO1");
when(newPIDProviderMock.getId()).thenReturn("PROTO1");
when(newPIDProviderMock.registerWhenPublished()).thenReturn(false);
when(newPIDProviderMock.canManagePID()).thenReturn(true);
// register the provider
PidUtil.addToProviderList(newPIDProviderMock);
// create new GloablID and register with the Utils.
GlobalId pid2 = new GlobalId(newPIDProviderMock.getProtocol(), newPIDProviderMock.getAuthority(),"MYIDENT", newPIDProviderMock.getSeparator(), newPIDProviderMock.getUrlPrefix(), newPIDProviderMock.getLabel());
pidutil.when(() -> PidUtil.parseAsGlobalID(eq("PROTO1"),eq("PROTO1"),eq("MYIDENT"))).thenReturn(pid2);
when(newPIDProviderMock.parsePersistentId(eq("PROTO1"),eq("PROTO1"),eq("MYIDENT"))).thenReturn(pid2);


when(newPIDProviderMock.generatePid(ds)).thenAnswer(new Answer<DvObject>() {
public DvObject answer(InvocationOnMock invocation) {
Dataset callback = (Dataset) invocation.getArguments()[0];
Expand All @@ -143,8 +146,7 @@ public DvObject answer(InvocationOnMock invocation) {
return callback;
}
});
// register the provider
pidutil.when(() -> PidUtil.getPidProvider(eq(newPIDProviderMock.getLabel()))).thenReturn(newPIDProviderMock);


// mocks and stubs for notification
RoleAssignment roleAssignment=new RoleAssignment();
Expand Down Expand Up @@ -196,12 +198,13 @@ public void testSucessfullChangeWithFilePIDs() throws Exception {
when(pidProvider.getShoulder()).thenReturn("PROTO");
when(pidProvider.getSeparator()).thenReturn("PROTO");
when(pidProvider.getLabel()).thenReturn("PROTO");
when(pidProvider.getId()).thenReturn("PROTO");
when(pidProvider.registerWhenPublished()).thenReturn(false);
// register the provider
pidutil.when(() -> PidUtil.getPidProvider(eq(pidProvider.getLabel()))).thenReturn(pidProvider);
PidUtil.addToProviderList(pidProvider);

GlobalId pid = new GlobalId(pidProvider.getProtocol(),pidProvider.getAuthority(),"OLD-ID", pidProvider.getSeparator(),pidProvider.getUrlPrefix(), pidProvider.getLabel());
pidutil.when(() -> PidUtil.parseAsGlobalID(eq("PROTO"),eq("PROTO"),eq("OLD-ID"))).thenReturn(pid);
when(pidProvider.parsePersistentId(eq("PROTO"),eq("PROTO"),eq("OLD-ID"))).thenReturn(pid);

// given a dataset linked with the PIDprovider
Dataset ds = MocksFactory.makeDataset();
Expand All @@ -212,7 +215,7 @@ public void testSucessfullChangeWithFilePIDs() throws Exception {
ds.setFiles(ds.getFiles().subList(0,1));
DataFile df = ds.getFiles().get(0);
GlobalId piddf = new GlobalId(pidProvider.getProtocol(),pidProvider.getAuthority(),"OLD-DF-ID", pidProvider.getSeparator(),pidProvider.getUrlPrefix(), pidProvider.getLabel());
pidutil.when(() -> PidUtil.parseAsGlobalID(eq("PROTO"),eq("PROTO"),eq("OLD-DF-ID"))).thenReturn(piddf);
when(pidProvider.parsePersistentId(eq("PROTO"),eq("PROTO"),eq("OLD-DF-ID"))).thenReturn(piddf);
df.setGlobalId(piddf);
when(pidProvider.alreadyRegistered(df)).thenReturn(true).thenReturn(false);
///
Expand All @@ -223,15 +226,19 @@ public void testSucessfullChangeWithFilePIDs() throws Exception {

// given a new PID Provider
PidProvider newPIDProviderMock = mock(PidProvider.class);
when(newPIDProviderMock.getId()).thenReturn("PROTO1");
when(newPIDProviderMock.getLabel()).thenReturn("PROTO1");
when(newPIDProviderMock.getProtocol()).thenReturn("PROTO1");
when(newPIDProviderMock.getAuthority()).thenReturn("PROTO1");
when(newPIDProviderMock.getShoulder()).thenReturn("PROTO1");
when(newPIDProviderMock.getSeparator()).thenReturn("PROTO1");
when(newPIDProviderMock.registerWhenPublished()).thenReturn(false);
when(newPIDProviderMock.canManagePID()).thenReturn(true);
// register the provider
PidUtil.addToProviderList(newPIDProviderMock);
// create new GloablID and register with the Utils.
GlobalId pid2 = new GlobalId(newPIDProviderMock.getProtocol(), newPIDProviderMock.getAuthority(),"MYIDENT", newPIDProviderMock.getSeparator(), newPIDProviderMock.getUrlPrefix(), newPIDProviderMock.getLabel());
pidutil.when(() -> PidUtil.parseAsGlobalID(eq("PROTO1"),eq("PROTO1"),eq("MYIDENT"))).thenReturn(pid2);
when(newPIDProviderMock.parsePersistentId(eq("PROTO1"),eq("PROTO1"),eq("MYIDENT"))).thenReturn(pid2);
when(newPIDProviderMock.generatePid(ds)).thenAnswer(new Answer<DvObject>() {
public DvObject answer(InvocationOnMock invocation) {
Dataset callback = (Dataset) invocation.getArguments()[0];
Expand All @@ -241,8 +248,8 @@ public DvObject answer(InvocationOnMock invocation) {
return callback;
}
});
GlobalId piddf2 = new GlobalId(pidProvider.getProtocol(),pidProvider.getAuthority(),"NEW-DF-ID", pidProvider.getSeparator(),pidProvider.getUrlPrefix(), pidProvider.getLabel());
pidutil.when(() -> PidUtil.parseAsGlobalID(eq(piddf2.getProtocol()),eq(piddf2.getAuthority()),eq(piddf2.getIdentifier()))).thenReturn(piddf2);
GlobalId piddf2 = new GlobalId(newPIDProviderMock.getProtocol(),newPIDProviderMock.getAuthority(),"NEW-DF-ID", newPIDProviderMock.getSeparator(),newPIDProviderMock.getUrlPrefix(), newPIDProviderMock.getLabel());
when(newPIDProviderMock.parsePersistentId(eq("PROTO1"),eq("PROTO1"),eq("NEW-DF-ID"))).thenReturn(piddf2);
when(newPIDProviderMock.generatePid(df)).thenAnswer(new Answer<DvObject>() {
public DvObject answer(InvocationOnMock invocation) {
DataFile callback = (DataFile) invocation.getArguments()[0];
Expand All @@ -253,8 +260,7 @@ public DvObject answer(InvocationOnMock invocation) {
}
});

// register the provider
pidutil.when(() -> PidUtil.getPidProvider(eq(newPIDProviderMock.getLabel()))).thenReturn(newPIDProviderMock);


// mocks and stubs for notification
RoleAssignment roleAssignment=new RoleAssignment();
Expand Down Expand Up @@ -313,7 +319,11 @@ public void testGuardNoOp() throws CommandException {
when(pidProvider.getAuthority()).thenReturn("PROTO");
when(pidProvider.getShoulder()).thenReturn("PROTO");
when(pidProvider.getSeparator()).thenReturn("PROTO");
when(pidProvider.getLabel()).thenReturn("PROTO");
when(pidProvider.getId()).thenReturn("PROTO");
when(pidProvider.registerWhenPublished()).thenReturn(true);
// register the provider
PidUtil.addToProviderList(pidProvider);

// given a dataset linked with a PidProvider and corresponding globalId
Dataset ds = MocksFactory.makeDataset();
Expand All @@ -322,9 +332,8 @@ public void testGuardNoOp() throws CommandException {
Dataverse dv = MocksFactory.makeDataverse();
ds.setOwner(dv);
GlobalId pid = new GlobalId(pidProvider.getProtocol(),pidProvider.getAuthority(),"OLD-ID", pidProvider.getSeparator(),pidProvider.getUrlPrefix(), pidProvider.getLabel());
pidutil.when(() -> PidUtil.parseAsGlobalID(eq("PROTO"),eq("PROTO"),eq("OLD-ID"))).thenReturn(pid);
when(pidProvider.parsePersistentId(eq("PROTO"),eq("PROTO"),eq("OLD-ID"))).thenReturn(pid);
ds.setGlobalId(pid);
pidutil.when(() -> PidUtil.getPidProvider(eq(pidProvider.getLabel()))).thenReturn(pidProvider);

ReconcileDatasetPidCommand sut = new ReconcileDatasetPidCommand(request, ds, pidProvider);
assertThrows(IllegalCommandException.class, () -> engine.submit(sut),"Should throw ICE when no change");
Expand All @@ -342,6 +351,7 @@ public void testGuardAlreadyPublished() throws CommandException {
when(pidProvider.getAuthority()).thenReturn("PROTO");
when(pidProvider.getShoulder()).thenReturn("PROTO");
when(pidProvider.getSeparator()).thenReturn("PROTO");
when(pidProvider.getLabel()).thenReturn("PROTO");
when(pidProvider.registerWhenPublished()).thenReturn(true);

// given a dataset linked with the PIDprovider
Expand Down

0 comments on commit 3984c67

Please sign in to comment.