Skip to content

Commit

Permalink
Merge pull request #42 from ballerina-platform/kaneel-dev
Browse files Browse the repository at this point in the history
Enable test cases
  • Loading branch information
kaneeldias authored Nov 3, 2023
2 parents 25f1df0 + 823514c commit d459a07
Show file tree
Hide file tree
Showing 16 changed files with 238 additions and 119 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build-with-bal-test-native.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ jobs:
ACCESS_KEY_ID: ${{ secrets.ACCESS_KEY_ID }}
SECRET_ACCESS_KEY: ${{ secrets.SECRET_ACCESS_KEY }}
REGION: ${{ secrets.REGION }}
AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }}
ADM_CLIENT_ID: ${{ secrets.ADM_CLIENT_ID }}
ADM_CLIENT_SECRET: ${{ secrets.ADM_CLIENT_SECRET }}
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ jobs:
ACCESS_KEY_ID: ${{ secrets.ACCESS_KEY_ID }}
SECRET_ACCESS_KEY: ${{ secrets.SECRET_ACCESS_KEY }}
REGION: ${{ secrets.REGION }}
AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }}
ADM_CLIENT_ID: ${{ secrets.ADM_CLIENT_ID }}
ADM_CLIENT_SECRET: ${{ secrets.ADM_CLIENT_SECRET }}

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/daily-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ jobs:
ACCESS_KEY_ID: ${{ secrets.ACCESS_KEY_ID }}
SECRET_ACCESS_KEY: ${{ secrets.SECRET_ACCESS_KEY }}
REGION: ${{ secrets.REGION }}
AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }}
ADM_CLIENT_ID: ${{ secrets.ADM_CLIENT_ID }}
ADM_CLIENT_SECRET: ${{ secrets.ADM_CLIENT_SECRET }}

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
Expand Down
21 changes: 21 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,26 @@ jobs:
env:
JAVA_HOME: /usr/lib/jvm/default-jvm

# # Build Module Examples
# - name: Ballerina Examples Build
# run: chmod +x ./examples/build.sh && ./examples/build.sh build
# env:
# JAVA_HOME: /usr/lib/jvm/default-jvm

# Test Ballerina Project
- name: Ballerina Test
# tests will be skipped if the PR is from a forked repository (as the secrets are not available)
if: ${{ github.event.pull_request.head.repo.full_name == github.repository }}
run: bal test ./ballerina --test-report --code-coverage --coverage-format=xml
env:
JAVA_HOME: /usr/lib/jvm/default-jvm
ACCESS_KEY_ID: ${{ secrets.ACCESS_KEY_ID }}
SECRET_ACCESS_KEY: ${{ secrets.SECRET_ACCESS_KEY }}
REGION: ${{ secrets.REGION }}
TEST_IAM_ROLE: ${{ secrets.TEST_IAM_ROLE }}
AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }}
ADM_CLIENT_ID: ${{ secrets.ADM_CLIENT_ID }}
ADM_CLIENT_SECRET: ${{ secrets.ADM_CLIENT_SECRET }}

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
8 changes: 8 additions & 0 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ dependencies = [
{org = "ballerina", name = "lang.value"},
{org = "ballerina", name = "observe"}
]
modules = [
{org = "ballerina", packageName = "log", moduleName = "log"}
]

[[package]]
org = "ballerina"
Expand Down Expand Up @@ -283,6 +286,9 @@ dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"}
]
modules = [
{org = "ballerina", packageName = "os", moduleName = "os"}
]

