Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
83 changes: 83 additions & 0 deletions ballerina/tests/mock_test.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
//
// 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;

// create mock client
final Client mockClient = check new (
{
auth: {
token: "test-token" // This approach eliminates the need for the client to make additional server requests for token validation, such as a refresh token request in the OAuth2 flow.
}
}, "http://localhost:9090"
);

// this is a mock test
@test:Config {
groups: ["mock_service_test"]
}
function testMockBatchUpsert() returns error? {

BatchInputSimplePublicObjectBatchInputUpsert payload = {
"inputs": [
{
"idProperty": "string",
"objectWriteTraceId": "string",
"id": "string",
"properties": {
"additionalProp1": "string",
"additionalProp2": "string",
"additionalProp3": "string"
}
}
]
};

BatchResponseSimplePublicUpsertObject|BatchResponseSimplePublicUpsertObjectWithErrors upsertResponse =
check mockClient->/batch/upsert.post(payload, {});

if (upsertResponse is BatchResponseSimplePublicUpsertObjectWithErrors) {
test:assertFail("Batch upsert failed");
}
test:assertNotEquals(upsertResponse.results[0].id, ());
test:assertEquals(upsertResponse.results[0].properties["hs_label"], "A fixed, one-time discount");
test:assertEquals(upsertResponse.results[0].properties["hs_value"], "50");
test:assertEquals(upsertResponse.results[0].properties["hs_type"], "PERCENT");
}

// this is a mock test
@test:Config {
groups: ["mock_service_test"]
}
function testMockList() returns error? {

GetCrmV3ObjectsDiscountsQueries params = {
'limit: 10,
archived: false,
properties: ["hs_label", "hs_value", "hs_type"]
};

CollectionResponseSimplePublicObjectWithAssociationsForwardPaging response =
check mockClient->/.get({}, params);

test:assertNotEquals(response.results, [], "No discounts found");
test:assertTrue(response.results.length() <= 10, "Limit Exceeded");
test:assertNotEquals(response.results[0].id, (), "Discount id is not found");
test:assertNotEquals(response.results[0].properties, (), "Discount properties are not found");
test:assertNotEquals(response.results[0].properties["hs_type"], (), "Discount label is not found");
test:assertNotEquals(response.results[0].properties["hs_value"], (), "Discount value is not found");
test:assertNotEquals(response.results[0].properties["hs_label"], (), "Discount type is not found");
}
101 changes: 24 additions & 77 deletions ballerina/tests/tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ import ballerina/http;
import ballerina/oauth2;
import ballerina/test;

configurable string clientId = "mock";
configurable string clientSecret = "mock";
configurable string refreshToken = "mock";

configurable boolean isLiveServer = false;
configurable string clientId = ?;
configurable string clientSecret = ?;
configurable string refreshToken = ?;

// test discount ids for batch and basic endpoints.
string discountId = "";
Expand All @@ -38,29 +36,21 @@ string newHsValue = "8";
string newHsLabel = "test_updated_label";
string newHsType = "PERCENT";

final string serviceUrl = isLiveServer ? "https://api.hubapi.com/crm/v3/objects/discounts" : "http://localhost:9090";

final Client hubspotClient = check initClient();

