Skip to content

Commit

Permalink
Implement mock server for Candid tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AzeemMuzammil committed Feb 16, 2024
1 parent 261cde2 commit 0ac36f4
Show file tree
Hide file tree
Showing 19 changed files with 595 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build-with-bal-test-graalvm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ jobs:
if: ${{ github.event_name != 'schedule' || (github.event_name == 'schedule' && github.repository_owner == 'ballerina-platform') }}
uses: ballerina-platform/ballerina-standard-library/.github/workflows/build-with-bal-test-graalvm-connector-template.yml@main
secrets: inherit
with:
additional-build-flags: "-x :candid-examples:build -Pgroups=mock"
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ jobs:
secrets: inherit
with:
repo-name: module-ballerinax-candid
additional-test-flags: -x test
additional-test-flags: -Pgroups=mock
2 changes: 1 addition & 1 deletion .github/workflows/daily-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ jobs:
secrets: inherit
with:
repo-name: module-ballerinax-candid
additional-test-flags: -x test
additional-test-flags: -Pgroups=mock
2 changes: 1 addition & 1 deletion .github/workflows/dev-stg-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ jobs:
secrets: inherit
with:
environment: ${{ github.event.inputs.environment }}
additional-publish-flags: -x test
additional-publish-flags: "-x :candid-examples:build -Pgroups=mock"
2 changes: 1 addition & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ jobs:
uses: ballerina-platform/ballerina-standard-library/.github/workflows/pr-build-connector-template.yml@main
secrets: inherit
with:
additional-test-flags: -x test
additional-test-flags: -Pgroups=mock
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ jobs:
with:
package-name: candid
package-org: ballerinax
additional-build-flags: "-x :candid-examples:build"
additional-release-flags: "-x test -x :candid-examples:build"
additional-publish-flags: "-x :candid-examples:build"
additional-build-flags: "-x :candid-examples:build -Pgroups=mock"
additional-release-flags: "-x :candid-examples:build -Pgroups=mock"
additional-publish-flags: "-x :candid-examples:build -Pgroups=mock"
2 changes: 1 addition & 1 deletion .github/workflows/trivy-scan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ jobs:
uses: ballerina-platform/ballerina-standard-library/.github/workflows/trivy-scan-template.yml@main
secrets: inherit
with:
additional-build-flags: "-x test -x :candid-examples:build"
additional-build-flags: "-x :candid-examples:build -Pgroups=mock"
16 changes: 15 additions & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,18 @@ modules = [
{org = "ballerina", packageName = "url", moduleName = "url"}
]

[[package]]
org = "ballerinai"
name = "observe"
version = "0.0.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "observe"}
]
modules = [
{org = "ballerinai", packageName = "observe", moduleName = "observe"}
]

[[package]]
org = "ballerinax"
name = "candid"
Expand All @@ -308,12 +320,14 @@ dependencies = [
{org = "ballerina", name = "log"},
{org = "ballerina", name = "os"},
{org = "ballerina", name = "test"},
{org = "ballerina", name = "url"}
{org = "ballerina", name = "url"},
{org = "ballerinai", name = "observe"}
]
modules = [
{org = "ballerinax", packageName = "candid", moduleName = "candid"},
{org = "ballerinax", packageName = "candid", moduleName = "candid.charitycheckpdf"},
{org = "ballerinax", packageName = "candid", moduleName = "candid.essentials"},
{org = "ballerinax", packageName = "candid", moduleName = "candid.mock"},
{org = "ballerinax", packageName = "candid", moduleName = "candid.premier"}
]

3 changes: 3 additions & 0 deletions ballerina/modules/charitycheckpdf/client.bal
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// AUTO-GENERATED FILE. DO NOT MODIFY.
// This file is auto-generated by the Ballerina OpenAPI tool.

// Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org).
//
// WSO2 LLC. licenses this file to you under the Apache License,
Expand Down
15 changes: 13 additions & 2 deletions ballerina/modules/charitycheckpdf/tests/test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,20 @@ const EIN = "13-1837418";
ApiKeysConfig apiKeyConfig = {
subscriptionKey: apiKey
};
Client charityCheckPDF = check new Client(apiKeyConfig, serviceUrl = "https://apidata.guidestar.org/charitycheckpdf");

@test:Config
Client charityCheckPDF = test:mock(Client);

@test:BeforeGroups {
value: ["candid"]
}
function initializeClientsForCandidServer() returns error? {
log:printInfo("Initializing client for Candid server");
charityCheckPDF = check new (apiKeyConfig, serviceUrl = "https://apidata.guidestar.org/charitycheckpdf");
}

