Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug Fix: New AWS-WAF Rule Breaks EZID-Related Tests #1625 #1626

Merged
merged 1 commit into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.1.0</version>
<artifactId>mockito-inline</artifactId>
<version>3.12.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
Expand Down
45 changes: 35 additions & 10 deletions test/edu/ucsb/nceas/metacat/admin/upgrade/UpdateDOITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,18 @@
import edu.ucsb.nceas.metacat.properties.PropertyService;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.junit.After;
import org.junit.Before;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.argThat;

public class UpdateDOITest extends D1NodeServiceTest {
private static final String UPDATETIMEKEY = "_updated";

private MockedStatic<PropertyService> mockProperties;

/**
* Constructor
* @param name
Expand All @@ -71,7 +79,25 @@ public void initialize() {
assertTrue(1 == 1);

}


@Before
public void setUp() throws Exception {
super.setUp();

mockProperties = Mockito.mockStatic(PropertyService.class);
mockProperties.when(() -> PropertyService.getProperty(
eq("server.name"))).thenReturn("UpdateDOITestMock.edu");
mockProperties.when(() -> PropertyService.getProperty(
argThat((String s) -> !s.equals("server.name")))).thenCallRealMethod();
}

@After
public void tearDown() {
mockProperties.close();
super.tearDown();
}


/*
* Test the update of a pid and sid
*/
Expand All @@ -80,7 +106,7 @@ public void testUpdate() throws Exception {
String ezidUsername = PropertyService.getProperty("guid.doi.username");
String ezidPassword = PropertyService.getProperty("guid.doi.password");
String ezidServiceBaseUrl = PropertyService.getProperty("guid.doi.baseurl");
Session session = getTestSession();
Session session = getTestSession();
String emlFile = "test/eml-multiple-creators.xml";
InputStream content = null;
//Test the case that the identifier is a doi but no sid.
Expand Down Expand Up @@ -132,7 +158,7 @@ public void testUpdate() throws Exception {
IOUtils.closeQuietly(content);
}

//Test the case that the identifier is non-doi but the sid is an doi
//Test the case that the identifier is non-doi but the sid is an doi
try {
Identifier guid = new Identifier();
guid.setValue("tesCreateDOIinSid." + System.currentTimeMillis());
Expand All @@ -157,7 +183,7 @@ public void testUpdate() throws Exception {
Thread.sleep(2000);
count++;
} while ((metadata == null || metadata.get(EzidDOIService.DATACITE) == null) && count < 30);

assertNotNull(metadata);
String result = metadata.get(EzidDOIService.DATACITE);
//System.out.println("the result is \n"+result);
Expand All @@ -179,7 +205,7 @@ public void testUpdate() throws Exception {
} finally {
IOUtils.closeQuietly(content);
}

//update the datacite metadata by ids.
Vector<String> ids = new Vector<String>();
ids.add(publishedSIDStr);
Expand All @@ -200,22 +226,21 @@ public void testUpdate() throws Exception {
pidUpdate1 = (new Long(updateTime)).longValue();
Thread.sleep(2000);
count++;
} while (pidUpdate1 == PIDUpdateTime && count < 30);
} while (pidUpdate1 == PIDUpdateTime && count < 30);
assertNotNull(metadata);
updateTime = metadata.get(UPDATETIMEKEY);
pidUpdate1 = (new Long(updateTime)).longValue();
assertTrue(pidUpdate1 > PIDUpdateTime);

long sidUpdate1 = -1;
do {
metadata = ezid.getMetadata(publishedSIDStr);
updateTime = metadata.get(UPDATETIMEKEY);
sidUpdate1 = (new Long(updateTime)).longValue();
Thread.sleep(2000);
count++;
} while ((sidUpdate1 == SIDUpdateTime) && count < 30);
} while ((sidUpdate1 == SIDUpdateTime) && count < 30);
assertNotNull(metadata);
assertTrue(sidUpdate1 > SIDUpdateTime);
}

}
30 changes: 23 additions & 7 deletions test/edu/ucsb/nceas/metacat/doi/ezid/RegisterDOITest.java
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thank you, Matthew!

Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@

