This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
pet-be_test.bal
81 lines (69 loc) · 2.9 KB
/
pet-be_test.bal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
// Licensed 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.
// Test file for Pet Store Sample Backend.
import ballerina/io;
import ballerina/test;
import ballerina/http;
import celleryio/cellery;
import ballerina/runtime;
string PET_BE_CONTROLLER_ENDPOINT = "";
# Handle creation of instances for running tests
@test:BeforeSuite
function setup() {
cellery:TestConfig testConfig = <@untainted>cellery:getTestConfig();
cellery:runInstances(testConfig);
cellery:Reference petBeUrls = <cellery:Reference>cellery:getInstanceEndpoints();
PET_BE_CONTROLLER_ENDPOINT= <string>petBeUrls["controller_ingress_api_url"];
}
@test:Config {}
function testDocker() {
map<cellery:Env> envVars = {PET_BE_CELL_URL: { value: PET_BE_CONTROLLER_ENDPOINT }};
error? err = cellery:runDockerTest("docker.io/wso2cellery/samples-pet-store-order-tests", envVars);
test:assertFalse(err is error, msg = "Docker image test failed.\n Reason: \n" + err.toString() + "\n");
}
# Tests inserting order from an external cell by calling the pet-be gateway
@test:Config {}
function testInsertOrder() {
io:println(PET_BE_CONTROLLER_ENDPOINT);
runtime:sleep(10000);
string ordersContext = "/orders";
string payload = "{\"order\":[{\"id\":1,\"amount\":1}]}";
string expectedPostResponsePrefix = "status=SUCCESS";
http:Client clientEndpoint = new(PET_BE_CONTROLLER_ENDPOINT);
http:Request req = new;
req.setPayload(payload);
req.setHeader("Content-Type", "application/json");
var response = clientEndpoint->post(ordersContext, req);
string responseStr = handleResponse(response);
test:assertTrue(
responseStr.startsWith(expectedPostResponsePrefix),
msg = "Order insertion failed.\n Expected: \n" + expectedPostResponsePrefix + "\n Actual: \n" + responseStr + "\n"
);
}
function handleResponse(http:Response|error response) returns @tainted string {
if (response is http:Response) {
var msg = response.getJsonPayload();
if (msg is json) {
return msg.toString();
} else {
return "Invalid payload received:" + msg.reason();
}
} else {
return "Error when calling the backend: " + response.reason();
}
}
# Handle deletion of instances for running tests
@test:AfterSuite
public function cleanUp() {
error? err = cellery:stopInstances();
}