Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.wise.vle.domain.webservice.crater;

import org.json.JSONException;
import org.json.JSONObject;

import lombok.Getter;
import lombok.Setter;

@Setter
public abstract class AbstractCRaterRequest implements CRaterRequest {
String itemId;
String cRaterClientId;

@Getter
String cRaterUrl;

public String generateBodyData() throws JSONException {
JSONObject body = new JSONObject();
body.put("client_id", cRaterClientId);
body.put("item_id", itemId);
return body.toString();
}

public boolean forBerkeleyEndpoint() {
return itemId.length() > 9 && itemId.substring(0, 9).equals("berkeley_");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ public interface CRaterRequest {

String getCRaterUrl();

void setCRaterUrl(String cRaterUrl);

String generateBodyData() throws JSONException;

boolean forBerkeleyEndpoint();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,16 @@
import org.json.JSONObject;
import org.wise.portal.presentation.util.http.Base64;

import lombok.Getter;
import lombok.Setter;

@Setter
public class CRaterScoringRequest implements CRaterRequest {
String itemId;
public class CRaterScoringRequest extends AbstractCRaterRequest {
String responseId;
String responseText;
String cRaterClientId;

@Getter
String cRaterUrl;

public String generateBodyData() throws JSONException {
JSONObject body = new JSONObject();
body.put("client_id", cRaterClientId);
JSONObject body = new JSONObject(super.generateBodyData());
body.put("service", "ScoringService");
body.put("item_id", itemId);
JSONArray responses = new JSONArray();
JSONObject response = new JSONObject();
response.put("response_id", responseId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class CRaterService {
*/
public String getScoringResponse(CRaterScoringRequest request) throws JSONException {
request.setCRaterClientId(appProperties.getProperty("cRater_client_id"));
request.setCRaterUrl(appProperties.getProperty("cRater_scoring_url"));
setRequestCRaterUrl(request, "cRater_scoring_url");
return post(request);
}

Expand All @@ -71,10 +71,15 @@ public String getScoringResponse(CRaterScoringRequest request) throws JSONExcept
*/
public String getVerificationResponse(CRaterVerificationRequest request) throws JSONException {
request.setCRaterClientId(appProperties.getProperty("cRater_client_id"));
request.setCRaterUrl(appProperties.getProperty("cRater_verification_url"));
setRequestCRaterUrl(request, "cRater_verification_url");
return post(request);
}

private void setRequestCRaterUrl(CRaterRequest request, String baseUrl) {
request.setCRaterUrl(appProperties.getProperty(
request.forBerkeleyEndpoint() ? "berkeley_" + baseUrl : baseUrl));
}

/**
* POSTs a CRater Request to the CRater Servlet and returns the CRater response string
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,10 @@
import org.json.JSONException;
import org.json.JSONObject;

import lombok.Getter;
import lombok.Setter;

@Setter
public class CRaterVerificationRequest implements CRaterRequest {
String itemId;
String cRaterClientId;

@Getter
String cRaterUrl;

public String generateBodyData() throws JSONException {
JSONObject body = new JSONObject();
body.put("client_id", cRaterClientId);
body.put("service", "VerificationService");
body.put("item_id", itemId);
return body.toString();
}
public class CRaterVerificationRequest extends AbstractCRaterRequest {
public String generateBodyData() throws JSONException {
JSONObject body = new JSONObject(super.generateBodyData());
body.put("service", "VerificationService");
return body.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class CRaterServiceTest {
private String password = "abc123";
private String scoringUrl = "https://test.org/score";
private String verifyUrl = "https://test.org/verify";
private String berkeleyScoringUrl = "https://test.org/score/berkeley";
private String berkeleyVerifyUrl = "https://test.org/verify/berkeley";

@Before
public void before() {
Expand Down Expand Up @@ -53,4 +55,26 @@ public void getVerificationResponse_ShouldGetCRaterProperties() throws JSONExcep
cRaterService.getVerificationResponse(request);
verify(appProperties);
}

@Test
public void getBerkeleyScoringResponse_ShouldGetCRaterProperties() throws JSONException {
CRaterScoringRequest request = new CRaterScoringRequest();
request.setItemId("berkeley_" + itemId);
request.setResponseId("1234567890");
request.setResponseText("hello");
expect(appProperties.getProperty("berkeley_cRater_scoring_url")).andReturn(berkeleyScoringUrl);
replay(appProperties);
cRaterService.getScoringResponse(request);
verify(appProperties);
}

@Test
public void getBerkeleyVerificationResponse_ShouldGetCRaterProperties() throws JSONException {
CRaterVerificationRequest request = new CRaterVerificationRequest();
request.setItemId("berkeley_" + itemId);
expect(appProperties.getProperty("berkeley_cRater_verification_url")).andReturn(berkeleyVerifyUrl);
replay(appProperties);
cRaterService.getVerificationResponse(request);
verify(appProperties);
}
}