Skip to content

Commit

Permalink
Merge pull request #15 from ballerina-platform/coverage
Browse files Browse the repository at this point in the history
Increase code coverage
  • Loading branch information
niveathika authored Jun 3, 2024
2 parents 43b9040 + eefa0ca commit 78df3be
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build-with-bal-test-graalvm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ jobs:
uses: ballerina-platform/ballerina-library/.github/workflows/build-with-bal-test-graalvm-connector-template.yml@main
secrets: inherit
with:
additional-build-flags: "-x :sap-examples:build"
additional-build-flags: "-x :sap-examples:build -Pdisable=sap"
additional-test-flags: "--disable-groups sap"
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
with:
repo-name: module-ballerinax-sap
additional-build-flags: "-x :sap-examples:build"
additional-test-flags: "-x :sap-examples:test"
additional-test-flags: "-x :sap-examples:test -Pdisable=sap"
2 changes: 1 addition & 1 deletion .github/workflows/daily-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ jobs:
with:
repo-name: module-ballerinax-sap
additional-build-flags: "-x :sap-examples:test"
additional-test-flags: "-x :sap-examples:test"
additional-test-flags: "-x :sap-examples:test -Pdisable=sap"
2 changes: 1 addition & 1 deletion .github/workflows/dev-stage-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 :sap-examples:test"
additional-publish-flags: "-x :sap-examples:test -Pdisable=sap"
2 changes: 1 addition & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ jobs:
secrets: inherit
with:
additional-build-flags: "-x :sap-examples:test"
additional-test-flags: "-x :sap-examples:test"
additional-test-flags: "-x :sap-examples:test -Pdisable=sap"
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ jobs:
package-org: ballerinax
additional-build-flags: "-x :sap-examples:test"
additional-publish-flags: "-x :sap-examples:test"
additional-release-flags: "-x :sap-examples:test"
additional-release-flags: "-x :sap-examples:test -Pdisable=sap"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ Execute the commands below to build from the source.
./gradlew clean test -Pgroups=<Comma separated groups/test cases>
```

Tip: The following groups of test cases are available.
**Tip**: For detailed information, please refer to the [Tests_README](ballerina/tests/README.md) document.
Groups | Environment
---| ---
mock | Mock server
Expand Down
6 changes: 5 additions & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "crypto"
version = "2.7.1"
version = "2.7.2"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "time"}
Expand Down Expand Up @@ -186,6 +186,9 @@ version = "0.0.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
modules = [
{org = "ballerina", packageName = "lang.runtime", moduleName = "lang.runtime"}
]

[[package]]
org = "ballerina"
Expand Down Expand Up @@ -316,6 +319,7 @@ version = "1.0.0"
dependencies = [
{org = "ballerina", name = "http"},
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.runtime"},
{org = "ballerina", name = "mime"},
{org = "ballerina", name = "test"},
{org = "ballerinai", name = "observe"}
Expand Down
4 changes: 4 additions & 0 deletions ballerina/modules/mock/server.bal
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ service /API_SALES_ORDER_SRV on new http:Listener(9093) {
return res;
}

resource function options .() returns http:Response|http:InternalServerError {
return http:INTERNAL_SERVER_ERROR;
}

resource function get A_SalesOrder() returns http:Response {
http:Response res = new;
res.setPayload("Sales Order");
Expand Down
50 changes: 50 additions & 0 deletions ballerina/tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Running Tests

The SAP connector supports two test environments. The primary environment is a mock server, specifically designed to handle CSRF token expiry. The secondary environment utilizes the S/4HANA Sales Order (A2X) API. Each environment has a unique set of compatible tests that can be executed independently.

Test Groups | Environment
---| ---
mock | Mock server
sap | S/4HANA API

**Note**: By default, the Gradle build enables tests for both environments. However, in GitHub workflows, the S/4HANA environment tests are disabled due to the lack of continuous access to the S/4HANA environment.

## Running Tests in the Mock Server Only

```sh
bal test --disable-groups sap
```

```sh
./gradlew clean build -Pdisable=sap
```

## Running Tests in S/4HANA API Only

**Note**: The test case designed to handle the CSRF token expiry scenario may take approximately 35 minutes to run due to the configuration in S/4HANA setup.

### Prerequisites

### 1. Setup the S/4HANA API

Refer to the [Setup Guide](https://central.ballerina.io/ballerinax/sap/latest#setup-guide) for the necessary credentials (hostname, username, password).

### 2. Configuration

Create a `Config.toml` file in the tests directory and add your authentication credentials.

```toml
hostname="<Hostname>"
username="<Username>"
password="<Password>"
```

### 3. Run tests

```sh
bal test --groups sap
```

```sh
./gradlew clean build -Pgroups=sap
```
59 changes: 59 additions & 0 deletions ballerina/tests/s4hana_test.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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/lang.runtime;
import ballerina/test;

configurable string hostname = "";
configurable string username = "";
configurable string password = "";

// This test is run against the SAP Sales Order API.
Client s4HanaClient = check new (string `https://${hostname}/sap/opu/odata/sap/API_SALES_ORDER_SRV`, {
auth: {
username,
password
}
});

