forked from sritejakv/splitwise-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOAuthUtilTest.java
More file actions
96 lines (80 loc) · 3.51 KB
/
OAuthUtilTest.java
File metadata and controls
96 lines (80 loc) · 3.51 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
package utils;
import com.github.scribejava.core.model.OAuth1AccessToken;
import com.github.scribejava.core.model.OAuthRequest;
import com.github.scribejava.core.model.Response;
import com.github.scribejava.core.model.Verb;
import com.github.scribejava.core.oauth.OAuth10aService;
import constants.Strings;
import constants.URL;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.internal.util.reflection.FieldSetter;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class OAuthUtilTest {
@InjectMocks
private OAuthUtil oAuthUtil;
@Mock
private OAuth10aService mockService;
private Response mockResponse = new Response(200, "mockMessage", new HashMap<String, String>(),
new InputStream() {
@Override
public int read() throws IOException {
return 0;
}
});
private Response mockInvalidResponse = new Response(400, "mockInvalidMessage",
new HashMap<String, String>(),
"Invalid Request.");
private String mockApiToken = "jdfajiodflslm";
private String mockApiSecret = "jipmewlfeifmdls";
private String mockRawResponse = "{key: value}";
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
oAuthUtil = new OAuthUtil();
}
@Test
public void makeRequest() throws Exception {
doAnswer(new Answer<Void>() {
public Void answer(InvocationOnMock invocation) throws Throwable {
Object[] arguments = invocation.getArguments();
return null;
}
}).when(mockService).signRequest(any(OAuth1AccessToken.class), any(OAuthRequest.class));
when(mockService.execute(any(OAuthRequest.class))).thenReturn(mockResponse);
FieldSetter.setField(oAuthUtil, oAuthUtil.getClass().getDeclaredField("service"), mockService);
oAuthUtil.setAccessToken(mockApiToken, mockApiSecret, mockRawResponse);
Response resp = oAuthUtil.makeRequest(String.format(URL.GET_FRIENDS, Strings.SPLITWISE_API_VERSION), Verb.GET);
assertThat(resp.getCode(), is(200));
verify(mockService).signRequest(any(OAuth1AccessToken.class), any(OAuthRequest.class));
verify(mockService).execute(any(OAuthRequest.class));
}
@Test(expected = Exception.class)
public void makeInvalidRequest() throws Exception {
doAnswer(new Answer<Void>() {
public Void answer(InvocationOnMock invocation) {
return null;
}
}).when(mockService).signRequest(any(OAuth1AccessToken.class), any(OAuthRequest.class));
when(mockService.execute(any(OAuthRequest.class))).thenReturn(mockInvalidResponse);
FieldSetter.setField(oAuthUtil, oAuthUtil.getClass().getDeclaredField("service"), mockService);
oAuthUtil.setAccessToken(mockApiToken, mockApiSecret, mockRawResponse);
Response resp = oAuthUtil.makeRequest(String.format(URL.GET_FRIENDS, Strings.SPLITWISE_API_VERSION), Verb.GET);
}
}