Skip to content

Commit

Permalink
upgrade Mockito libraries, change the way to initialize/close staticMock
Browse files Browse the repository at this point in the history
  • Loading branch information
Yen, David (398B-Affiliate) committed Apr 20, 2024
1 parent ea8a8d0 commit ad7d4dd
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 67 deletions.
16 changes: 8 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.12.641</version>
<version>1.12.701</version>
</dependency>
<!-- For AWS Secret Manager -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-secretsmanager</artifactId>
<version>1.12.544</version>
<version>1.12.701</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
Expand Down Expand Up @@ -107,7 +107,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-reload4j</artifactId>
<version>1.7.36</version>
<version>2.0.13</version>
</dependency>
<dependency>
<groupId>com.vividsolutions</groupId>
Expand All @@ -123,19 +123,19 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
<version>2.17.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.5.1</version>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>4.5.1</version>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
Expand Down Expand Up @@ -218,8 +218,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public UMMGranuleFile(Granule granule, Dataset dataset, boolean rangeIs360) {
this(granule, dataset,rangeIs360, new HashSet<MENDsIsoXMLSpatialTypeEnum>() );
}

public UMMGranuleFile() {
}

public UMMGranuleFile(Granule granule, Dataset dataset, boolean rangeIs360,
HashSet<MENDsIsoXMLSpatialTypeEnum> inputIsoXMLSpatialTypeEnumHashSet) {
this.granule = granule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URISyntaxException;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import gov.nasa.cumulus.metadata.aggregator.*;
Expand All @@ -30,6 +28,8 @@
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockedStatic;
import org.xml.sax.SAXException;
Expand All @@ -48,6 +48,27 @@


public class MetadataFilesToEchoTest {
MockedStatic<CMRRestClientProvider> mockedCMRRestClientProvider = null;
@Before
public void initialize() throws URISyntaxException, ParseException, IOException{
System.out.println("MetadataFilesToEchoTest constructor is being called");
UMMGranuleFile spiedUMMGranuleFile = Mockito.spy(UMMGranuleFile.class);
Mockito.doReturn(true)
.when(spiedUMMGranuleFile)
.isSpatialValid(any());
CMRLambdaRestClient mockedEchoLambdaRestClient= Mockito.mock(CMRLambdaRestClient.class);
Mockito.doReturn(true)
.when(mockedEchoLambdaRestClient)
.isUMMGSpatialValid(any(), any(), any());

mockedCMRRestClientProvider = mockStatic(CMRRestClientProvider.class);
mockedCMRRestClientProvider.when(CMRRestClientProvider::getLambdaRestClient).thenReturn(mockedEchoLambdaRestClient);
}

@After
public void cleanup(){
mockedCMRRestClientProvider.close();
}

@Test
public void testParseCommonHandlerFiles() {
Expand Down Expand Up @@ -982,7 +1003,6 @@ public void testSWOTCreateJsonSWOTIsoXMLSpatialType() throws IOException, ParseE
array.add("orbit");
array.add("bbox");

//HashSet<MENDsIsoXMLSpatialTypeEnum> h = lambda.createIsoXMLSpatialTypeSet("[footprint,orbit]");
HashSet<MENDsIsoXMLSpatialTypeEnum> h = lambda.createIsoXMLSpatialTypeSet(array);
assertTrue(h.contains(MENDsIsoXMLSpatialTypeEnum.FOOTPRINT));
assertTrue(h.contains(MENDsIsoXMLSpatialTypeEnum.ORBIT));
Expand All @@ -998,20 +1018,6 @@ public void testSWOTCreateJsonSWOTIsoXMLSpatialType() throws IOException, ParseE
File cfgFile = new File(classLoader.getResource("MODIS_T-JPL-L2P-v2014.0.cmr.cfg").getFile());
mfte.readConfiguration(cfgFile.getAbsolutePath());
mfte.readIsoMetadataFile(file.getAbsolutePath(), "s3://fake_bucket/fake_dir/fake.nc.iso.xml");
////// Star Mocking code of isSpatialValid
UMMGranuleFile mockedUMMGranuleFile = Mockito.mock(UMMGranuleFile.class);
Mockito.spy(mockedUMMGranuleFile);
Mockito.doReturn(true)
.when(mockedUMMGranuleFile)
.isSpatialValid(any());
CMRLambdaRestClient mockedEchoLambdaRestClient= Mockito.mock(CMRLambdaRestClient.class);
Mockito.doReturn(true)
.when(mockedEchoLambdaRestClient)
.isUMMGSpatialValid(any(), any(), any());

MockedStatic<CMRRestClientProvider> mockedECHORestClientProvider = mockStatic(CMRRestClientProvider.class);
when(CMRRestClientProvider.getLambdaRestClient()).thenReturn(mockedEchoLambdaRestClient);
////// END Mocking code of isSpatialValid

JSONObject granule = mfte.createJson();
JSONArray polygonPoints =((JSONArray)((JSONObject) ((JSONObject)((JSONArray)((JSONObject)((JSONObject)((JSONObject)granule
Expand Down
89 changes: 47 additions & 42 deletions src/test/java/gov/nasa/cumulus/metadata/test/UMMTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,36 @@
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockedStatic;
import org.xml.sax.SAXException;
import org.mockito.Mockito;

public class UMMTest {

MockedStatic<CMRRestClientProvider> mockedCMRRestClientProvider = null;
@Before
public void initialize() throws URISyntaxException, ParseException, IOException{
System.out.println("MetadataFilesToEchoTest constructor is being called");
UMMGranuleFile spiedUMMGranuleFile = Mockito.spy(UMMGranuleFile.class);
Mockito.doReturn(true)
.when(spiedUMMGranuleFile)
.isSpatialValid(any());
CMRLambdaRestClient mockedEchoLambdaRestClient= Mockito.mock(CMRLambdaRestClient.class);
Mockito.doReturn(true)
.when(mockedEchoLambdaRestClient)
.isUMMGSpatialValid(any(), any(), any());

mockedCMRRestClientProvider = mockStatic(CMRRestClientProvider.class);
mockedCMRRestClientProvider.when(CMRRestClientProvider::getLambdaRestClient).thenReturn(mockedEchoLambdaRestClient);
}

@After
public void cleanup(){
mockedCMRRestClientProvider.close();
}
@Test
public void testIsoRequiredFields() throws IOException, ParseException, XPathExpressionException, ParserConfigurationException, SAXException, URISyntaxException {
/*
Expand Down Expand Up @@ -743,48 +766,6 @@ public void testSentinelManifestNoProductName()
assertNull(umm.get("AdditionalAttributes"));
}

private JSONObject parseXfduManifest(String testFile, String testConfigFile, String granuleId)
throws XPathExpressionException, ParserConfigurationException,
IOException, SAXException, ParseException, java.text.ParseException, URISyntaxException {
String testDir = "src/test/resources";

String testFilePath = testDir + File.separator + testFile;

String testConfigFilePath = testDir + File.separator + testConfigFile;

MetadataFilesToEcho mtfe = new MetadataFilesToEcho(false);

mtfe.readConfiguration(testConfigFilePath);
mtfe.readSentinelManifest(testFilePath);

mtfe.getGranule().setName(granuleId);

// Mock the isSpatial valid function within UMMGFile class

UMMGranuleFile mockedUMMGranuleFile = Mockito.mock(UMMGranuleFile.class);
Mockito.spy(mockedUMMGranuleFile);
Mockito.doReturn(true)
.when(mockedUMMGranuleFile)
.isSpatialValid(any());

CMRLambdaRestClient mockedEchoLambdaRestClient= Mockito.mock(CMRLambdaRestClient.class);
Mockito.doReturn(true)
.when(mockedEchoLambdaRestClient)
.isUMMGSpatialValid(any(), any(), any());

MockedStatic<CMRRestClientProvider> mockedECHORestClientProvider = mockStatic(CMRRestClientProvider.class);
when(CMRRestClientProvider.getLambdaRestClient()).thenReturn(mockedEchoLambdaRestClient);

//write UMM-G to file
mtfe.writeJson(testDir + "/" + granuleId + ".cmr.json");

//the CMR file should have the following values...
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader(testDir + "/" + granuleId + ".cmr.json"));
// close the static mock. Otherwise, re-register static mock will cause exception.
mockedECHORestClientProvider.close();
return (JSONObject) obj;
}

@Test
public void testGetUmmChecksumAlgorithm() {
Expand Down Expand Up @@ -847,4 +828,28 @@ public void testCreateTrack() throws ParseException{
String tile = (String)tiles.get(1);
assertTrue(StringUtils.equals(tile, "tile2-2"));
}

private JSONObject parseXfduManifest(String testFile, String testConfigFile, String granuleId)
throws XPathExpressionException, ParserConfigurationException,
IOException, SAXException, ParseException, java.text.ParseException, URISyntaxException {
String testDir = "src/test/resources";
String testFilePath = testDir + File.separator + testFile;
String testConfigFilePath = testDir + File.separator + testConfigFile;

MetadataFilesToEcho mtfe = new MetadataFilesToEcho(false);

mtfe.readConfiguration(testConfigFilePath);
mtfe.readSentinelManifest(testFilePath);

mtfe.getGranule().setName(granuleId);
//write UMM-G to file
mtfe.writeJson(testDir + "/" + granuleId + ".cmr.json");

//the CMR file should have the following values...
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader(testDir + "/" + granuleId + ".cmr.json"));
return (JSONObject) obj;
}


}

0 comments on commit ad7d4dd

Please sign in to comment.