import org.apache.commons.io.IOUtils;
import org.dataone.client.v2.formats.ObjectFormatCache;
import org.dataone.configuration.Settings;
import org.dataone.ore.ResourceMapFactory;
import org.dataone.service.exceptions.InvalidRequest;
import org.dataone.service.exceptions.NotAuthorized;
Expand All @@ -67,12 +66,16 @@
import edu.ucsb.nceas.metacat.dataone.D1NodeServiceTest;
import edu.ucsb.nceas.metacat.dataone.MNodeReplicationTest;
import edu.ucsb.nceas.metacat.dataone.MNodeService;
import edu.ucsb.nceas.metacat.dataone.MockCNode;
import edu.ucsb.nceas.metacat.doi.DOIService;
import edu.ucsb.nceas.metacat.doi.DOIServiceFactory;
import edu.ucsb.nceas.metacat.doi.datacite.EML2DataCiteFactoryTest;
import edu.ucsb.nceas.metacat.doi.ezid.EzidDOIService;
import edu.ucsb.nceas.metacat.properties.PropertyService;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;

/**
* A JUnit test to exercise the DOI registration for content added
Expand All @@ -94,6 +97,8 @@ public class RegisterDOITest extends D1NodeServiceTest {
private String ezidServiceBaseUrl = null;
private EZIDService ezid = null;
private DOIService doiService = null;
private MockedStatic<PropertyService> mockProperties;

/**
* Set up the test fixtures
*
Expand All @@ -109,13 +114,24 @@ public void setUp() throws Exception {
ezid = new EZIDService(ezidServiceBaseUrl);
doiService = DOIServiceFactory.getDOIService();
serverName = PropertyService.getProperty("server.name");
// serverName = PropertyService.getProperty("server.name");
serverName = "RegisterDOITest.edu";
mockProperties = Mockito.mockStatic(PropertyService.class);
mockProperties.when(() -> PropertyService.getProperty(
eq("server.name"))).thenReturn(serverName);
mockProperties.when(() -> PropertyService.getProperty(
argThat((String s) -> !s.equals("server.name")))).thenCallRealMethod();
mockProperties.when(() -> PropertyService.setPropertyNoPersist(
anyString(), anyString())).thenCallRealMethod();
}

/**
* Remove the test fixtures
*/
@After
public void tearDown() {
mockProperties.close();
super.tearDown();
}

/**
Expand Down Expand Up @@ -985,7 +1001,7 @@ public void testPublishEML220() throws Exception {
public void testPublishIdentifierProcessWithAutoPublishOn() throws Exception {
printTestHeader("testPublishIdentifierProcessWithAutoPublishOn");
try {
PropertyService.getInstance().setPropertyNoPersist("guid.doi.autoPublish", "true");
PropertyService.setPropertyNoPersist("guid.doi.autoPublish", "true");
doiService.refreshStatus();
ezid.login(ezidUsername, ezidPassword);
// Mint a DOI
Expand Down Expand Up @@ -1090,7 +1106,7 @@ public void testPublishIdentifierProcessWithAutoPublishOn() throws Exception {
public void testPublishIdentifierProcessWithAutoPublishOff() throws Exception {
printTestHeader("testPublishIdentifierProcessWithAutoPublishOff");
try {
PropertyService.getInstance().setPropertyNoPersist("guid.doi.autoPublish", "false");
PropertyService.setPropertyNoPersist("guid.doi.autoPublish", "false");
doiService.refreshStatus();
ezid.login(ezidUsername, ezidPassword);
// Mint a DOI
Expand Down Expand Up @@ -1205,7 +1221,7 @@ public void testPublishPrivatePackageToPublic() throws Exception {
Session publicSession = new Session();
publicSession.setSubject(publicSub);

PropertyService.getInstance().setPropertyNoPersist("guid.doi.enforcePublicReadableEntirePackage", "true");
PropertyService.setPropertyNoPersist("guid.doi.enforcePublicReadableEntirePackage", "true");
boolean enforcePublicEntirePackageInPublish = new Boolean(PropertyService.getProperty("guid.doi.enforcePublicReadableEntirePackage"));
MNodeService.setEnforcePublisEntirePackage(enforcePublicEntirePackageInPublish);

Expand Down Expand Up @@ -1355,7 +1371,7 @@ public void testPublishPrivatePackageToPartialPublic() throws Exception {
Session publicSession = new Session();
publicSession.setSubject(publicSub);

PropertyService.getInstance().setPropertyNoPersist("guid.doi.enforcePublicReadableEntirePackage", "false");
PropertyService.setPropertyNoPersist("guid.doi.enforcePublicReadableEntirePackage", "false");
boolean enforcePublicEntirePackageInPublish = new Boolean(PropertyService.getProperty("guid.doi.enforcePublicReadableEntirePackage"));
MNodeService.setEnforcePublisEntirePackage(enforcePublicEntirePackageInPublish);

Expand Down