Skip to content

Commit 08b2c80

Browse files
vins01-4scienceAdamF42
authored andcommitted
Merged in task/dspace-cris-2024_02_x/DSC-2802 (pull request DSpace#5657)
[DSC-2802] Approved-by: Fapohunda, Adamo
2 parents 717deaa + a8c9f60 commit 08b2c80

5 files changed

Lines changed: 197 additions & 72 deletions

File tree

dspace-api/src/main/java/org/dspace/content/authority/ZDBAuthority.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
import org.dspace.authority.AuthorityValue;
1919
import org.dspace.content.authority.zdb.ZDBAuthorityValue;
2020
import org.dspace.content.authority.zdb.ZDBService;
21-
import org.dspace.utils.DSpace;
21+
import org.dspace.content.authority.zdb.ZDBServicesFactory;
22+
2223
/**
2324
*
2425
* @author Mykhaylo Boychuk (4science.it)
@@ -29,9 +30,11 @@ public class ZDBAuthority extends ItemAuthority {
2930

3031
private static Logger log = LogManager.getLogger(ZDBAuthority.class);
3132

32-
private ZDBService source = new DSpace().getServiceManager().getServiceByName("ZDBSource", ZDBService.class);
33+
private ZDBService source;
3334

34-
private static DSpace dspace = new DSpace();
35+
public ZDBAuthority() {
36+
this.source = ZDBServicesFactory.getInstance().getZDBService();
37+
}
3538

3639
@Override
3740
public Choices getMatches(String query, int start, int limit, String locale) {
@@ -68,8 +71,7 @@ protected Choice[] addExternalResults(String text, Choices choices, int start, i
6871

6972
private Map<String, String> getZDBExtra(AuthorityValue val) {
7073
Map<String, String> extras = new HashMap<String, String>();
71-
List<ZDBExtraMetadataGenerator> generators = dspace.getServiceManager()
72-
.getServicesByType(ZDBExtraMetadataGenerator.class);
74+
List<ZDBExtraMetadataGenerator> generators = ZDBServicesFactory.getInstance().getMetadataGenerators();
7375
if (generators != null) {
7476
for (ZDBExtraMetadataGenerator gg : generators) {
7577
Map<String, String> extrasTmp = gg.build(val);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* The contents of this file are subject to the license and copyright
3+
* detailed in the LICENSE and NOTICE files at the root of the source
4+
* tree and available online at
5+
*
6+
* http://www.dspace.org/license/
7+
*/
8+
package org.dspace.content.authority.zdb;
9+
10+
import java.util.List;
11+
12+
import org.dspace.content.authority.ZDBExtraMetadataGenerator;
13+
14+
/**
15+
* Factory for ZDB services.
16+
*
17+
* @author Adamo Fapohunda (adamo.fapohunda at 4science.com)
18+
*/
19+
public abstract class ZDBServicesFactory {
20+
21+
public abstract ZDBService getZDBService();
22+
23+
public abstract List<ZDBExtraMetadataGenerator> getMetadataGenerators();
24+
25+
public static ZDBServicesFactory getInstance() {
26+
return new ZDBServicesFactoryImpl();
27+
}
28+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* The contents of this file are subject to the license and copyright
3+
* detailed in the LICENSE and NOTICE files at the root of the source
4+
* tree and available online at
5+
*
6+
* http://www.dspace.org/license/
7+
*/
8+
package org.dspace.content.authority.zdb;
9+
10+
import java.util.List;
11+
12+
import org.dspace.content.authority.ZDBExtraMetadataGenerator;
13+
import org.dspace.utils.DSpace;
14+
15+
/**
16+
* Default implementation of ZDBServicesFactory.
17+
*
18+
* @author Adamo Fapohunda (adamo.fapohunda at 4science.com)
19+
*/
20+
public class ZDBServicesFactoryImpl extends ZDBServicesFactory {
21+
22+
private ZDBService zdbService;
23+
private List<ZDBExtraMetadataGenerator> metadataGenerators;
24+
25+
public ZDBServicesFactoryImpl() {
26+
DSpace dspace = new DSpace();
27+
this.zdbService = dspace.getServiceManager().getServiceByName("ZDBSource", ZDBService.class);
28+
this.metadataGenerators = dspace.getServiceManager().getServicesByType(ZDBExtraMetadataGenerator.class);
29+
}
30+
31+
public ZDBServicesFactoryImpl(ZDBService zdbService) {
32+
this.zdbService = zdbService;
33+
this.metadataGenerators = new DSpace().getServiceManager().getServicesByType(ZDBExtraMetadataGenerator.class);
34+
}
35+
36+
@Override
37+
public ZDBService getZDBService() {
38+
return zdbService;
39+
}
40+
41+
@Override
42+
public List<ZDBExtraMetadataGenerator> getMetadataGenerators() {
43+
return metadataGenerators;
44+
}
45+
46+
public void setZDBService(ZDBService zdbService) {
47+
this.zdbService = zdbService;
48+
}
49+
}

dspace-server-webapp/src/test/java/org/dspace/app/rest/ZDBAuthorityIT.java

Lines changed: 113 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,109 @@
1111
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
1212
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
1313

14+
import java.io.IOException;
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
1418
import org.dspace.app.rest.matcher.ItemAuthorityMatcher;
1519
import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
20+
import org.dspace.content.authority.DCInputAuthority;
1621
import org.dspace.content.authority.ItemAuthority;
22+
import org.dspace.content.authority.service.ChoiceAuthorityService;
23+
import org.dspace.content.authority.zdb.ZDBAuthorityValue;
24+
import org.dspace.content.authority.zdb.ZDBService;
25+
import org.dspace.content.authority.zdb.ZDBServicesFactory;
26+
import org.dspace.content.authority.zdb.ZDBServicesFactoryImpl;
27+
import org.dspace.core.service.PluginService;
1728
import org.dspace.services.ConfigurationService;
1829
import org.hamcrest.Matchers;
30+
import org.junit.After;
31+
import org.junit.AfterClass;
32+
import org.junit.Before;
33+
import org.junit.BeforeClass;
1934
import org.junit.Test;
35+
import org.mockito.MockedStatic;
36+
import org.mockito.Mockito;
2037
import org.springframework.beans.factory.annotation.Autowired;
21-
import org.springframework.context.annotation.Import;
2238

2339
/**
2440
* This class handles ZDBAuthority related IT.
2541
*
2642
* @author Luca Giamminonni (luca.giamminonni at 4Science.it)
2743
*/
28-
@Import(ZDBAuthorityTestConfig.class)
2944
public class ZDBAuthorityIT extends AbstractControllerIntegrationTest {
3045

46+
public static final String ZDB_AUTHORITY =
47+
"org.dspace.content.authority.ZDBAuthority = ZDBAuthority";
48+
49+
private static MockedStatic<ZDBServicesFactory> mockZDBServiceFactory;
50+
3151
@Autowired
3252
private ConfigurationService configurationService;
3353

54+
@Autowired
55+
protected ChoiceAuthorityService choiceAuthorityService;
56+
57+
@Autowired
58+
protected PluginService pluginService;
59+
60+
private ZDBService zdbService;
61+
62+
@BeforeClass
63+
public static void init() {
64+
mockZDBServiceFactory = Mockito.mockStatic(ZDBServicesFactory.class);
65+
}
66+
67+
@AfterClass
68+
public static void close() {
69+
mockZDBServiceFactory.close();
70+
}
71+
72+
@Before
73+
public void setup() throws IOException {
74+
75+
this.configurationService.setProperty("cris.zdb.search.url", null);
76+
this.configurationService.setProperty("cris.zdb.detail.url", null);
77+
78+
zdbService = Mockito.mock(ZDBService.class);
79+
80+
// Create factory with mocked service - generators will be loaded from ServiceManager
81+
ZDBServicesFactoryImpl zdbServiceFactory = new ZDBServicesFactoryImpl(zdbService);
82+
83+
Mockito.when(
84+
zdbService.list(Mockito.eq("Acta AND Mathematica AND informatica"), Mockito.anyInt(),
85+
Mockito.anyInt()))
86+
.thenReturn(createMockResults());
87+
88+
// Mock the static factory method to return our factory with mocked service
89+
mockZDBServiceFactory.when(ZDBServicesFactory::getInstance).thenReturn(zdbServiceFactory);
90+
91+
// Register the authority plugin
92+
configurationService.setProperty(
93+
"plugin.named.org.dspace.content.authority.ChoiceAuthority",
94+
new String[] {ZDB_AUTHORITY}
95+
);
96+
}
97+
98+
@After
99+
public void tearDown() throws Exception {
100+
DCInputAuthority.reset();
101+
pluginService.clearNamedPluginClasses();
102+
choiceAuthorityService.clearCache();
103+
}
104+
34105
@Test
35106
public void zdbAuthorityTest() throws Exception {
107+
// Configure ZDB authority source
108+
configurationService.setProperty("cris.ItemAuthority.ZDBAuthority.source", "zdb");
109+
110+
// Reset authorities and trigger re-initialization with mock factory
111+
DCInputAuthority.reset();
112+
pluginService.clearNamedPluginClasses();
113+
choiceAuthorityService.getChoiceAuthoritiesNames();
114+
choiceAuthorityService.clearCache();
115+
DCInputAuthority.getPluginNames();
116+
36117
String token = getAuthToken(eperson.getEmail(), password);
37118
getClient(token).perform(get("/api/submission/vocabularies/ZDBAuthority/entries")
38119
.param("filter", "Acta AND Mathematica AND informatica"))
@@ -80,4 +161,34 @@ private String getSource() {
80161
return configurationService.getProperty(
81162
"cris.ItemAuthority.ZDBAuthority.source", ItemAuthority.DEFAULT);
82163
}
164+
165+
private List<ZDBAuthorityValue> createMockResults() {
166+
List<ZDBAuthorityValue> results = new ArrayList<>();
167+
// Create the first entry
168+
ZDBAuthorityValue zdb1 = new ZDBAuthorityValue();
169+
zdb1.setServiceId("1447228-4");
170+
zdb1.setValue("Acta mathematica et informatica");
171+
zdb1.addOtherMetadata("journalZDBID", "1447228-4");
172+
zdb1.addOtherMetadata("journalTitle", "Acta mathematica et informatica");
173+
174+
// Create the second entry
175+
ZDBAuthorityValue zdb2 = new ZDBAuthorityValue();
176+
zdb2.setServiceId("1194912-0");
177+
zdb2.setValue("Acta mathematica Universitatis Ostraviensis");
178+
zdb2.addOtherMetadata("journalZDBID", "1194912-0");
179+
zdb2.addOtherMetadata("journalTitle", "Acta mathematica Universitatis Ostraviensis");
180+
zdb2.addOtherMetadata("journalIssn", "1211-4774");
181+
182+
// Create the third entry
183+
ZDBAuthorityValue zdb3 = new ZDBAuthorityValue();
184+
zdb3.setServiceId("2618143-5");
185+
zdb3.setValue("Acta mathematica Universitatis Ostraviensis");
186+
zdb3.addOtherMetadata("journalZDBID", "2618143-5");
187+
zdb3.addOtherMetadata("journalTitle", "Acta mathematica Universitatis Ostraviensis");
188+
189+
results.add(zdb1);
190+
results.add(zdb2);
191+
results.add(zdb3);
192+
return results;
193+
}
83194
}

dspace-server-webapp/src/test/java/org/dspace/app/rest/ZDBAuthorityTestConfig.java

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)