forked from DSpace/DSpace
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCachingOrcidRestConnectorTest.java
More file actions
201 lines (165 loc) · 8.32 KB
/
Copy pathCachingOrcidRestConnectorTest.java
File metadata and controls
201 lines (165 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.external;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.io.IOException;
import java.io.InputStream;
import org.dspace.AbstractDSpaceTest;
import org.dspace.external.provider.orcid.xml.ExpandedSearchConverter;
import org.dspace.utils.DSpace;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.cache.Cache;
import org.springframework.cache.jcache.JCacheCacheManager;
public class CachingOrcidRestConnectorTest extends AbstractDSpaceTest {
//This token should be valid for 20 years
private static final String sandboxToken = "4bed1e13-7792-4129-9f07-aaf7b88ba88f";
private static final String orcid = "0000-0002-9150-2529";
private static final String expectedLabel = "Connor, John";
// Canned ORCID "expanded-search" response (num-found=1725, first result -> "Connor, John").
// Used to mock the HTTP layer so the tests don't depend on the live ORCID sandbox.
private static final String EXPANDED_SEARCH_XML = "org/dspace/external/orcid-expanded-search.xml";
private CachingOrcidRestConnector sut;
/**
* Load a canned API response from the test classpath as a fresh InputStream.
* (A new stream is returned on every call because the connector consumes/closes it.)
*/
private InputStream cannedResponse(String resource) {
InputStream is = getClass().getClassLoader().getResourceAsStream(resource);
assertNotNull("Missing test resource: " + resource, is);
return is;
}
@Before
public void setup() {
sut = new CachingOrcidRestConnector();
}
@Test(expected = RuntimeException.class)
public void getAccessToken_badUrl() {
String accessToken = sut.getAccessToken("secret","id", "http://example.com");
assertNull("Expecting accessToken to be null", accessToken);
}
@Test(expected = RuntimeException.class)
public void getAccessToken_badParams() {
//expect an exception to be thrown
sut.getAccessToken(null, null, null);
}
@Test(expected = RuntimeException.class)
public void getAccessToken() {
String accessToken = sut.getAccessToken("DEAD", "BEEF", "https://sandbox.orcid.org/oauth/token");
assertNotNull("Expecting accessToken to be not null", accessToken);
}
@Test
public void getLabel() throws Exception {
sut = Mockito.spy(sut);
sut.setApiURL("https://pub.sandbox.orcid.org/v3.0");
//Mock the CachingOrcidRestConnector so that getAccessToken returns sandboxToken
doReturn(sandboxToken).when(sut).getAccessToken(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
//Mock the HTTP layer with a canned response so we don't depend on the live ORCID sandbox.
doReturn(cannedResponse(EXPANDED_SEARCH_XML)).when(sut).httpGet(Mockito.anyString(), Mockito.anyString());
String label = sut.getLabel(orcid);
assertEquals(expectedLabel, label);
}
@Test
public void search() throws Exception {
sut = Mockito.spy(sut);
sut.setApiURL("https://pub.sandbox.orcid.org/v3.0");
//Mock the CachingOrcidRestConnector so that getAccessToken returns sandboxToken
doReturn(sandboxToken).when(sut).getAccessToken(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
//Mock the HTTP layer with a canned ORCID expanded-search response. Previously this test hit the live
//ORCID sandbox and asserted numFound() > 1000, which flaked whenever the sandbox dataset was reset/shrunk.
//Mocking the transport keeps the real parsing + edismax wildcard query-building path under test, but makes
//the result deterministic.
doReturn(cannedResponse(EXPANDED_SEARCH_XML)).when(sut).httpGet(Mockito.anyString(), Mockito.anyString());
ExpandedSearchConverter.Results search = sut.search("joh", 0, 1);
assertTrue("Expected a successful ORCID response, got: " + search, search.isOk());
//'joh' is alphabetic, so the connector turns it into an edismax wildcard query ("joh || joh*") that matches
//many authors; the canned response carries num-found=1725.
assertEquals("Unexpected num-found for the canned ORCID response", 1725L, (long) search.numFound());
assertEquals("Connor, John", search.results().get(0).label());
}
@Test
public void search_fail() throws Exception {
sut = Mockito.spy(sut);
sut.setApiURL("https://pub.sandbox.orcid.org/v3.0");
//Mock the CachingOrcidRestConnector so that getAccessToken returns an invalid token
doReturn("FAKE").when(sut).getAccessToken(Mockito.anyString(), Mockito.anyString(),
Mockito.anyString());
//Simulate the ORCID API rejecting the (fake) token: every httpGet fails. Done via the mocked HTTP layer
//so the test is deterministic and doesn't rely on the live sandbox returning a 401.
doThrow(new IOException("simulated ORCID auth failure")).when(sut)
.httpGet(Mockito.anyString(), Mockito.anyString());
ExpandedSearchConverter.Results search = sut.search("joh", 0, 1);
assertFalse(search.isOk());
//Further calls fail too, token is stored (so getAccessToken is only resolved once)
search = sut.search("joh", 0, 1);
assertFalse(search.isOk());
verify(sut, times(1)).getAccessToken(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
}
@Test
public void testCachable() {
CachingOrcidRestConnector c = new DSpace().getServiceManager().getServiceByName(
"CachingOrcidRestConnector", CachingOrcidRestConnector.class);
Cache cache = prepareCache();
assertNull(cache.get(orcid));
/*
I have issues trying to mock/spy when the class a spring bean modified by cglib
doReturn(sandboxToken).when(c).getAccessToken(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
verify(c, times(1)).getLabel(orcid);
*/
c.setApiURL("https://pub.sandbox.orcid.org/v3.0");
c.forceAccessToken(sandboxToken);
String r1 = c.getLabel(orcid);
assertEquals(expectedLabel, r1);
String r2 = c.getLabel(orcid);
assertEquals(expectedLabel, r2);
//get the orcid-labels cache and verify that the label is there
assertEquals(expectedLabel, cache.get(orcid).get());
}
@Test
public void testCacheableWithError() {
CachingOrcidRestConnector c = new DSpace().getServiceManager().getServiceByName(
"CachingOrcidRestConnector", CachingOrcidRestConnector.class);
Cache cache = prepareCache();
assertNull(cache.get(orcid));
//skip init
c.forceAccessToken(sandboxToken);
//set bad ApiURL to provoke an error
c.setApiURL("https://api.sandbox.orcid.org/");
String r1 = c.getLabel(orcid);
//on error, getLabel should return null
assertNull(r1);
//the cache should not contain a value for this id
assertNull(cache.get(orcid));
//fix the error
c.setApiURL("https://pub.sandbox.orcid.org/v3.0");
// the error flipped the initialized flag, this reset it
c.forceAccessToken(sandboxToken);
String r2 = c.getLabel(orcid);
assertEquals(expectedLabel, r2);
//the cache should now contain a value for this id
assertEquals(expectedLabel, cache.get(orcid).get());
}
private Cache prepareCache() {
//get the cacheManager from the serviceManager
JCacheCacheManager cacheManager = new DSpace().getServiceManager().getServiceByName("cacheManager",
JCacheCacheManager.class);
Cache cache = cacheManager.getCache("orcid-labels");
//each test should have a clean cache
cache.clear();
return cache;
}
}