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

Mock Entrez PMIDlookup #112

Merged
merged 15 commits into from
Jul 9, 2024
Merged

Mock Entrez PMIDlookup #112

merged 15 commits into from
Jul 9, 2024

Conversation

tsande16
Copy link
Contributor

@tsande16 tsande16 commented May 9, 2024

The tests for EntrezPmidLookupTest were hitting against the real PubMed Entrez service. In most cases this would work, but if the service was down it would cause failures. This service has now been mocked in the tests.

To test:
mvn verify

@tsande16 tsande16 self-assigned this May 9, 2024
@tsande16 tsande16 requested a review from rpoet-jh May 10, 2024 13:47
Copy link
Contributor

@rpoet-jh rpoet-jh left a comment

Choose a reason for hiding this comment

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

Good job, Tim! I have a few comments.

@Test
public void testGetEntrezRecordJson() {
PmidLookup apiService = new PmidLookup();
@MockBean
Copy link
Contributor

Choose a reason for hiding this comment

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

In order to use @MockBean, the test needs to have @SpringBootTest and maybe @TestPropertySource("classpath:test-application.properties"). With this, the test becomes spring aware, and spring will take care of wiring things up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

String pmid = "29249144";
@BeforeEach
public void setUp() {
mockPmidLookup = mock(PmidLookup.class);
Copy link
Contributor

Choose a reason for hiding this comment

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

When you add @SpringBootTest, you will no longer need this method. Spring takes care of resetting the @MockBean between each test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


JSONObject pmr = apiService.retrievePubMedRecordAsJson(pmid);
assertTrue(pmr.getString("source").contains("Proteome"));
when(mockPmidLookup.retrievePubMedRecordAsJson(pmid)).thenReturn(entrezJson);
Copy link
Contributor

Choose a reason for hiding this comment

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

From what I read, this test is just calling the method being mocked. This is not really testing the PmiLookup code.

I think a better approach would be to use wiremock, set the PmiLookup URL to hit that and return json. This way, the PmiLookup code is covered. See here for an example:

Copy link
Contributor Author

@tsande16 tsande16 May 14, 2024

Choose a reason for hiding this comment

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

I decided to go with putting the entrez service url in the application.properties and in the IT, it will be the same endpoint except I need to get the wiremock port real time in the IT. I was going make a setter method in PmidLookup class, does this sound like a good plan for you? I can also emulate what you did with making the entrez service url also a system property. I wasn't sure which would be the better way to go. On one hand it could be overkill having multiple ways to configure this URL (sys property and application.properties), but also using a setter method just for an IT doesn't feel right either.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would try the following:

  • add httpPort to @WireMockTest to set the port, so something like @WireMockTest(httpPort = 9911)
  • set the entrez.url property to http://localhost:9911 in the test-application.properties
  • set the new PmidLookup.entrezUrl attribute using a @Value annotation

Note that I had to go with the Sys prop solution in journal loader because it is not spring boot enabled. Also note that using system props is part of standard spring boot property loading: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#features.external-config . Not applicable for journal loader, but just an FYI.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay cool! I thought the port was randomly assigned, but if I can manual set it, all the better! Thanks for the info.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@tsande16
Copy link
Contributor Author

Going to wait until the release is complete and will do a rebase with the new version. Then this should be ready for review.

@tsande16 tsande16 force-pushed the 850-mock-entrez-it-pmidlookup branch from 8d81a2f to a17245d Compare July 8, 2024 14:09
@tsande16 tsande16 requested a review from rpoet-jh July 8, 2024 14:34
@@ -155,6 +162,38 @@
</execution>
</executions>
</plugin>

<plugin>
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not needed, it is declared in pass-nihms-loader/pom.xml.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@rpoet-jh
Copy link
Contributor

rpoet-jh commented Jul 8, 2024

Just one small change noted in #112 (comment) @tsande16 , then I will approve.

public void testGetPubMedRecordWithHighAsciiChars() {
PmidLookup pmidLookup = new PmidLookup();
String pmid = "27648456";
public void testGetPubMedRecordWithHighAsciiChars() throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

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

A suggestion to reduce code is to update pmidrecord.json to have the high ascii character in the pmid_record_ascii.json title. Then you can just verify the title in testGetPubMedRecord and delete testGetPubMedRecordWithHighAsciiChars.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Other tests use this pmidrecord.json such as testNewCompliantPublication, testNewInProcessPublication, testNewNonCompliantPublication. Would it be okay to change these tests as well with the high ascii character?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are in TransformAndLoadCompliantIT, TransformAndLoadInProcessIT, and TransformAndLoadNonCompliantIT

Copy link
Contributor Author

@tsande16 tsande16 Jul 8, 2024

Choose a reason for hiding this comment

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

Done. This now includes the refactored NihmsSubmissionEtlITBase that has the member variables, title, doi, and issue that was used by the inheriting classes.

@MockBean private NihmsTransformLoadCLIRunner nihmsTransformLoadCLIRunner;

@Value("${pmc.entrez.service.url}")
private String ENTREZ_PATH;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this can be deleted, not being used looks like.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -0,0 +1,190 @@
{
Copy link
Contributor

Choose a reason for hiding this comment

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

You can delete this file now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -54,8 +55,11 @@ public class PmidLookup {
* delayed responses.
* https://www.ncbi.nlm.nih.gov/books/NBK25497/
*/
private static final String DEFAULT_ENTREZ_PATH = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary" +
".fcgi?db=pubmed&retmode=json&rettype=abstract&id=%s";
/*private static final String DEFAULT_ENTREZ_PATH = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary" +
Copy link
Contributor

Choose a reason for hiding this comment

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

You can delete this too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>org.eclipse.pass.loader.nihms.NihmsHarvesterCLI</mainClass>
<finalName>nihms-data-harvest-cli</finalName>
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this needs to remain here.

Copy link
Contributor Author

@tsande16 tsande16 Jul 8, 2024

Choose a reason for hiding this comment

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

For this one I was getting compilation errors. I think there was a recent mvn change or something. I will put it back in to get the error message.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. This has been reverted, but will be looked into in the future, per our discussion.

@tsande16 tsande16 merged commit 043b435 into main Jul 9, 2024
2 checks passed
@tsande16 tsande16 linked an issue Jul 9, 2024 that may be closed by this pull request
@tsande16 tsande16 deleted the 850-mock-entrez-it-pmidlookup branch July 18, 2024 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mock Entrez IT PMIDlookup
2 participants