Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Test cases #9

Merged
merged 15 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.9.1"
distribution-version = "2201.9.0"

[[package]]
org = "ballerina"
Expand Down Expand Up @@ -147,6 +147,15 @@ dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "lang.error"
version = "0.0.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "lang.int"
Expand Down Expand Up @@ -205,6 +214,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 @@ -248,6 +260,9 @@ dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"}
]
modules = [
{org = "ballerina", packageName = "os", moduleName = "os"}
]

[[package]]
org = "ballerina"
Expand All @@ -258,6 +273,20 @@ dependencies = [
{org = "ballerina", name = "time"}
]

[[package]]
org = "ballerina"
name = "test"
version = "0.0.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.array"},
{org = "ballerina", name = "lang.error"}
]
modules = [
{org = "ballerina", packageName = "test", moduleName = "test"}
]

[[package]]
org = "ballerina"
name = "time"
Expand Down Expand Up @@ -296,7 +325,10 @@ version = "0.1.0"
dependencies = [
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "http"},
{org = "ballerina", name = "log"},
{org = "ballerina", name = "mime"},
{org = "ballerina", name = "os"},
{org = "ballerina", name = "test"},
{org = "ballerina", name = "url"},
{org = "ballerinai", name = "observe"}
]
Expand Down
61 changes: 61 additions & 0 deletions ballerina/tests/mock_server.bal
HussainLatiff marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) 2024 WSO2 LLC. (http://www.wso2.org).
//
// WSO2 LLC. 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.

import ballerina/http;
import ballerina/log;

listener http:Listener httpServer = new(9090);


http:Service mockService = service object {

resource function get users/[string user_id]() returns UserResponse|http:Response {
UserResponse response = {
id: "688069266636800112",
username: "exampleUser",
discriminator: "1234",
public_flags: 0,
flags: 1,
bot: false,
system: false,
avatar: "avatar_url",
banner: "banner_url",
accent_color: 16777215,
global_name: "globalExampleUser"
};
return response;
}
resource function get channels/[string channel_id]/invites() returns anydata {
anydata response = [1, "string", true];
return response;
}

resource function get channels/[string channel_id]/webhooks() returns anydata {
anydata response = [1, "string", true];
return response;
}

};

function init() returns error? {
if isLiveServer {
log:printInfo("Skiping mock server initialization as the tests are running on live server");
return;
}
log:printInfo("Initiating mock server");
check httpServer.attach(mockService, "/");
check httpServer.'start();
}
48 changes: 48 additions & 0 deletions ballerina/tests/tests.bal
HussainLatiff marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2024 WSO2 LLC. (http://www.wso2.org).
//
// WSO2 LLC. 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.

import ballerina/test;
import ballerina/os;

configurable boolean isLiveServer = os:getEnv("IS_LIVE_SERVER") == "true";
configurable string CliId = isLiveServer ? os:getEnv("Client_ID") : "test";
configurable string CliScre = isLiveServer ? os:getEnv("Client_Secret") : "test";
HussainLatiff marked this conversation as resolved.
Show resolved Hide resolved
configurable string serviceUrl = isLiveServer ? os:getEnv("DISCORD_URL") : "http://localhost:9090/";
ConnectionConfig config = {auth:{
token: "BearerToken"
}};
final Client discord = check new Client(config, serviceUrl);

@test:Config{}
function testGetUser() returns error? {
string user_id = "User Id";
HussainLatiff marked this conversation as resolved.
Show resolved Hide resolved
UserResponse getUserInfo = check discord->/users/[user_id]();
test:assertTrue((getUserInfo.length()> 0));
HussainLatiff marked this conversation as resolved.
Show resolved Hide resolved
}

@test:Config{}
function testGetVoiceChannel() returns error? {
string channel_id = "Voice Channel ID";
anydata getVoiceChannelInfo = check discord->/channels/[channel_id]/invites();
test:assertTrue((getVoiceChannelInfo.count()> 0));
}

@test:Config{}
function testGetWebhook() returns error? {
string channel_id = "Channel ID";
HussainLatiff marked this conversation as resolved.
Show resolved Hide resolved
anydata getWebhookInfo = check discord->/channels/[channel_id]/webhooks();
test:assertTrue((getWebhookInfo.count()> 0));
}
2 changes: 1 addition & 1 deletion ballerina/types.bal
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,7 @@ public type UserResponse record {
string? avatar?;
string discriminator;
int:Signed32 public_flags;
@constraint:Int {minValue: 1, maxValue: -1}
@constraint:Int {minValue: -1, maxValue: 1}
int flags;
boolean? bot?;
boolean? system?;
Expand Down
Loading