33import org .jetbrains .annotations .Nullable ;
44import org .json .JSONArray ;
55import org .json .JSONObject ;
6- import org .labkey .panoramapublic .ncbi .NcbiConstants .DB ;
76
87import java .io .IOException ;
98import java .util .ArrayList ;
1413/**
1514 * Mock implementation of {@link NcbiPublicationSearchService} that returns canned data registered by tests.
1615 * Used by Selenium tests when running on TeamCity.
17- * Extends {@link NcbiPublicationSearchServiceImpl} and only overrides the two methods that make HTTP calls
18- * to NCBI: {@link #getJson(String)} (used by ESearch/ESummary) and {@link #getCitation(String, DB)}
19- * (used for the Citation Exporter API). All search logic, filtering, author/title verification, and
20- * priority filtering run through the real implementation code.
16+ * Extends {@link NcbiPublicationSearchServiceImpl} and only overrides {@link #getString(String)},
17+ * the single method that makes HTTP calls to NCBI. All search logic, filtering, author/title
18+ * verification, citation parsing, and priority filtering run through the real implementation code.
2119 * Tests register mock articles via {@link #register}, providing the database, ID, search key,
2220 * metadata fields, and citation. The mock builds internal lookup maps from this data and returns
23- * appropriate JSON responses when the real search logic calls {@code getJson()} or {@code getCitation ()}.
21+ * appropriate responses when the real search logic calls {@code getString ()}.
2422 */
2523public class MockNcbiPublicationSearchService extends NcbiPublicationSearchServiceImpl
2624{
@@ -37,7 +35,7 @@ public class MockNcbiPublicationSearchService extends NcbiPublicationSearchServi
3735
3836 /**
3937 * Register a mock article. The mock stores the data in internal lookup maps used by
40- * {@link #getJson (String)} and {@link #getCitation(String, DB )}.
38+ * {@link #getString (String)}.
4139 * @param database "pmc" or "pubmed" — the NCBI database this article is in
4240 * @param id the article ID in the given database (numeric ID for pmc or pubmed)
4341 * @param searchKey what ESearch query term finds this article (e.g. PXD ID for PMC, author last name for PubMed)
@@ -115,31 +113,27 @@ public void register(String database, String id, String searchKey,
115113 }
116114
117115 /**
118- * Returns canned JSON for NCBI ESearch and ESummary API requests based on registered mock data.
116+ * Returns canned responses for NCBI API requests based on registered mock data.
117+ * Handles ESearch, ESummary, and Citation Exporter URLs.
119118 */
120119 @ Override
121- protected JSONObject getJson (String url ) throws IOException
120+ protected String getString (String url ) throws IOException
122121 {
123122 if (url .contains ("esearch.fcgi" ))
124123 {
125- return handleESearch (url );
124+ return handleESearch (url ). toString () ;
126125 }
127126 else if (url .contains ("esummary.fcgi" ))
128127 {
129- return handleESummary (url );
128+ return handleESummary (url ).toString ();
129+ }
130+ else if (url .contains ("lit/ctxp" ))
131+ {
132+ return handleCitation (url ).toString ();
130133 }
131134 throw new IOException ("MockNcbiPublicationSearchService: unexpected URL: " + url );
132135 }
133136
134- /**
135- * Returns the registered citation for the given PubMed ID, or null if not registered.
136- */
137- @ Override
138- public @ Nullable String getCitation (String pubMedId , DB database )
139- {
140- return _citations .get (pubMedId );
141- }
142-
143137 private JSONObject handleESearch (String url )
144138 {
145139 boolean isPmc = url .contains ("db=pmc" );
@@ -175,4 +169,33 @@ private JSONObject handleESummary(String url)
175169
176170 return new JSONObject ().put ("result" , result );
177171 }
172+
173+ /**
174+ * Build a citation JSON response matching the NCBI Literature Citation Exporter format.
175+ * The real API returns {@code {"nlm":{"orig":"citation text..."}}}.
176+ * If no citation is registered for the ID, returns an empty JSON object.
177+ */
178+ private JSONObject handleCitation (String url )
179+ {
180+ // Extract the publication ID from the URL (last segment after "id=")
181+ String id = null ;
182+ int idIdx = url .indexOf ("id=" );
183+ if (idIdx >= 0 )
184+ {
185+ id = url .substring (idIdx + 3 );
186+ // Remove any trailing query parameters
187+ int ampIdx = id .indexOf ('&' );
188+ if (ampIdx >= 0 )
189+ {
190+ id = id .substring (0 , ampIdx );
191+ }
192+ }
193+
194+ String citation = id != null ? _citations .get (id ) : null ;
195+ if (citation != null )
196+ {
197+ return new JSONObject ().put ("nlm" , new JSONObject ().put ("orig" , citation ));
198+ }
199+ return new JSONObject ();
200+ }
178201}
0 commit comments