|
11 | 11 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
12 | 12 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
13 | 13 |
|
| 14 | +import java.io.IOException; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | + |
14 | 18 | import org.dspace.app.rest.matcher.ItemAuthorityMatcher; |
15 | 19 | import org.dspace.app.rest.test.AbstractControllerIntegrationTest; |
| 20 | +import org.dspace.content.authority.DCInputAuthority; |
16 | 21 | 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; |
17 | 28 | import org.dspace.services.ConfigurationService; |
18 | 29 | import org.hamcrest.Matchers; |
| 30 | +import org.junit.After; |
| 31 | +import org.junit.AfterClass; |
| 32 | +import org.junit.Before; |
| 33 | +import org.junit.BeforeClass; |
19 | 34 | import org.junit.Test; |
| 35 | +import org.mockito.MockedStatic; |
| 36 | +import org.mockito.Mockito; |
20 | 37 | import org.springframework.beans.factory.annotation.Autowired; |
21 | | -import org.springframework.context.annotation.Import; |
22 | 38 |
|
23 | 39 | /** |
24 | 40 | * This class handles ZDBAuthority related IT. |
25 | 41 | * |
26 | 42 | * @author Luca Giamminonni (luca.giamminonni at 4Science.it) |
27 | 43 | */ |
28 | | -@Import(ZDBAuthorityTestConfig.class) |
29 | 44 | public class ZDBAuthorityIT extends AbstractControllerIntegrationTest { |
30 | 45 |
|
| 46 | + public static final String ZDB_AUTHORITY = |
| 47 | + "org.dspace.content.authority.ZDBAuthority = ZDBAuthority"; |
| 48 | + |
| 49 | + private static MockedStatic<ZDBServicesFactory> mockZDBServiceFactory; |
| 50 | + |
31 | 51 | @Autowired |
32 | 52 | private ConfigurationService configurationService; |
33 | 53 |
|
| 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 | + |
34 | 105 | @Test |
35 | 106 | 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 | + |
36 | 117 | String token = getAuthToken(eperson.getEmail(), password); |
37 | 118 | getClient(token).perform(get("/api/submission/vocabularies/ZDBAuthority/entries") |
38 | 119 | .param("filter", "Acta AND Mathematica AND informatica")) |
@@ -80,4 +161,34 @@ private String getSource() { |
80 | 161 | return configurationService.getProperty( |
81 | 162 | "cris.ItemAuthority.ZDBAuthority.source", ItemAuthority.DEFAULT); |
82 | 163 | } |
| 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 | + } |
83 | 194 | } |
0 commit comments