Skip to content

Commit

Permalink
Merge pull request #14 from project-sunbird/vinaya-ft
Browse files Browse the repository at this point in the history
Issue #SB-3647 feat: Added negative test scenarios for create issuer API
  • Loading branch information
manzarul authored Jul 6, 2018
2 parents 2d75dde + 11cec2c commit a350545
Show file tree
Hide file tree
Showing 17 changed files with 225 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,34 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;
import org.sunbird.common.util.HttpUtil;
import org.sunbird.integration.test.user.EndpointConfig.TestGlobalProperty;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class CreateIssuerTest extends TestNGCitrusTestDesigner {
import org.sunbird.common.util.HttpUtil;
import org.sunbird.integration.test.common.BaseCitrusTest;
import org.sunbird.integration.test.user.EndpointConfig.TestGlobalProperty;


public class CreateIssuerTest extends BaseCitrusTest {

public static final String TEST_NAME_CREATE_ISSUER_SUCCESS = "createIssuerSuccess";
public static final String TEST_NAME_CREATE_ISSUER_WITH_IMAGE_SUCCESS = "createIssuerWithImageSuccess";
public static final String TEST_NAME_CREATE_ISSUER_SUCCESS_WITH_IMAGE = "createIssuerSuccessWithImage";
public static final String TEST_NAME_CREATE_ISSUER_FAILURE_WITHOUT_NAME = "createIssuerFailureWithoutName";
public static final String TEST_NAME_CREATE_ISSUER_FAILURE_WITHOUT_DESCRIPTION = "createIssuerFailureWithoutDescription";
public static final String TEST_NAME_CREATE_ISSUER_FAILURE_WITHOUT_URL = "createIssuerFailureWithoutUrl";
public static final String TEST_NAME_CREATE_ISSUER_FAILURE_WITH_INVALID_URL = "createIssuerFailureWithInvalidUrl";
public static final String TEST_NAME_CREATE_ISSUER_FAILURE_WITHOUT_EMAIL = "createIssuerFailureWithoutEmail";
public static final String TEST_NAME_CREATE_ISSUER_FAILURE_WITH_INVALID_EMAIL = "createIssuerFailureWithInvalidEmail";

public static final String TEMPLATE_DIR = "templates/badge/issuer/create";
public static final String TEST_DIR_CREATE_ISSUER_SUCCESS = MessageFormat.format("{0}/{1}/", TEMPLATE_DIR, TEST_NAME_CREATE_ISSUER_SUCCESS);
public static final String TEST_DIR_CREATE_ISSUER_WITH_IMAGE_SUCCESS = MessageFormat.format("{0}/{1}/", TEMPLATE_DIR, TEST_NAME_CREATE_ISSUER_WITH_IMAGE_SUCCESS);
public static final String TEST_DIR_CREATE_ISSUER_SUCCESS_WITH_IMAGE = MessageFormat.format("{0}/{1}/", TEMPLATE_DIR, TEST_NAME_CREATE_ISSUER_SUCCESS_WITH_IMAGE);
public static final String TEST_DIR_CREATE_ISSUER_FAILURE_WITHOUT_NAME = MessageFormat.format("{0}/{1}/", TEMPLATE_DIR, TEST_NAME_CREATE_ISSUER_FAILURE_WITHOUT_NAME);
public static final String TEST_DIR_CREATE_ISSUER_FAILURE_WITHOUT_DESCRIPTION = MessageFormat.format("{0}/{1}/", TEMPLATE_DIR, TEST_NAME_CREATE_ISSUER_FAILURE_WITHOUT_DESCRIPTION);
public static final String TEST_DIR_CREATE_ISSUER_FAILURE_WITHOUT_URL = MessageFormat.format("{0}/{1}/", TEMPLATE_DIR, TEST_NAME_CREATE_ISSUER_FAILURE_WITHOUT_URL);
public static final String TEST_DIR_CREATE_ISSUER_FAILURE_WITH_INVALID_URL = MessageFormat.format("{0}/{1}/", TEMPLATE_DIR, TEST_NAME_CREATE_ISSUER_FAILURE_WITH_INVALID_URL);
public static final String TEST_DIR_CREATE_ISSUER_FAILURE_WITHOUT_EMAIL = MessageFormat.format("{0}/{1}/", TEMPLATE_DIR, TEST_NAME_CREATE_ISSUER_FAILURE_WITHOUT_EMAIL);
public static final String TEST_DIR_CREATE_ISSUER_FAILURE_WITH_INVALID_EMAIL = MessageFormat.format("{0}/{1}/", TEMPLATE_DIR, TEST_NAME_CREATE_ISSUER_FAILURE_WITH_INVALID_EMAIL);

public static final String REQUEST_FORM_DATA = "request.params";
public static final String RESPONSE_JSON = "response.json";
Expand All @@ -32,60 +47,94 @@ public class CreateIssuerTest extends TestNGCitrusTestDesigner {
@Autowired private TestGlobalProperty initGlobalValues;

private String getCreateIssuerUrl() {
System.out.println("initGlobalValues = " + initGlobalValues);
return initGlobalValues.getLmsUrl().contains("localhost")
? "/v1/issuer/create"
: "/api/badging/v1/issuer/create";
}

private void performTest(
String testName,
HttpClient httpClient,
TestGlobalProperty config,
String url,
String requestFormData,
String responseJson) {
System.out.println(requestFormData);

getTestCase().setName(testName);

String testFolderPath = MessageFormat.format("{0}/{1}", TEMPLATE_DIR, testName);

new HttpUtil().multipartPost(http().client(httpClient), config, url, requestFormData, testFolderPath);

http()
.client(httpClient)
.receive()
.response(HttpStatus.OK)
.payload(new ClassPathResource(responseJson));
}

@DataProvider(name = "createIssuerDataProvider")
public Object[][] createIssuerDataProvider() {
@DataProvider(name = "createIssuerDataProviderSuccess")
public Object[][] createIssuerDataProviderSuccess() {
return new Object[][] {
new Object[]{
TEST_DIR_CREATE_ISSUER_SUCCESS + REQUEST_FORM_DATA,
TEST_DIR_CREATE_ISSUER_SUCCESS + RESPONSE_JSON,
TEST_NAME_CREATE_ISSUER_SUCCESS
},
new Object[]{
TEST_DIR_CREATE_ISSUER_WITH_IMAGE_SUCCESS + REQUEST_FORM_DATA,
TEST_DIR_CREATE_ISSUER_WITH_IMAGE_SUCCESS + RESPONSE_JSON,
TEST_NAME_CREATE_ISSUER_WITH_IMAGE_SUCCESS
TEST_DIR_CREATE_ISSUER_SUCCESS_WITH_IMAGE + REQUEST_FORM_DATA,
TEST_DIR_CREATE_ISSUER_SUCCESS_WITH_IMAGE + RESPONSE_JSON,
TEST_NAME_CREATE_ISSUER_SUCCESS_WITH_IMAGE
}
};
}

@Test(dataProvider = "createIssuerDataProvider")
@DataProvider(name = "createIssuerDataProviderFailure")
public Object[][] createIssuerDataProviderFailure() {
return new Object[][] {
new Object[]{
TEST_DIR_CREATE_ISSUER_FAILURE_WITHOUT_NAME + REQUEST_FORM_DATA,
TEST_DIR_CREATE_ISSUER_FAILURE_WITHOUT_NAME + RESPONSE_JSON,
TEST_NAME_CREATE_ISSUER_FAILURE_WITHOUT_NAME
},
new Object[]{
TEST_DIR_CREATE_ISSUER_FAILURE_WITHOUT_DESCRIPTION + REQUEST_FORM_DATA,
TEST_DIR_CREATE_ISSUER_FAILURE_WITHOUT_DESCRIPTION + RESPONSE_JSON,
TEST_NAME_CREATE_ISSUER_FAILURE_WITHOUT_DESCRIPTION
},
new Object[]{
TEST_DIR_CREATE_ISSUER_FAILURE_WITHOUT_URL + REQUEST_FORM_DATA,
TEST_DIR_CREATE_ISSUER_FAILURE_WITHOUT_URL + RESPONSE_JSON,
TEST_NAME_CREATE_ISSUER_FAILURE_WITHOUT_URL
},
new Object[]{
TEST_DIR_CREATE_ISSUER_FAILURE_WITH_INVALID_URL + REQUEST_FORM_DATA,
TEST_DIR_CREATE_ISSUER_FAILURE_WITH_INVALID_URL + RESPONSE_JSON,
TEST_NAME_CREATE_ISSUER_FAILURE_WITH_INVALID_URL
},
new Object[]{
TEST_DIR_CREATE_ISSUER_FAILURE_WITHOUT_EMAIL + REQUEST_FORM_DATA,
TEST_DIR_CREATE_ISSUER_FAILURE_WITHOUT_EMAIL + RESPONSE_JSON,
TEST_NAME_CREATE_ISSUER_FAILURE_WITHOUT_EMAIL
},
new Object[]{
TEST_DIR_CREATE_ISSUER_FAILURE_WITH_INVALID_EMAIL + REQUEST_FORM_DATA,
TEST_DIR_CREATE_ISSUER_FAILURE_WITH_INVALID_EMAIL + RESPONSE_JSON,
TEST_NAME_CREATE_ISSUER_FAILURE_WITH_INVALID_EMAIL
}
};
}

@Test(dataProvider = "createIssuerDataProviderSuccess")
@CitrusParameters({"requestFormData", "responseJson", "testName"})
@CitrusTest
public void testCreateIssuerSuccess(String requestFormData, String responseJson, String testName) {
System.out.println("initGlobalValues = " + initGlobalValues);
performMultipartTest(
testName,
TEMPLATE_DIR,
restTestClient,
initGlobalValues,
getCreateIssuerUrl(),
requestFormData,
responseJson,
HttpStatus.OK);
}

@Test(dataProvider = "createIssuerDataProviderFailure")
@CitrusParameters({"requestFormData", "responseJson", "testName"})
@CitrusTest
public void testCreateIssuer(String requestFormData, String responseJson, String testName) {
public void testCreateIssuerFailure(String requestFormData, String responseJson, String testName) {
System.out.println("initGlobalValues = " + initGlobalValues);
performTest(
testName,
restTestClient,
initGlobalValues,
getCreateIssuerUrl(),
requestFormData,
responseJson);
performMultipartTest(
testName,
TEMPLATE_DIR,
restTestClient,
initGlobalValues,
getCreateIssuerUrl(),
requestFormData,
responseJson,
HttpStatus.BAD_REQUEST);
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package org.sunbird.integration.test.common;

import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;
import com.consol.citrus.http.client.HttpClient;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;
import org.sunbird.common.util.HttpUtil;
import org.sunbird.integration.test.user.EndpointConfig.TestGlobalProperty;

/**
* Class to contain the common things for all citrus tests.
Expand All @@ -15,4 +21,29 @@ public class BaseCitrusTest extends TestNGCitrusTestDesigner {
public static Map<String,List<String>> toDeleteCassandraRecordsMap = new HashMap<String, List<String>>();
public static Map<String,List<String>> toDeleteEsRecordsMap = new HashMap<String, List<String>>();

public void performMultipartTest(
String testName,
String testTemplateDir,
HttpClient httpClient,
TestGlobalProperty config,
String url,
String requestFormData,
String responseJson,
HttpStatus responseCode) {
System.out.println(requestFormData);

getTestCase().setName(testName);

String testFolderPath = MessageFormat.format("{0}/{1}", testTemplateDir, testName);

new HttpUtil().multipartPost(http().client(httpClient), config, url, requestFormData, testFolderPath);

http()
.client(httpClient)
.receive()
.response(responseCode)
.payload(new ClassPathResource(responseJson));
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name=swarn
description=something
url=http://www.example.com
email=abc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"id": "api.issuer.create",
"ver": "v1",
"ts": "@ignore@",
"params": {
"resmsgid": null,
"msgid": "@ignore@",
"err": "EMAIL_FORMAT_ERROR",
"status": "EMAIL_FORMAT_ERROR",
"errmsg": "Email is invalid."
},
"responseCode": "CLIENT_ERROR",
"result": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name=swarn
description=something
url=abc
[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"id": "api.issuer.create",
"ver": "v1",
"ts": "@ignore@",
"params": {
"resmsgid": null,
"msgid": "@ignore@",
"err": "URL_FORMAT_ERROR",
"status": "URL_FORMAT_ERROR",
"errmsg": "URL is invalid."
},
"responseCode": "CLIENT_ERROR",
"result": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name=swarn
url=http://www.example.com
[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"id": "api.issuer.create",
"ver": "v1",
"ts": "@ignore@",
"params": {
"resmsgid": null,
"msgid": "@ignore@",
"err": "MANDATORY_PARAMETER_MISSING",
"status": "MANDATORY_PARAMETER_MISSING",
"errmsg": "Mandatory parameter description is missing."
},
"responseCode": "CLIENT_ERROR",
"result": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name=swarn
description=something
url=http://www.example.com
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"id": "api.issuer.create",
"ver": "v1",
"ts": "@ignore@",
"params": {
"resmsgid": null,
"msgid": "@ignore@",
"err": "MANDATORY_PARAMETER_MISSING",
"status": "MANDATORY_PARAMETER_MISSING",
"errmsg": "Mandatory parameter email is missing."
},
"responseCode": "CLIENT_ERROR",
"result": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
description=something
url=http://www.example.com
[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"id": "api.issuer.create",
"ver": "v1",
"ts": "@ignore@",
"params": {
"resmsgid": null,
"msgid": "@ignore@",
"err": "MANDATORY_PARAMETER_MISSING",
"status": "MANDATORY_PARAMETER_MISSING",
"errmsg": "Mandatory parameter name is missing."
},
"responseCode": "CLIENT_ERROR",
"result": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name=swarn
description=something
[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"id": "api.issuer.create",
"ver": "v1",
"ts": "@ignore@",
"params": {
"resmsgid": null,
"msgid": "@ignore@",
"err": "MANDATORY_PARAMETER_MISSING",
"status": "MANDATORY_PARAMETER_MISSING",
"errmsg": "Mandatory parameter url is missing."
},
"responseCode": "CLIENT_ERROR",
"result": {}
}

0 comments on commit a350545

Please sign in to comment.