Skip to content
This repository has been archived by the owner on Mar 31, 2022. It is now read-only.

Commit

Permalink
update markdown files and test files
Browse files Browse the repository at this point in the history
  • Loading branch information
shethdhvani committed Nov 6, 2019
1 parent 4812627 commit a52100b
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 75 deletions.
12 changes: 12 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ signed the OCA can be accepted.

The requirements are to be test driven. Follow the Gherkin format to define a test flow before writing any production code. This will improve readability and set expectations of what code will do.


# Endpoints

```
Expand All @@ -33,6 +34,7 @@ When: API call method
Then: Code response and returned variable
```


# Tests

Use same format and include an assert statement.
Expand All @@ -53,6 +55,7 @@ Example:
}
```


# Contributing

```
Expand Down Expand Up @@ -86,3 +89,12 @@ Example:
//
```


# Build, Compile, and Add .balo files

As of now, we have not pushed this module to Ballerina Central. So in order for others to use this, we need to push the .balo files to the github repository. Build and compile the module as below and make sure you have the .balo files when you make a pull request. For convenience, the below commands skip tests. However, for any change you make, make sure you run all tests so that the module is not broken.

```shell
$ ballerina build -c --skip-tests --all
$ ballerina compile --skip-tests
```
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Alternatively, you can install Oracle OCI connectors from the source using the f
$ git clone https://github.com/oracle/ballerina-oci.git
```

2. Run below commands from the `ballerina-oci` root directory:
2. Run below commands from the `ballerina-oci` root directory. For convenience, the below commands skip tests. However, for any change you make, make sure you run all tests so that the module is not broken.