[[package]]
org = "ballerina"
Expand Down Expand Up @@ -354,7 +360,9 @@ dependencies = [
{org = "ballerina", name = "lang.int"},
{org = "ballerina", name = "lang.regexp"},
{org = "ballerina", name = "lang.runtime"},
{org = "ballerina", name = "log"},
{org = "ballerina", name = "mime"},
{org = "ballerina", name = "os"},
{org = "ballerina", name = "random"},
{org = "ballerina", name = "test"},
{org = "ballerina", name = "time"},
Expand Down
38 changes: 15 additions & 23 deletions ballerina/tests/endpoint-tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,19 @@

import ballerina/test;

string applicationArn = "";

@test:BeforeGroups {value: ["endpoint"]}
function beforeEndpointTests() returns error? {
applicationArn = check amazonSNSClient->createPlatformApplication(testRunId + "FirebasePlatformApplication",
FIREBASE_CLOUD_MESSAGING, auth = {platformCredential: firebaseServerKey});
}

@test:Config {
groups: ["endpoint"]
}
function createEndpointTest() returns error? {
string arn = check amazonSNSClient->createEndpoint(applicationArn, testRunId + "testDeviceToken");
string arn = check amazonSNSClient->createEndpoint(testApplication, testRunId + "testDeviceToken");
test:assertTrue(isArn(arn));
}

@test:Config {
groups: ["endpoint"]
}
function createEndpointWithEmptyTokenTest() returns error? {
string|Error arn = amazonSNSClient->createEndpoint(applicationArn, "");
string|Error arn = amazonSNSClient->createEndpoint(testApplication, "");
test:assertTrue(arn is OperationError);
test:assertEquals((<OperationError>arn).message(), "Invalid parameter: Token Reason: cannot be empty");
}
Expand All @@ -47,14 +39,14 @@ function createEndpointWithEmptyTokenTest() returns error? {
function createEndpointWithInvalidArnTest() returns error? {
string|Error arn = amazonSNSClient->createEndpoint(invalidApplicationArn, testRunId + "testDeviceToken2");
test:assertTrue(arn is OperationError);
test:assertEquals((<OperationError>arn).message(), "Invalid parameter: PlatformApplicationArn Reason: Wrong number of slashes in relative portion of the ARN.");
test:assertEquals((<OperationError>arn).message(), "Invalid parameter: PlatformApplicationArn Reason: An ARN must have at least 6 elements, not 1");
}

@test:Config {
groups: ["endpoint"]
}
function createEndpointWithCustomUserDataTest() returns error? {
string arn = check amazonSNSClient->createEndpoint(applicationArn, testRunId + "testDeviceToken3",
string arn = check amazonSNSClient->createEndpoint(testApplication, testRunId + "testDeviceToken3",
customUserData = "testCustomUserData");
test:assertTrue(isArn(arn));
}
Expand All @@ -64,7 +56,7 @@ function createEndpointWithCustomUserDataTest() returns error? {
}
function createEndpointWithAttributes() returns error? {
EndpointAttributes attributes = {enabled: true, token: testRunId + "testToken4", customUserData: "testCustomUserData2"};
string arn = check amazonSNSClient->createEndpoint(applicationArn, testRunId + "testDeviceToken4", attributes);
string arn = check amazonSNSClient->createEndpoint(testApplication, testRunId + "testDeviceToken4", attributes);
test:assertTrue(isArn(arn));
}

Expand All @@ -73,7 +65,7 @@ function createEndpointWithAttributes() returns error? {
}
function createEndpointWithInvalidAttributes() returns error? {
EndpointAttributes attributes = {enabled: true, token: "", customUserData: "testCustomUserData2"};
string|Error arn = amazonSNSClient->createEndpoint(applicationArn, testRunId + "testDeviceToken5", attributes);
string|Error arn = amazonSNSClient->createEndpoint(testApplication, testRunId + "testDeviceToken5", attributes);
test:assertTrue(arn is OperationError);
test:assertEquals((<OperationError>arn).message(), "Invalid parameter: Attributes Reason: Invalid value for attribute: Token: cannot be empty");
}
Expand All @@ -83,15 +75,15 @@ function createEndpointWithInvalidAttributes() returns error? {
}
function listPlatformApplicationEndpointsTest() returns error? {
EndpointAttributes attributes = {enabled: false};
string arn = check amazonSNSClient->createEndpoint(applicationArn, testRunId + "testDeviceTokenNew", attributes,
string arn = check amazonSNSClient->createEndpoint(testApplication, testRunId + "testDeviceTokenNew", attributes,
"CustomDataNew");

foreach int i in 0...100 {
_ = check amazonSNSClient->createEndpoint(applicationArn, testRunId + "testDeviceToken0" + i.toString());
_ = check amazonSNSClient->createEndpoint(testApplication, testRunId + "testDeviceToken0" + i.toString());
}

stream<Endpoint, Error?> endpointStream =
amazonSNSClient->listEndpoints(applicationArn);
amazonSNSClient->listEndpoints(testApplication);
Endpoint[] endpoints = check from Endpoint endpoint in endpointStream
select endpoint;

Expand All @@ -111,7 +103,7 @@ function listPlatformApplicationEndpointsTest() returns error? {
}
function getEndpointAttributesTest() returns error? {
EndpointAttributes attributes = {enabled: false};
string arn = check amazonSNSClient->createEndpoint(applicationArn, testRunId + "testDeviceToken7", attributes,
string arn = check amazonSNSClient->createEndpoint(testApplication, testRunId + "testDeviceToken7", attributes,
"CustomData7");

EndpointAttributes retrievedAttributes = check amazonSNSClient->getEndpointAttributes(arn);
Expand All @@ -125,7 +117,7 @@ function getEndpointAttributesTest() returns error? {
}
function getEndpointAttributesWithInvalidArnTest() returns error? {
EndpointAttributes attributes = {enabled: false};
string arn = check amazonSNSClient->createEndpoint(applicationArn, testRunId + "testDeviceToken7", attributes,
string arn = check amazonSNSClient->createEndpoint(testApplication, testRunId + "testDeviceToken7", attributes,
"CustomData7");

EndpointAttributes|Error retrievedAttributes = amazonSNSClient->getEndpointAttributes(arn + "invalid");
Expand All @@ -137,7 +129,7 @@ function getEndpointAttributesWithInvalidArnTest() returns error? {
groups: ["endpoint"]
}
function setEndpointAttributesTest() returns error? {
string arn = check amazonSNSClient->createEndpoint(applicationArn, testRunId + "testDeviceToken8");
string arn = check amazonSNSClient->createEndpoint(testApplication, testRunId + "testDeviceToken8");
_ = check amazonSNSClient->setEndpointAttributes(arn, {
enabled: false, token: testRunId + "testDeviceToken9", customUserData: "CustomData9"
});
Expand All @@ -152,7 +144,7 @@ function setEndpointAttributesTest() returns error? {
groups: ["endpoint"]
}
function setEndpointAttributesWithInvalidArnTest() returns error? {
string arn = check amazonSNSClient->createEndpoint(applicationArn, testRunId + "testDeviceToken10");
string arn = check amazonSNSClient->createEndpoint(testApplication, testRunId + "testDeviceToken10");
Error? e = amazonSNSClient->setEndpointAttributes(arn + "invalid", {
enabled: false,
token: testRunId + "testDeviceToken10",
Expand All @@ -167,7 +159,7 @@ function setEndpointAttributesWithInvalidArnTest() returns error? {
groups: ["endpoin"]
}
function deleteEndpointTest() returns error? {
string arn = check amazonSNSClient->createEndpoint(applicationArn, testRunId + "testDeviceToken11");
string arn = check amazonSNSClient->createEndpoint(testApplication, testRunId + "testDeviceToken11");
_ = check amazonSNSClient->deleteEndpoint(arn);

EndpointAttributes|Error attributes = amazonSNSClient->getEndpointAttributes(arn);
Expand All @@ -179,7 +171,7 @@ function deleteEndpointTest() returns error? {
groups: ["endpoint"]
}
function deleteEndpointWithInvalidTest() returns error? {
string arn = check amazonSNSClient->createEndpoint(applicationArn, testRunId + "testDeviceToken11");
string arn = check amazonSNSClient->createEndpoint(testApplication, testRunId + "testDeviceToken11");
Error? e = amazonSNSClient->deleteEndpoint(arn + "invalid");

test:assertTrue(e is OperationError);
Expand Down
88 changes: 74 additions & 14 deletions ballerina/tests/init.bal
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,90 @@

import ballerina/time;
import ballerina/lang.regexp;
import ballerina/test;
import ballerina/log;
import ballerina/os;

configurable string accessKeyId = ?;
configurable string secretAccessKey = ?;
configurable string region = ?;
string testRunId = regexp:replaceAll(re `[:.]`, time:utcToString(time:utcNow()), "");

configurable string accessKeyId = os:getEnv("ACCESS_KEY_ID");
configurable string secretAccessKey = os:getEnv("SECRET_ACCESS_KEY");
configurable string region = os:getEnv("REGION");

ConnectionConfig config = {
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
region: region
region: region,
retryConfig: {
count: 3,
interval: 10
}
};

Client amazonSNSClient = check new(config);

string testRunId = regexp:replaceAll(re `[:.]`, time:utcToString(time:utcNow()), "");
string testHttp = "http://www.wso2.com";
string testHttps = "https://www.wso2.com";
string testEmail = "[email protected]";
string testPhoneNumber = "+94123456789";
string testApplication = "";
string testEndpoint = "";

configurable string admClientId = os:getEnv("ADM_CLIENT_ID");
configurable string admClientSecret = os:getEnv("ADM_CLIENT_SECRET");
configurable string testIamRole = os:getEnv("TEST_IAM_ROLE");
configurable string testAwsAccountId = os:getEnv("AWS_ACCOUNT_ID");

@test:BeforeSuite
function resetEnvironment() returns error? {
check deleteAllTopics();
check deleteAllPlatformApplications();
check deleteAllSubscriptions();
check initializeTestVariables();
}

// Existing topics need to be deleted as SNS imposes a limit on the # of topics an account may have
function deleteAllTopics() returns error? {
stream<string, Error?> topicStream = amazonSNSClient->listTopics();
string[] topicArns = check from string topicArn in topicStream select topicArn;

foreach int i in 1...topicArns.length() {
check amazonSNSClient->deleteTopic(topicArns[i-1]);
log:printInfo(i.toString() + "/" + topicArns.length().toString() + " topics deleted - " + topicArns[i-1]);
}
}

// Existing applications need to be deleted as SNS imposes a limit on the # of applications an account may have
function deleteAllPlatformApplications() returns error? {
stream<PlatformApplication, Error?> applicationStream = amazonSNSClient->listPlatformApplications();
string[] applicationArns = check from PlatformApplication application in applicationStream
select application.platformApplicationArn;

foreach int i in 1...applicationArns.length() {
check amazonSNSClient->deletePlatformApplication(applicationArns[i-1]);
log:printInfo(i.toString() + "/" + applicationArns.length().toString() + " platform applications deleted");
}
}

// Existing subscriptions need to be deleted as SNS imposes a limit on the # of subscriptions an account may have
function deleteAllSubscriptions() returns error? {
stream<Subscription, Error?> subscriptionStream = amazonSNSClient->listSubscriptions();
string[] subscriptionArns = check from Subscription subscription in subscriptionStream
where subscription.subscriptionArn != "PendingConfirmation"
select subscription.subscriptionArn;

foreach int i in 1...subscriptionArns.length() {
check amazonSNSClient->unsubscribe(subscriptionArns[i-1]);
log:printInfo(i.toString() + "/" + subscriptionArns.length().toString() + " subscriptions deleted");
}
}

function initializeTestVariables() returns error? {
testApplication = check amazonSNSClient->createPlatformApplication(testRunId + "AmazonPlatformApplication",
AMAZON_DEVICE_MESSAGING, auth = {platformCredential: admClientSecret, platformPrincipal: admClientId});
testEndpoint = check amazonSNSClient->createEndpoint(testApplication, testRunId + "testDeviceToken");
}

configurable string testHttp = ?;
configurable string testHttps = ?;
configurable string testEmail = ?;
configurable string testPhoneNumber = ?;
configurable string testApplication = ?;
configurable string testIamRole = ?;
configurable string testAwsAccountId = ?;
configurable string testEndpoint = ?;

function isArn(string arn) returns boolean {
return arn.startsWith("arn:aws:");
}
2 changes: 1 addition & 1 deletion ballerina/tests/phone-number-tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function listOrignationPhoneNumbersTest() returns error? {
stream<OriginationPhoneNumber, Error?> phoneNumberStream = amazonSNSClient->listOriginationNumbers();
OriginationPhoneNumber[] phoneNumbers = check from OriginationPhoneNumber phoneNumber in phoneNumberStream
select phoneNumber;
test:assertEquals(phoneNumbers.length(), 1, "Invalid number of origination phone numbers");
test:assertEquals(phoneNumbers.length(), 0, "Invalid number of origination phone numbers");
}

@test:Config {
Expand Down
Loading

0 comments on commit d459a07

Please sign in to comment.