diff --git a/ballerina/tests/mock_server.bal b/ballerina/tests/mock_server.bal index 83775ac..a7cef73 100644 --- a/ballerina/tests/mock_server.bal +++ b/ballerina/tests/mock_server.bal @@ -17,14 +17,12 @@ import ballerina/http; import ballerina/log; -listener http:Listener httpServer = new(9090); +listener http:Listener httpServer = new (9090); +http:Service mockService = service object { -http:Service mockService = service object { - -resource function get users/[string user_id]() returns UserResponse|http:Response { - UserResponse response = { - id: "688069266636800112", + resource function get users/[string userId]() returns UserResponse|http:Response => { + id: userId, username: "exampleUser", discriminator: "1234", public_flags: 0, @@ -36,26 +34,19 @@ resource function get users/[string user_id]() returns UserResponse|http:Respons 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; - } + resource function get channels/[string channelId]/invites() returns anydata => ["Invite1", "Invite2"]; + resource function get channels/[string channelId]/webhooks() returns anydata => ["Webhook1", "Webhook2"]; }; - function init() returns error? { +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(); -} \ No newline at end of file +} diff --git a/ballerina/tests/tests.bal b/ballerina/tests/tests.bal index 5adee66..cdda29c 100644 --- a/ballerina/tests/tests.bal +++ b/ballerina/tests/tests.bal @@ -14,35 +14,47 @@ // specific language governing permissions and limitations // under the License. -import ballerina/test; import ballerina/os; +import ballerina/test; 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"; -configurable string serviceUrl = isLiveServer ? os:getEnv("DISCORD_URL") : "http://localhost:9090/"; -ConnectionConfig config = {auth:{ - token: "BearerToken" -}}; +configurable string token = isLiveServer ? os:getEnv("TOKEN") : "token"; +configurable string clientId = isLiveServer ? os:getEnv("CLIENT_ID") : "test"; +configurable string clientSecret = isLiveServer ? os:getEnv("CLIENT_SECRET") : "test"; +configurable string serviceUrl = isLiveServer ? os:getEnv("DISCORD_URL") : "http://localhost:9090"; + +string userId = "User Id"; +string voiceChannelId = "Voice Channel ID"; +string channelId = "Channel ID"; + +ConnectionConfig config = { + auth: { + token: "BearerToken" + } +}; + final Client discord = check new Client(config, serviceUrl); -@test:Config{} +@test:Config { + groups: ["live_tests", "mock_tests"] +} function testGetUser() returns error? { - string user_id = "User Id"; - UserResponse getUserInfo = check discord->/users/[user_id](); - test:assertTrue((getUserInfo.length()> 0)); + UserResponse user = check discord->/users/[userId](); + test:assertEquals(user.id, userId); } -@test:Config{} +@test:Config { + groups: ["live_tests", "mock_tests"] +} function testGetVoiceChannel() returns error? { - string channel_id = "Voice Channel ID"; - anydata getVoiceChannelInfo = check discord->/channels/[channel_id]/invites(); - test:assertTrue((getVoiceChannelInfo.count()> 0)); + anydata[] voiceChannels = check discord->/channels/[voiceChannelId]/invites(); + test:assertTrue(voiceChannels.length() > 0); } -@test:Config{} +@test:Config { + groups: ["live_tests", "mock_tests"] +} function testGetWebhook() returns error? { - string channel_id = "Channel ID"; - anydata getWebhookInfo = check discord->/channels/[channel_id]/webhooks(); - test:assertTrue((getWebhookInfo.count()> 0)); + anydata[] channelWebhooks = check discord->/channels/[channelId]/webhooks(); + test:assertTrue(channelWebhooks.length() > 0); }