@test:Config {
groups: ["sap"]
}
function testCSRFTokenRefresh() returns error? {
json body = {
"SalesOrder": "1234",
"SalesOrderType": "OR",
"SalesOrganization": "1710",
"DistributionChannel": "10",
"OrganizationDivision": "00",
"SalesGroup": "170",
"SalesOffice": "170",
"RequestedDeliveryDate": "2024-04-13T12:00",
"SoldToParty": "17100001",
"PurchaseOrderByCustomer": "12345"
};

http:Response response = check s4HanaClient->/A_SalesOrder.post(body);
test:assertEquals(response.statusCode, 404, "Status code should be 404, this creates a partial sales order meant to fail");

// Sleep for 33 m to allow the CSRF token to expire
runtime:sleep(2000);

// Try again. If the csrf token is refreshed we expect 404 this time too
response = check s4HanaClient->/A_SalesOrder.post(body);
test:assertEquals(response.statusCode, 404, "Status code should be 404, this creates a partial sales order meant to fail");
}
61 changes: 61 additions & 0 deletions ballerina/tests/test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,87 @@ function testTokenRefreshAfterExpiry() returns error? {
test:assertEquals(response.statusCode, 200, "Status code should be 200");
}

@test:Config {
dependsOn: [testTokenRefreshAfterExpiry]
}
function testPostRemote() returns error? {
http:Response response = check sapClient->/A_SalesOrder.post("testPayload");
test:assertEquals(response.statusCode, 200, "Status code should be 200");
}

@test:Config {
dependsOn: [testPostRemote]
}
function testHeadRemote() returns error? {
http:Response response = check sapClient->/.head();
test:assertEquals(response.statusCode, 200, "Status code should be 200");
}

@test:Config {
dependsOn: [testHeadRemote]
}
function testHeadResource() returns error? {
http:Response response = check sapClient->head("");
test:assertEquals(response.statusCode, 200, "Status code should be 200");
}

@test:Config {}
function testGetFunction() returns error? {
http:Response response = check sapClient->get("/A_SalesOrder");
test:assertEquals(response.statusCode, 200, "Status code should be 200");
}

@test:Config {}
function testGetRemote() returns error? {
http:Response response = check sapClient->/A_SalesOrder();
test:assertEquals(response.statusCode, 200, "Status code should be 200");
}

@test:Config {}
function testPatchFunction() returns error? {
http:Response response = check sapClient->patch("/A_SalesOrder/1", "testPayload");
test:assertEquals(response.statusCode, 200, "Status code should be 200");
}

@test:Config {}
function testPatchRemote() returns error? {
http:Response response = check sapClient->/A_SalesOrder/'1.patch("testPayload");
test:assertEquals(response.statusCode, 200, "Status code should be 200");
}

@test:Config {}
function testPutFunction() returns error? {
http:Response response = check sapClient->put("/A_SalesOrder/1", "testPayload");
test:assertEquals(response.statusCode, 200, "Status code should be 200");
}

@test:Config {}
function testPutRemote() returns error? {
http:Response response = check sapClient->/A_SalesOrder/'1.put("testPayload");
test:assertEquals(response.statusCode, 200, "Status code should be 200");
}

@test:Config {}
function testDeleteFunction() returns error? {
http:Response response = check sapClient->delete("/A_SalesOrder", "testPayload");
test:assertEquals(response.statusCode, 200, "Status code should be 200");
}

@test:Config {}
function testDeleteRemote() returns error? {
http:Response response = check sapClient->/A_SalesOrder.delete("testPayload");
test:assertEquals(response.statusCode, 200, "Status code should be 200");
}

@test:Config {}
function testOptionsRemote() returns error? {
int intPath = 1;
http:Response response = check sapClient->/[intPath].options(head = "testPayload");
test:assertEquals(response.statusCode, 404, "Status code should be 404");
}

@test:Config {}
function testOptionsResource() returns error? {
json|error response = sapClient->options("");
test:assertTrue(response is error, "Status code should be 500");
}

0 comments on commit 78df3be

Please sign in to comment.