@test:Config {
groups: ["candid"]
}
function testCharitycheckpdf() returns error? {
log:printInfo("charityCheckPDF -> testCharitycheckpdf()");
http:Response result = check charityCheckPDF->/v1/pdf/[EIN];
Expand Down
47 changes: 47 additions & 0 deletions ballerina/modules/charitycheckpdf/tests/test_w_mock_server.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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 candid.mock as _;

import ballerina/log;
import ballerina/http;
import ballerina/test;

const EIN = "13-1837418";

Client charityCheckPDFForMockServer = test:mock(Client);

@test:BeforeGroups {
value: ["mock"]
}
function initializeClientsForMockServer() returns error? {
log:printInfo("Initializing client for mock server");
charityCheckPDF = check new (
apiKeyConfig = {
subscriptionKey: "6006e88b7fc2e0c31fbcb744cca10cafa280341758cd1db45fc1b29b05305dc0"
},
serviceUrl = "http://localhost:9090/charitycheckpdf"
);
}

@test:Config {
groups: ["mock"]
}
function testCharitycheckpdfMock() returns error? {
log:printInfo("charityCheckPDFForMockServer -> testCharitycheckpdfMock()");
http:Response result = check charityCheckPDF->/v1/pdf/[EIN];
test:assertEquals(result.getContentType(), "application/pdf");
}
3 changes: 3 additions & 0 deletions ballerina/modules/charitycheckpdf/types.bal
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// AUTO-GENERATED FILE. DO NOT MODIFY.
// This file is auto-generated by the Ballerina OpenAPI tool.

// Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org).
//
// WSO2 LLC. licenses this file to you under the Apache License,
Expand Down
3 changes: 3 additions & 0 deletions ballerina/modules/charitycheckpdf/utils.bal
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// AUTO-GENERATED FILE. DO NOT MODIFY.
// This file is auto-generated by the Ballerina OpenAPI tool.

// Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org).
//
// WSO2 LLC. licenses this file to you under the Apache License,
Expand Down
35 changes: 28 additions & 7 deletions ballerina/modules/essentials/tests/test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,20 @@ const KEY_OR_VALUE = "C00";
ApiKeysConfig apiKeyConfig = {
subscriptionKey: apiKey
};
Client essentials = check new Client(apiKeyConfig, serviceUrl = "https://api.candid.org/essentials");

@test:Config
Client essentials = test:mock(Client);

@test:BeforeGroups {
value: ["candid"]
}
function initializeClientsForCandidServer() returns error? {
log:printInfo("Initializing client for Candid server");
essentials = check new (apiKeyConfig, serviceUrl = "https://api.candid.org/essentials");
}

@test:Config {
groups: ["candid"]
}
function testEssentialsV1() returns error? {
log:printInfo("essentials -> testEssentialsV1()");
Query query = {
Expand All @@ -49,7 +60,9 @@ function testEssentialsV1() returns error? {
}
}

@test:Config
@test:Config {
groups: ["candid"]
}
function testEssentialsV2() returns error? {
log:printInfo("essentials -> testEssentialsV2()");
Query query = {
Expand All @@ -69,7 +82,9 @@ function testEssentialsV2() returns error? {
}
}

@test:Config
@test:Config {
groups: ["candid"]
}
function testEssentialsV3() returns error? {
log:printInfo("essentials -> testEssentialsV3()");
V3Query query = {
Expand All @@ -84,7 +99,9 @@ function testEssentialsV3() returns error? {
}
}

@test:Config
@test:Config {
groups: ["candid"]
}
function testEssentialsLookup() returns error? {
log:printInfo("essentials -> testEssentialsLookup()");
EssentialsLookupResponse result = check essentials->/lookup;
Expand All @@ -96,7 +113,9 @@ function testEssentialsLookup() returns error? {
}
}

@test:Config
@test:Config {
groups: ["candid"]
}
function testEssentialsLookupFilterName() returns error? {
log:printInfo("essentials -> testEssentialsLookupFilterName()");
EssentialsFilteredLookupResponse result = check essentials->/lookup/[FILTER_NAME];
Expand All @@ -108,7 +127,9 @@ function testEssentialsLookupFilterName() returns error? {
}
}

@test:Config
@test:Config {
groups: ["candid"]
}
function testEssentialsLookupFilterNameKeyOrValue() returns error? {
log:printInfo("essentials -> testEssentialsLookupFilterNameKeyOrValue()");
EssentialsFilteredLookupResponse result = check essentials->/lookup/[FILTER_NAME]/[KEY_OR_VALUE];
Expand Down
142 changes: 142 additions & 0 deletions ballerina/modules/essentials/tests/test_w_mock_server.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// 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 candid.mock as _;

import ballerina/log;
import ballerina/test;

const SEARCH_TERM = "candid";
const FILTER_NAME = "ntee_major";
const KEY_OR_VALUE = "C00";

Client essentialsMockServer = test:mock(Client);

@test:BeforeGroups {
value: ["mock"]
}
function initializeClientsForMockServer() returns error? {
log:printInfo("Initializing client for mock server");
essentialsMockServer = check new (
apiKeyConfig = {
subscriptionKey: "6006e88b7fc2e0c31fbcb744cca10cafa280341758cd1db45fc1b29b05305dc0"
},
serviceUrl = "http://localhost:9090/essentials"
);
}

@test:Config {
groups: ["mock"]
}
function testEssentialsV1Mock() returns error? {
log:printInfo("essentialsMockServer -> testEssentialsV1Mock()");
Query query = {
search_terms: SEARCH_TERM
};
V1EssentialsResponse result = check essentialsMockServer->/v1.post(query);
V1EssentialsResponse_data? data = result?.data;
if data is V1EssentialsResponse_data {
V1EssentialsResponse_data_hits[]? hits = data?.hits;
if hits is V1EssentialsResponse_data_hits[] {
test:assertEquals(hits.length(), 25);
} else {
test:assertFail();
}
} else {
test:assertFail();
}
}

@test:Config {
groups: ["mock"]
}
function testEssentialsV2Mock() returns error? {
log:printInfo("essentialsMockServer -> testEssentialsV2Mock()");
Query query = {
search_terms: SEARCH_TERM
};
V2EssentialsResponse result = check essentialsMockServer->/v2.post(query);
V2EssentialsResponse_data? data = result?.data;
if data is V2EssentialsResponse_data {
V2EssentialsResponse_data_hits[]? hits = data?.hits;
if hits is V2EssentialsResponse_data_hits[] {
test:assertEquals(hits.length(), 25);
} else {
test:assertFail();
}
} else {
test:assertFail();
}
}

@test:Config {
groups: ["mock"]
}
function testEssentialsV3Mock() returns error? {
log:printInfo("essentialsMockServer -> testEssentialsV3Mock()");
V3Query query = {
search_terms: SEARCH_TERM
};
V3EssentialsResponse result = check essentialsMockServer->/v3.post(query);
V3EssentialsResponse_hits[]? hits = result?.hits;
if hits is V3EssentialsResponse_hits[] {
test:assertEquals(hits.length(), 25);
} else {
test:assertFail();
}
}

@test:Config {
groups: ["mock"]
}
function testEssentialsLookupMock() returns error? {
log:printInfo("essentialsMockServer -> testEssentialsLookupMock()");
EssentialsLookupResponse result = check essentialsMockServer->/lookup;
string[]? data = result?.data;
if data is string[] {
test:assertTrue(data.some(val => val == FILTER_NAME));
} else {
test:assertFail();
}
}

@test:Config {
groups: ["mock"]
}
function testEssentialsLookupFilterNameMock() returns error? {
log:printInfo("essentialsMockServer -> testEssentialsLookupFilterNameMock()");
EssentialsFilteredLookupResponse result = check essentialsMockServer->/lookup/[FILTER_NAME];
EssentialsFilteredLookupResponse_data[]? data = result?.data;
if data is EssentialsFilteredLookupResponse_data[] {
test:assertTrue(data.some(val => val?.'key == KEY_OR_VALUE));
} else {
test:assertFail();
}
}

@test:Config {
groups: ["mock"]
}
function testEssentialsLookupFilterNameKeyOrValueMock() returns error? {
log:printInfo("essentialsMockServer -> testEssentialsLookupFilterNameKeyOrValueMock()");
EssentialsFilteredLookupResponse result = check essentialsMockServer->/lookup/[FILTER_NAME]/[KEY_OR_VALUE];
EssentialsFilteredLookupResponse_data[]? data = result?.data;
if data is EssentialsFilteredLookupResponse_data[] {
test:assertTrue(data.some(val => val?.'key == KEY_OR_VALUE));
} else {
test:assertFail();
}
}
Loading

0 comments on commit 0ac36f4

Please sign in to comment.