/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.opensocial.restful.tests;
import org.opensocial.restful.framework.HttpHelper;
import org.opensocial.restful.framework.JsonHelper;
import org.opensocial.restful.framework.TestEnvironment;
import org.opensocial.restful.framework.Validator;
import org.json.JSONException;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeSuite;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Map;
import java.util.logging.Logger;
/**
* Base class for all tests.
*/
public class RestfulBaseTest {
protected TestEnvironment env;
protected HttpHelper httpHelper;
protected static final Logger LOGGER = Logger.getAnonymousLogger();
@BeforeSuite
protected void setUp() throws IOException, JSONException {
env = new TestEnvironment("config.properties");
httpHelper = new HttpHelper();
}
/**
* Retrieves testUserId for testing {guid} related tests.
*/
protected String getTestUserId() throws IOException, JSONException {
String completeUrl = httpHelper.getCompleteUrl(env.getBaseUrl(),
"/people/@me/@self?fields=id&", env.getStPrefix(),
env.getSecurityToken());
String response = httpHelper.doHttpGet(completeUrl);
JSONObject responseObj = JsonHelper.toJSONObject(response);
if (responseObj != null) {
JSONObject entry = (JSONObject) responseObj.get("entry");
return (String) entry.get("id");
}
return null;
}
/**
* Retrieves testGroupId for testing {groupId} related tests.
*/
protected String getTestGroupId() throws IOException, JSONException {
String completeUrl = httpHelper.getCompleteUrl(env.getBaseUrl(),
"/groups/@me&", env.getStPrefix(), env.getSecurityToken());
String response = httpHelper.doHttpGet(completeUrl);
JSONObject responseObj = JsonHelper.toJSONObject(response);
if (responseObj != null) {
JSONObject entry = (JSONObject) responseObj.get("entry");
// TODO(shreyas): Return valid group id
return entry.toString();
}
return null;
}
/**
* Tests http get request for given url. Validates if response is valid
* JSON response.
*
* @param completeUrl url to hit
* @param isItemsPerPage true if itemstPerPage is expected in response
* @param isEntryAnArray true if entry should be object of JSONArray,
* false otherwise
*/
protected void testRestfulHttpGetRequest(String completeUrl,
boolean isItemsPerPage, boolean isEntryAnArray) {
String response = httpHelper.doHttpGet(completeUrl);
JSONObject responseObj = JsonHelper.toJSONObject(response);
if (responseObj != null) {
Validator.validateJSONResponse(responseObj, isItemsPerPage,
isEntryAnArray);
} else {
Assert.fail("In-valid JSON response received");
}
}
/**
* Tests http post request. Validates if response is valid
* JSON response.
*
* @param completeUrl url to hit
* @param dataMap data to be posted
* @param isItemsPerPage true if itemstPerPage is expected in response
* @param isEntryAnArray true if entry should be object of JSONArray,
* false otherwise
*/
protected void testRestfulHttpPostRequest(
String completeUrl, Map<String, String> dataMap, boolean isItemsPerPage,
boolean isEntryAnArray) {
String response = httpHelper.doHttpPost(completeUrl, dataMap);
JSONObject responseObj = JsonHelper.toJSONObject(response);
if (responseObj != null) {
Validator.validateJSONResponse(responseObj, isItemsPerPage,
isEntryAnArray);
} else {
Assert.fail("In-valid JSON response received");
}
}
/**
* Tests http put request. Validates if response is valid
* JSON response.
*
* @param completeUrl url to hit
* @param dataMap data to be updated
* @param isItemsPerPage true if itemstPerPage is expected in response
* @param isEntryAnArray true if entry should be object of JSONArray,
* false otherwise
*/
protected void testRestfulHttpPutRequest(
String completeUrl, Map<String, String> dataMap, boolean isItemsPerPage,
boolean isEntryAnArray)
throws UnsupportedEncodingException {
String response = httpHelper.doHttpPut(completeUrl, dataMap);
JSONObject responseObj = JsonHelper.toJSONObject(response);
if (responseObj != null) {
Validator.validateJSONResponse(responseObj, isItemsPerPage,
isEntryAnArray);
} else {
Assert.fail("In-valid JSON response received");
}
}
/**
* Tests http delete request. Validates if response is valid
* JSON response.
*
* @param completeUrl url to hit
* @param identifier entry to delete
* @param isItemsPerPage true if itemstPerPage is expected in response
* @param isEntryAnArray true if entry should be object of JSONArray,
* false otherwise
*/
protected void testRestfulHttpDeleteRequest(String completeUrl,
String identifier, boolean isItemsPerPage, boolean isEntryAnArray) {
String response = httpHelper.doHttpDelete(completeUrl, identifier);
JSONObject responseObj = JsonHelper.toJSONObject(response);
if (responseObj != null) {
Validator.validateJSONResponse(responseObj, isItemsPerPage,
isEntryAnArray);
} else {
Assert.fail("In-valid JSON response received");
}
}
/**
* Returns new string depending on system time.
*/
protected String getNewValue(String value) {
Calendar cal = Calendar.getInstance();
return value + cal.getTime().toString();
}
protected String getTestStartedBanner(String testName) {
return "===================== Test: " + testName + " started."
+ "===========================\n";
}
}