forked from sritejakv/splitwise-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplitwiseTest.java
More file actions
141 lines (129 loc) · 5.19 KB
/
SplitwiseTest.java
File metadata and controls
141 lines (129 loc) · 5.19 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
import com.github.scribejava.core.model.Response;
import com.github.scribejava.core.model.Verb;
import constants.MockResponses;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import utils.OAuthUtil;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class SplitwiseTest {
//Fill in the apiKey and apiSecret before running the test cases.
private String apiKey = "sdfksdfds";
private String apiSecret = "jfkdjfkd";
//Fill token, tokenSecret after authorization.
private String token = "kjfkdjksk";
private String tokenSecret = "lkjdfimdskl;";
@InjectMocks
private Splitwise splitwise;
@Before
public void setUp() {
splitwise = new Splitwise(apiKey, apiSecret);
splitwise.util.setAccessToken(token, tokenSecret, "");
}
@Test
public void getAuthorizationUrl() {
String authUrl = null;
OAuthUtil mockUtil = mock(OAuthUtil.class);
splitwise.util = mockUtil;
try {
when(mockUtil.getAuthorizationUrl()).thenReturn(MockResponses.MOCK_AUTHURL);
authUrl = splitwise.getAuthorizationUrl();
URL auth = new URL(authUrl);
} catch (InterruptedException e) {
fail(e.getMessage());
} catch (ExecutionException e) {
fail(e.getMessage());
} catch (IOException e) {
fail(e.getMessage());
}
}
@Test
public void getCurrentUser() {
Response mockResponse = new Response(200, "mockMessage", new HashMap<String, String>(),
MockResponses.MOCK_USER);
OAuthUtil mockUtil = mock(OAuthUtil.class);
splitwise.util = mockUtil;
try {
when(mockUtil.makeRequest(any(String.class), any(Verb.class))).thenReturn(mockResponse);
String userDetails = splitwise.getCurrentUser();
JSONObject userJson = new JSONObject(userDetails);
assertTrue(userJson.has("user"));
verify(mockUtil).makeRequest(any(String.class), any(Verb.class));
} catch (Exception e) {
fail(e.getMessage());
}
}
@Test
public void getFriends() {
Response mockResponse = new Response(200, "mockMessage", new HashMap<String, String>(),
MockResponses.MOCK_FRIEND);
OAuthUtil mockUtil = mock(OAuthUtil.class);
splitwise.util = mockUtil;
try {
when(mockUtil.makeRequest(any(String.class), any(Verb.class))).thenReturn(mockResponse);
String friends = splitwise.getFriends();
JSONObject userJson = new JSONObject(friends);
assertTrue(userJson.has("friends"));
verify(mockUtil).makeRequest(any(String.class), any(Verb.class));
} catch (Exception e) {
fail(e.getMessage());
}
}
@Test
public void getExpenses() {
Response mockResponse = new Response(200, "mockMessage", new HashMap<String, String>(),
MockResponses.MOCK_EXPENSES);
OAuthUtil mockUtil = mock(OAuthUtil.class);
splitwise.util = mockUtil;
try {
when(mockUtil.makeRequest(any(String.class), any(Verb.class))).thenReturn(mockResponse);
String expenses = splitwise.getExpenses();
JSONObject userJson = new JSONObject(expenses);
assertTrue(userJson.has("expenses"));
verify(mockUtil).makeRequest(any(String.class), any(Verb.class));
} catch (Exception e) {
fail(e.getMessage());
}
}
@Test
public void getCurrencies() {
Response mockResponse = new Response(200, "mockMessage", new HashMap<String, String>(),
MockResponses.MOCK_CURRENCIES);
OAuthUtil mockUtil = mock(OAuthUtil.class);
splitwise.util = mockUtil;
try {
when(mockUtil.makeRequest(any(String.class), any(Verb.class))).thenReturn(mockResponse);
String currencies = splitwise.getCurrencies();
JSONObject userJson = new JSONObject(currencies);
assertTrue(userJson.has("currencies"));
verify(mockUtil).makeRequest(any(String.class), any(Verb.class));
} catch (Exception e) {
fail(e.getMessage());
}
}
@Test
public void getCategories() {
Response mockResponse = new Response(200, "mockMessage", new HashMap<String, String>(),
MockResponses.MOCK_CATGORIES);
OAuthUtil mockUtil = mock(OAuthUtil.class);
splitwise.util = mockUtil;
try {
when(mockUtil.makeRequest(any(String.class), any(Verb.class))).thenReturn(mockResponse);
String categories = splitwise.getCategories();
JSONObject userJson = new JSONObject(categories);
assertTrue(userJson.has("categories"));
verify(mockUtil).makeRequest(any(String.class), any(Verb.class));
} catch (Exception e) {
fail(e.getMessage());
}
}
}