-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCRaterControllerTest.java
More file actions
138 lines (119 loc) · 4.9 KB
/
Copy pathCRaterControllerTest.java
File metadata and controls
138 lines (119 loc) · 4.9 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
package org.wise.vle.web;
import static junit.framework.TestCase.assertEquals;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import org.easymock.EasyMockExtension;
import org.easymock.Mock;
import org.easymock.TestSubject;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.wise.portal.service.ping.CRaterPingService;
import org.wise.vle.domain.webservice.crater.CRaterPingRequest;
import org.wise.vle.domain.webservice.crater.CRaterScoringRequest;
import org.wise.vle.domain.webservice.crater.CRaterService;
import org.wise.vle.domain.webservice.crater.CRaterVerificationRequest;
@ExtendWith(EasyMockExtension.class)
public class CRaterControllerTest {
@TestSubject
private CRaterController controller = new CRaterController();
@Mock
private CRaterService cRaterService;
@Mock
private CRaterPingService cRaterPingService;
private String clientId = "wise-test";
private String itemId = "test-item-id";
private String berkeleyItemId = "berkeley_test-item-id";
private Long trackingId = 123456789L;
@Test
public void verifyItemId_ShouldReturnString() {
CRaterVerificationRequest request = new CRaterVerificationRequest();
request.setItemId(itemId);
try {
expect(cRaterService.getCRaterResponse(request))
.andReturn(createVerificationResponseString(itemId, true, trackingId, clientId));
replay(cRaterService);
String response = controller.verifyItemId(request);
assertNotNull(response);
verify(cRaterService);
} catch (JSONException exception) {
}
}
private String createVerificationResponseString(String itemId, Boolean available, Long trackingId,
String clientId) {
StringBuffer responseBuffer = new StringBuffer();
responseBuffer.append("{");
responseBuffer.append(" \"item_id\": \"" + itemId + "\",");
responseBuffer.append(" \"available\": \"" + available + "\",");
responseBuffer.append(" \"tracking_id\": " + trackingId + ",");
responseBuffer.append(" \"client_id\": \"" + clientId + "\"");
responseBuffer.append("}");
return responseBuffer.toString();
}
@Test
public void scoreItemId_ShouldReturnString() {
CRaterScoringRequest request = new CRaterScoringRequest();
request.setItemId(itemId);
try {
expect(cRaterService.getCRaterResponse(request))
.andReturn(createScoringResponseString(itemId, trackingId, clientId));
replay(cRaterService);
String response = controller.scoreItem(request);
assertNotNull(response);
verify(cRaterService);
} catch (JSONException exception) {
}
}
private String createScoringResponseString(String itemId, Long trackingId, String clientId) {
StringBuffer responseBuffer = new StringBuffer();
responseBuffer.append("{");
responseBuffer.append(" \"item_id\": \"" + itemId + "\",");
responseBuffer.append(" \"responses\": {},");
responseBuffer.append(" \"tracking_id\": " + trackingId + ",");
responseBuffer.append(" \"client_id\": \"" + clientId + "\"");
responseBuffer.append("}");
return responseBuffer.toString();
}
@Test
public void pingItem_ItemAlreadyPinged_ShouldReturnEmpty() {
CRaterPingRequest request = new CRaterPingRequest();
request.setItemId(berkeleyItemId);
try {
expect(cRaterPingService.hasPingedItem(berkeleyItemId)).andReturn(true);
replay(cRaterService, cRaterPingService);
String response = controller.pingItem(request);
assertEquals(response, "");
verify(cRaterService, cRaterPingService);
} catch (JSONException exception) {
}
}
@Test
public void pingItem_ItemNotPinged_ShouldReturnString() {
CRaterPingRequest request = new CRaterPingRequest();
request.setItemId(berkeleyItemId);
try {
expect(cRaterService.getCRaterResponse(request))
.andReturn(createPingResponseString(berkeleyItemId, trackingId, clientId));
expect(cRaterPingService.hasPingedItem(berkeleyItemId)).andReturn(false);
cRaterPingService.cachePingedItem(berkeleyItemId, 280);
expectLastCall();
replay(cRaterService, cRaterPingService);
String response = controller.pingItem(request);
assertNotNull(response);
assertNotEquals(response, "");
verify(cRaterService, cRaterPingService);
} catch (JSONException exception) {
}
}
private String createPingResponseString(String itemId, Long trackingId, String clientId) {
StringBuffer responseBuffer = new StringBuffer();
responseBuffer.append("{");
responseBuffer.append(" \"item_id\": \"" + itemId + "\",");
responseBuffer.append(" \"success\": true,");
responseBuffer.append(" \"tracking_id\": " + trackingId + ",");
responseBuffer.append(" \"client_id\": \"" + clientId + "\"");
responseBuffer.append("}");
return responseBuffer.toString();
}
}