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
175 lines (142 loc) · 6.65 KB
/
Copy pathCachingOrcidRestConnectorTest.java
File metadata and controls
175 lines (142 loc) · 6.65 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
/**
* 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.times;
import static org.mockito.Mockito.verify;
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";
private CachingOrcidRestConnector sut;
@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() {
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());
String label = sut.getLabel(orcid);
assertEquals(expectedLabel, label);
}
@Test
public void search() {
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());
ExpandedSearchConverter.Results search = sut.search("joh", 0, 1);
//Should match all Johns also, because edismax with wildcard.
//We hit the live ORCID sandbox here, so the exact count fluctuates with sandbox data.
//Only assert the call succeeded and the wildcard returned more hits than the requested page size
//(which proves both the API call and the edismax wildcard expansion work) -- using a small,
//stable lower bound to avoid CI flakiness when the sandbox dataset shrinks.
assertTrue("Expected a successful ORCID sandbox response, got: " + search, search.isOk());
assertTrue("Expected edismax wildcard to return more than 1 match for 'joh', got: "
+ search.numFound(), search.numFound() > 1);
}
@Test
public void search_fail() {
sut = Mockito.spy(sut);
sut.setApiURL("https://pub.sandbox.orcid.org/v3.0");
//Mock the CachingOrcidRestConnector so that getAccessToken returns and invalid token
doReturn("FAKE").when(sut).getAccessToken(Mockito.anyString(), Mockito.anyString(),
Mockito.anyString());
ExpandedSearchConverter.Results search = sut.search("joh", 0, 1);
assertFalse(search.isOk());
//Further calls fail too, token is stored
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;
}
}