isolated function initClient() returns Client|error {
if isLiveServer {
OAuth2RefreshTokenGrantConfig auth = {
clientId: clientId,
clientSecret: clientSecret,
refreshToken: refreshToken,
credentialBearer: oauth2:POST_BODY_BEARER
};
return check new ({auth}, serviceUrl);
// create connection config for live client
ConnectionConfig config = {
auth: {
clientId,
clientSecret,
refreshToken,
credentialBearer: oauth2:POST_BODY_BEARER
}
return check new ({
auth: {
token: "test-token"
}
}, serviceUrl);
}
};

// create live client
final Client hubspotClient = check new (config);

@test:Config {
groups: ["live_tests", "mock_tests"]
groups: ["live_service_test"]
}
function testList() returns error? {

Expand All @@ -84,8 +74,7 @@ function testList() returns error? {

@test:Config {
dependsOn: [testCreate],
enable: isLiveServer,
groups: ["live_tests"]
groups: ["live_service_test"]
}
function testRead() returns error? {
GetCrmV3ObjectsDiscountsDiscountidQueries params = {
Expand All @@ -103,8 +92,7 @@ function testRead() returns error? {

@test:Config {
dependsOn: [testRead],
enable: isLiveServer,
groups: ["live_tests"]
groups: ["live_service_test"]
}
function testUpdate() returns error? {
SimplePublicObjectInput payload = {
Expand All @@ -123,8 +111,7 @@ function testUpdate() returns error? {

@test:Config {
dependsOn: [testUpdate],
enable: isLiveServer,
groups: ["live_tests"]
groups: ["live_service_test"]
}
function testArchive() returns error? {
http:Response deleteResponse = check hubspotClient->/[discountId].delete({});
Expand All @@ -133,8 +120,7 @@ function testArchive() returns error? {
}

@test:Config {
enable: isLiveServer,
groups: ["live_tests"]
groups: ["live_service_test"]
}
function testCreate() returns error? {
hsLabel = "test_discount";
Expand Down Expand Up @@ -166,9 +152,8 @@ function testCreate() returns error? {
}

@test:Config {
enable: isLiveServer,
dependsOn: [testBatchRead],
groups: ["live_tests"]
groups: ["live_service_test"]
}
function testBatchUpdate() returns error? {
BatchInputSimplePublicObjectBatchInput payload = {
Expand Down Expand Up @@ -227,9 +212,8 @@ function testBatchUpdate() returns error? {
}

@test:Config {
enable: isLiveServer,
dependsOn: [testSearch],
groups: ["live_tests"]
groups: ["live_service_test"]
}
function testBatchRead() returns error? {
BatchReadInputSimplePublicObjectId payload = {
Expand Down Expand Up @@ -261,9 +245,8 @@ function testBatchRead() returns error? {
}

@test:Config {
enable: isLiveServer,
dependsOn: [testArchive],
groups: ["live_tests"]
groups: ["live_service_test"]
}
function testBatchCreate() returns error? {

Expand Down Expand Up @@ -326,9 +309,8 @@ function testBatchCreate() returns error? {
}

@test:Config {
enable: isLiveServer,
dependsOn: [testBatchUpdate],
groups: ["live_tests"]
groups: ["live_service_test"]
}
function testBatchArchive() returns error? {
BatchInputSimplePublicObjectId payload = {
Expand All @@ -345,9 +327,8 @@ function testBatchArchive() returns error? {
}

@test:Config {
enable: isLiveServer,
dependsOn: [testBatchCreate],
groups: ["live_tests"]
groups: ["live_service_test"]
}
function testSearch() returns error? {
PublicObjectSearchRequest payload = {
Expand Down Expand Up @@ -375,37 +356,3 @@ function testSearch() returns error? {
i = i + 1;
}
}

// this is a mock test
@test:Config {
enable: !isLiveServer,
groups: ["mock_tests"]
}
function testBatchUpsert() returns error? {

BatchInputSimplePublicObjectBatchInputUpsert payload = {
"inputs": [
{
"idProperty": "string",
"objectWriteTraceId": "string",
"id": "string",
"properties": {
"additionalProp1": "string",
"additionalProp2": "string",
"additionalProp3": "string"
}
}
]
};

BatchResponseSimplePublicUpsertObject|BatchResponseSimplePublicUpsertObjectWithErrors upsertResponse =
check hubspotClient->/batch/upsert.post(payload, {});

if (upsertResponse is BatchResponseSimplePublicUpsertObjectWithErrors) {
test:assertFail("Batch upsert failed");
}
test:assertNotEquals(upsertResponse.results[0].id, ());
test:assertEquals(upsertResponse.results[0].properties["hs_label"], "A fixed, one-time discount");
test:assertEquals(upsertResponse.results[0].properties["hs_value"], "50");
test:assertEquals(upsertResponse.results[0].properties["hs_type"], "PERCENT");
}
Loading