```shell
$ ballerina build -c --skip-tests --all
Expand Down
44 changes: 11 additions & 33 deletions src/core/core_utils.bal
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@

import ballerina/encoding;
import ballerina/http;
import ballerina/io;
// TODO: commenting the below for now till we fix the JSON conversion error
// import ballerina/io;
import ballerina/time;
// import ballerina/system;
import ballerina/crypto;
// import ballerina/file;
import ballerina/log;
// import ballerina/mime;

//testing out base 8
import ballerina/lang.'array as arrays;
Expand All @@ -45,16 +43,15 @@ public function ociGetRequest(OciConfiguration ociConfig, http:Client ociClient,

if(id == "") {
canonicalQueryString += buildQueryString(queries);
}else{
} else {
canonicalQueryString += id;
}
//string canonicalQueryString = "/" + API_VERSION + "/" + ociResource + "/" + id;

string reqTarget = GET + " " + canonicalQueryString;
var signature = generateSignature(request, GET, ociConfig.tenancyId, ociConfig.authUserId,
ociConfig.keyFingerprint, ociConfig.pathToKey, ociConfig.keyStorePassword,
ociConfig.keyAlias, ociConfig.keyPassword, "", ociConfig.host, reqTarget);
http:Response|error response = ociClient->get(canonicalQueryString, message = request);
//io:println(response);
return getCleanedJSON(ociResource, GET, response);
}

Expand All @@ -66,15 +63,14 @@ public function ociGetRequest(OciConfiguration ociConfig, http:Client ociClient,
# When: DELETE
# Then: Delete resource
public function ociDeleteRequest(OciConfiguration ociConfig, http:Client ociClient,
string ociResource, string id) {
string ociResource, string id) {
http:Request request = new;
string canonicalQueryString = "/" + API_VERSION + "/" + ociResource + "/" + getEncodedString(id);
string reqTarget = DELETE + " " + canonicalQueryString;
var signature = generateSignature(request, DELETE, ociConfig.tenancyId, ociConfig.authUserId,
ociConfig.keyFingerprint, ociConfig.pathToKey, ociConfig.keyStorePassword,
ociConfig.keyAlias, ociConfig.keyPassword, "", ociConfig.host, reqTarget);
var response = ociClient->delete(canonicalQueryString, request);
// return getCleanedJSON(ociResource, DELETE, response);
}

# Given
Expand All @@ -89,7 +85,7 @@ public function ociDeleteRequest(OciConfiguration ociConfig, http:Client ociClie
# Then: Return json response
# Possible bug - jsonBody gets "" as body
public function ociPostRequest(OciConfiguration ociConfig, http:Client ociClient,
string ociResource, string id, string action, json jsonBody) returns json {
string ociResource, string id, string action, json jsonBody) returns json {
http:Request request = new;
string canonicalQueryString = "/" + API_VERSION + "/" + ociResource + "/";

Expand Down Expand Up @@ -118,17 +114,14 @@ public function ociPostRequest(OciConfiguration ociConfig, http:Client ociClient
# Then: Return json response
# Possible bug - jsonBody gets "" as body
public function ociPutRequest(OciConfiguration ociConfig, http:Client ociClient,
string ociResource, string id, json jsonBody) returns json {
string ociResource, string id, json jsonBody) returns json {
http:Request request = new;
string canonicalQueryString = "/" + API_VERSION + "/" + ociResource + "/" + getEncodedString(id);
string reqTarget = PUT + " " + canonicalQueryString;
var signature = generateSignature(request, PUT, ociConfig.tenancyId, ociConfig.authUserId,
ociConfig.keyFingerprint, ociConfig.pathToKey, ociConfig.keyStorePassword,
ociConfig.keyAlias, ociConfig.keyPassword, jsonBody, ociConfig.host, reqTarget);


// io:println(canonicalQueryString);

var response = ociClient->put(canonicalQueryString, request);
return getCleanedJSON(ociResource, PUT, response);
}
Expand All @@ -139,8 +132,6 @@ public function ociPutRequest(OciConfiguration ociConfig, http:Client ociClient,
# When: PUT request
# Then: Return encoded id or throw error
public function getEncodedString(string id) returns string {
//var value = http:encode(id, UTF_8);
// var value = encoding:encodeUriComponent(id, UTF_8);
var value = encoding:encodeUriComponent(id,UTF_8);
if (value is string) {
return value;
Expand Down Expand Up @@ -191,10 +182,8 @@ public function isMsgError(json msg) returns boolean {
# When: getCleanedJSON() has error when retrieving json payload from http response
# Then: print error message code and message
public function printJsonError(string ociResource, string request, json msg) {
// io:println(msg["code"]);
// io:println(msg["message"]);
io:println(msg?.code);
io:println(msg?.message);
// io:println(msg?.code);
// io:println(msg?.message);
}

# Given
Expand All @@ -214,7 +203,6 @@ public function buildQueryString(map<string> queries) returns string {
# When: string cannot be encoded, it throws error
# Then: print error message code and message
public function encodeString(string value) returns string {
// var encodedValue = http:encode(value, UTF-8);
var encodedValue = encoding:encodeUriComponent(value, UTF_8);
string encodedString;
if (encodedValue is string) {
Expand Down Expand Up @@ -251,19 +239,17 @@ public function generateSignature(http:Request request, string httpMethod, strin
signingString = signingString + "(" + REQUEST_TARGET + "): " + reqTarget + "\n";
signingString = signingString + HOST + ": " + host ;
string httpMethodLower = httpMethod.toLowerAscii();
if (httpMethodLower == PUT || httpMethodLower == POST) {
// string body = jsonBody.toString();
if (httpMethodLower == PUT || httpMethodLower == POST) {

string body = jsonBody.toJsonString();

if (body != "") {
request.setPayload(jsonBody);
}
// byte[] bodyArr = body.toByteArray(UTF_8);

byte[] bodyArr = body.toBytes();
int contentLength = body.length();
byte[] contentSha256Byte = crypto:hashSha256(bodyArr);
// string contentSha256 = encoding:encodeBase64(contentSha256Byte);

string contentSha256 = arrays:toBase64(contentSha256Byte);

Expand All @@ -274,23 +260,15 @@ public function generateSignature(http:Request request, string httpMethod, strin
CONTENT_LENGTH + ": " + contentLength.toString() + "\n" +
CONTENT_TYPE + ": " + APPLICATION_JSON + "\n" +
X_CONTENT_SHA256 + ": " + contentSha256;

// io:println("Signing String");
// io:println(signingString);
// io:println();
}

// Get Private Key
crypto:KeyStore keyStore = {path: pathToKey, password: keyStorePassword};
var privateKey = crypto:decodePrivateKey(keyStore, keyAlias, keyPassword);
if (privateKey is crypto:PrivateKey) {
// Encrypt signing string
// byte[] signingArray = signingString.toByteArray(UTF_8);

byte[] signingArray = signingString.toBytes();

byte[] signatureArray = check crypto:signRsaSha256(signingArray, privateKey);
// string signature = encoding:encodeBase64(signatureArray);
string signature = arrays:toBase64(signatureArray);

// Create Authorization Header
Expand Down
18 changes: 8 additions & 10 deletions src/core/tests/core_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
//

import ballerina/config;
// import ballerina/http;
// import ballerina/log;
import ballerina/test;
import ballerina/runtime;
import ballerina/io;
Expand Down Expand Up @@ -59,14 +57,14 @@ function delay(float minutes) {
}

// Instances test variables
string compartmentId = "ocid1.compartment.oc1..aaaaaaaabsv7hnaozvjazwtzbid6kt2nsimfisb5nmsyhurqsctq5ipiizja";
string instanceDisplayName = "BallerinaTest1";
string instanceDisplayNameUpdate = "BallerinaTestUpdate";
string instanceId = "ocid1.instance.oc1.phx.anyhqljrytsgwayckfhavbszynjh6lmdcv75ofyfzpxn6a5oxle5ggrlec5q";
string instanceAvailDomain = "UWQV:PHX-AD-2";
string instanceShape = "VM.Standard2.1";
string instanceImageId = "ocid1.image.oc1.phx.aaaaaaaa7nklq5tjkb76kcbkikfsc2erz7nsjejssynz3oo4zfuxmywwsdbq";
string instanceSubnetId = "ocid1.subnet.oc1.phx.aaaaaaaaqa3wodl5xtedlvuqqvpyxpwrcrzbtge6vzf6ots5yxbwrfnduphq" ;
string compartmentId = "";
string instanceDisplayName = "";
string instanceDisplayNameUpdate = "";
string instanceId = "";
string instanceAvailDomain = "";
string instanceShape = "";
string instanceImageId = "";
string instanceSubnetId = "" ;


// VCN test variables
Expand Down
47 changes: 16 additions & 31 deletions src/iam/tests/iam_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
//

import ballerina/config;
//import ballerina/http;
//import ballerina/log;
import ballerina/test;
import ballerina/io;
import ballerina/runtime;
Expand Down Expand Up @@ -59,13 +57,12 @@ function delay(float minutes) {
runtime:sleep(totalDelay);
}

//Users test variables
//string compartmentId = "ocid1.tenancy.oc1..aaaaaaaasvq5kgrujit7sohgfbtkaexgi35auduftlaxol65resgapojzofa";
string userDescription = "User created via ballerina";
string userName = "Ballerina";
string userId = "ocid1.user.oc1..aaaaaaaaq2bstu4yg3z6vjtl6ph7sk223kgx55zg6nxsfdlbgkfoeyz4vrya";
string userDescriptionUpdate = "Description updated by ballerina";

//Users test variables
string userDescription = "";
string userName = "";
string userId = "";
string userDescriptionUpdate = "";


//Group
Expand All @@ -74,25 +71,22 @@ string userDescriptionUpdate = "Description updated by ballerina";
# Example: ballerina test --config ballerina.conf --groups groups-list

//Groups test variables
string groupDisplayName = "BallerinaGroup";
string groupDescription = "Group created via ballerina";
string groupDescriptionUpdate = "Description updated by ballerina";
string groupId = "ocid1.group.oc1..aaaaaaaafpkd3wowr55m6geh6ikv3hlkxnl5erzhtcqfcgu2yob2ie6rufrq";


string groupDisplayName = "";
string groupDescription = "";
string groupDescriptionUpdate = "";
string groupId = "";


//Compartment
# Terminal command to test:
# ballerina test --config ballerina.conf --groups compartments-<GROUPS>
# Example: ballerina test --config ballerina.conf --groups compartments-list
string compartmentName = "Ballerina";
string compartmentDescription = "test that can be deleted later";
string compartmentUpdatedDescription = "updated via ballerina";
string compartmentUpdatedName = "Ballerina_new";
string compartmentId = "ocid1.compartment.oc1..aaaaaaaam2r7gxy4bvkf2ebt3y4hges4sjmihtqbkx43iqo4b2znvvzxioua";
string compartmentLimit = "50";

string compartmentName = "";
string compartmentDescription = "";
string compartmentUpdatedDescription = "";
string compartmentUpdatedName = "";
string compartmentId = "";
string compartmentLimit = "";


# Given: compartmentId and user requirements in jsonBody
Expand Down Expand Up @@ -145,7 +139,6 @@ function testGetUser() {
test:assertEquals(user.id, userId, msg = "USER not found");
}


# Given: Dictionary query string of user compartmentId
# When: listUsers
# Then: Returns list of users from compartment
Expand All @@ -163,7 +156,6 @@ function testListUsers() {
io:println("User Name: " + user.name);
io:println("User Id: " + user.id);
}
//test:assertEquals(exists, true , msg = "Cannot find user with name " + userName + " within list");
}

# Given: userId with userDescriptionUpdate in jsonBody
Expand Down Expand Up @@ -197,7 +189,6 @@ function testCreateGroups() {
"name" : groupDisplayName,
"description": groupDescription
};
//io:println("ociClient -> createGroup()");
OciGroups groupi = ociClient->createGroups(jsonBody);
io:println("Group " + groupi.name + " created with OCID--> " + groupi.id);
test:assertEquals(groupDisplayName, groupi.name, msg = "GROUPS " + groupDisplayName + " not created.");
Expand Down Expand Up @@ -250,7 +241,6 @@ function testListGroups() {
io:println("Group Name: " + groupi.name);
io:println("Group Id: " + groupi.id);
}
//test:assertEquals(exists, true, msg = "Cannot find Group with name " + groupDisplayName + " within list");
}

# Given: groupId
Expand All @@ -270,9 +260,6 @@ function testUpdateGroup() {
test:assertEquals(groupDescriptionUpdate, groupi.description, msg = "Group description not updated");
}




# Given: Dictionary query string of compartmentId
# When: listCompartments
# Then: Returns list of compartments from parent compartment
Expand All @@ -286,7 +273,6 @@ function testListCompartments() {
"compartmentId" : compartmentId,
"compartmentIdInSubtree" : "true",
"limit": compartmentLimit

};
OciCompartment[] compartments = ociClient->listCompartments(queries);
boolean exists = false;
Expand All @@ -296,7 +282,6 @@ function testListCompartments() {
exists = true;
}
}
//test:assertEquals(exists, true, msg = "Cannot find compartment with name " + compartmentName + " within list");
}

@test:Config{
Expand Down Expand Up @@ -344,4 +329,4 @@ function testUpdateCompartment() {
};
OciCompartment compartment = ociClient->updateCompartment(compartmentId, jsonBody);
test:assertEquals(compartment.name, compartmentUpdatedName , msg = "Cannot update compartment");
}
}
Binary file modified target/balo/core-2019r3-any-0.1.0.balo
Binary file not shown.
Binary file modified target/balo/iam-2019r3-any-0.1.0.balo
Binary file not shown.
Binary file modified target/balo/objectstorage-2019r3-any-0.1.0.balo
Binary file not shown.

0 comments on commit a52100b

Please sign in to comment.