Skip to content

Commit

Permalink
Merge pull request #165 from chamil321/test-fail
Browse files Browse the repository at this point in the history
Introduce RE_V and RN_V result types
  • Loading branch information
chamil321 authored Jul 31, 2020
2 parents 6b53487 + 909ac9c commit ce1330f
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 55 deletions.
2 changes: 2 additions & 0 deletions distributor/src/distributor/constants.bal
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ const ELECTION_TYPE_PARLIAMENTARY = "PARLIAMENTARY";

const RP_V = "RP_V";
const RE_VI = "RE_VI";
const RE_V = "RE_V";
const RE_S = "RE_S";
const RN_SI = "RN_SI";
const RN_V = "RN_V";
const RN_VS = "RN_VS";
const RN_VSN = "RN_VSN";
const RE_SC = "RE_SC";
Expand Down
8 changes: 5 additions & 3 deletions distributor/src/distributor/genhtml.bal
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ function generateParliamentaryResultHtml(string electionCode, map<json> result,
" POLLING DIVISION (" + <string>result.pd_code + ") RESULT"; }
RE_VI => { resTypeHeading = <string>result.ed_name + " ELECTORAL DISTRICT CUMULATIVE RESULTS AT " +
<string>result.timestamp; }
RE_V => { resTypeHeading = <string>result.ed_name + " ELECTORAL DISTRICT VOTES RESULT"; }
RE_S => { resTypeHeading = <string>result.ed_name + " ELECTORAL DISTRICT SEAT ALLOCATION RESULT"; }
RN_SI => { resTypeHeading = "ALL ISLAND CUMULATIVE SEAT ALLOCATION RESULTS AT " + <string>result.timestamp; }
RN_V => { resTypeHeading = "ALL ISLAND VOTES RESULT"; }
RN_VS => { resTypeHeading = "ALL ISLAND VOTES + SEAT ALLOCATION RESULT"; }
RN_VSN => { resTypeHeading = "ALL ISLAND VOTES + SEAT ALLOCATION + NATIONAL LIST ALLOCATION RESULT"; }
RE_SC => { resTypeHeading = <string>result.ed_name + " ELECTORAL DISTRICT SEATS ALLOCATED CANDIDATES"; }
Expand All @@ -125,7 +127,7 @@ function generateParliamentaryResultHtml(string electionCode, map<json> result,
"<th>Name of Party</th>" +
"<th class='text-center'>Party Abbreviaton</th>";

if ('type == RP_V || 'type == RE_VI || 'type == RN_VS || 'type == RN_VSN) {
if ('type == RP_V || 'type == RE_VI || 'type == RE_V || 'type == RN_V || 'type == RN_VS || 'type == RN_VSN) {
body += "<th class='text-right'>Votes Received</th>" +
"<th class='text-right'>Percentage</th>";
}
Expand All @@ -144,7 +146,7 @@ function generateParliamentaryResultHtml(string electionCode, map<json> result,
"<td>" + <string>pr.party_name + "</td>" +
"<td class='text-center'>" + <string>pr.party_code +"</td>";

if ('type == RP_V || 'type == RE_VI || 'type == RN_VS || 'type == RN_VSN) {
if ('type == RP_V || 'type == RE_VI || 'type == RE_V || 'type == RN_V || 'type == RN_VS || 'type == RN_VSN) {
body += "<td class='text-right'>" + commaFormatInt(<int>pr.vote_count) + "</td>" +
"<td class='text-right'>" + <string>pr.vote_percentage + "</td>";
}
Expand Down Expand Up @@ -238,7 +240,7 @@ function sortParliamentaryByPartyResults(json[] unsorted, string resultType) ret
return sortByPartyResultsBySeatCount(unsorted);
}
}
// RN_VS, RN_VSN
// RN_V, RN_VS, RN_VSN
return sortByPartyResultsByVoteCount(unsorted);
}

Expand Down
41 changes: 23 additions & 18 deletions distributor/src/distributor/messenger.bal
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,15 @@ function sendSMS(string message, string resultId) {
}
string logMessage = "Sending SMS for " + resultId;
log:printInfo(logMessage);
foreach string targetMobile in mobileSubscribers {
log:printInfo(logMessage + " to " + targetMobile);
var response = smsClient->sendSms(sourceDepartment, message, targetMobile);
if response is error {
log:printError("Message sending failed for \'" + targetMobile + "\'", response);
string[] keys = mobileSubscribers.keys();
foreach string userName in keys {
string? targetMobile = mobileSubscribers[userName];
if !(targetMobile is ()) {
log:printInfo(logMessage + " to " + targetMobile);
var response = smsClient->sendSms(sourceDepartment, message, targetMobile);
if response is error {
log:printError("Message sending failed for \'" + targetMobile + "\'", response);
}
}
}
}
Expand All @@ -81,7 +85,7 @@ function sendSMS(string message, string resultId) {
function validate(string mobileNo) returns string|error {
string mobile = <@untained> mobileNo.trim();

if (mobile.startsWith("+94") && mobile.length() == 12) {
if (mobile.startsWith("+") && mobile.length() == 12) {
mobile = mobile.substring(1);
}

Expand All @@ -91,18 +95,19 @@ function validate(string mobileNo) returns string|error {
log:printError(errorMsg);
return error(ERROR_REASON, message = errorMsg);
}

if (mobile.startsWith("0") && mobile.length() == 10) {
return mobile;
}
if (mobile.startsWith("94") && mobile.length() == 11) {
return mobile;
}
// Allow only the local mobile numbers to register via public API. International number are avoided.
log:printError("Invalid mobile number : " + mobile);
return error(ERROR_REASON, message = "Invalid mobile number. Resend the request as follows: If the " +
"mobile no is 0771234567, send POST request to \'/sms\' with JSON payload " +
"\'{\"username\":\"myuser\", \"mobile\":\"0771234567\"}\'");
return mobile;

//if (mobile.startsWith("0") && mobile.length() == 10) {
// return mobile;
//}
//if (mobile.startsWith("94") && mobile.length() == 11) {
// return mobile;
//}
//// Allow only the local mobile numbers to register via public API. International number are avoided.
//log:printError("Invalid mobile number : " + mobile);
//return error(ERROR_REASON, message = "Invalid mobile number. Resend the request as follows: If the " +
// "mobile no is 0771234567, send POST request to \'/sms\' with JSON payload " +
// "\'{\"username\":\"myuser\", \"mobile\":\"0771234567\"}\'");
}

# Register recipient in the mobileSubscribers list and persist in the smsRecipients db table.
Expand Down
2 changes: 1 addition & 1 deletion distributor/src/distributor/tests/resources/contact1.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"username":"newuser1",
"mobile":"0711234561"
"mobile":"07112345611111111"
},
{
"username":"newuser2",
Expand Down
2 changes: 1 addition & 1 deletion distributor/src/distributor/tests/resources/contact2.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"username":"newuser1",
"mobile":"+00771234562"
"mobile":"+00771234567976902"
},
{
"username":"newuser2",
Expand Down
24 changes: 12 additions & 12 deletions distributor/src/distributor/tests/sms_publisher_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function testSubscriberRegistration() {
// Test bulk success registration
req = new;
req.setFileAsPayload("src/distributor/tests/resources/contact1.json");
response = httpEndpoint->post("/sms/addall", req);
response = httpEndpoint->post("/sms/all", req);
if (response is http:Response) {
var result = response.getTextPayload();
if (result is string) {
Expand All @@ -115,11 +115,11 @@ function testSubscriberRegistration() {
// Test bulk registration with invalid nos
req = new;
req.setFileAsPayload("src/distributor/tests/resources/contact2.json");
response = httpEndpoint->post("/sms/addall", req);
response = httpEndpoint->post("/sms/all", req);
if (response is http:Response) {
var result = response.getTextPayload();
if (result is string) {
test:assertEquals(result, "Validation failed: invalid recipient mobile no: newuser1:+00771234562");
test:assertEquals(result, "Validation failed: invalid recipient mobile no: newuser1:+00771234567976902");
} else {
test:assertFail(msg = "Invalid response message:");
}
Expand All @@ -130,7 +130,7 @@ function testSubscriberRegistration() {
// Test bulk registration with malformed JSON
req = new;
req.setFileAsPayload("src/distributor/tests/resources/contact3.json");
response = httpEndpoint->post("/sms/addall", req);
response = httpEndpoint->post("/sms/all", req);
if (response is http:Response) {
var result = response.getTextPayload();
if (result is string) {
Expand All @@ -156,7 +156,7 @@ function testResetRecipients() {
}
});
http:Request req = new;
var response = httpEndpoint->delete("/sms/reset", req);
var response = httpEndpoint->delete("/sms/all", req);
if (response is http:Response) {
var result = response.getTextPayload();
if (result is string) {
Expand All @@ -182,15 +182,15 @@ function testValidateFunction() {
test:assertTrue(validate("+94716181195") == "94716181195", msg = "Failed assertion : 94716181195");

// validate invalid local numbers
error err = <error> validate("07161811948979870");
string detail = <string> err.detail()?.message;
test:assertTrue(stringutils:contains(detail, "Invalid mobile number. Resend the request as follows: If the " +
"mobile no is 0771234567, send POST request to '/sms' with JSON payload '{\"username\":\"myuser\", " +
"\"mobile\":\"0771234567\"}'"));
//error err = <error> validate("07161811948979870");
//string detail = <string> err.detail()?.message;
//test:assertTrue(stringutils:contains(detail, "Invalid mobile number. Resend the request as follows: If the " +
// "mobile no is 0771234567, send POST request to '/sms' with JSON payload '{\"username\":\"myuser\", " +
// "\"mobile\":\"0771234567\"}'"));

// validate invalid local numbers with non numeric chars
err = <error> validate("07161811AB");
detail = <string> err.detail()?.message;
error err = <error> validate("07161811AB");
string detail = <string> err.detail()?.message;
test:assertTrue(stringutils:contains(detail, "Invalid mobile number. Given mobile number contains non numeric " +
"characters: 07161811AB"));

Expand Down
5 changes: 3 additions & 2 deletions distributor/src/distributor/website.bal
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ service mediaWebsite on mediaListener {
return caller->ok(r.jsonResult);
} else if format == "html" {
http:Response hr = new;
boolean sorted = (r.jsonResult.'type == RN_SI || r.jsonResult.'type == RN_VS ||
r.jsonResult.'type == RN_VSN) ? true : false;
string resultType = r.jsonResult.'type.toString();
boolean sorted = (resultType == RN_SI || resultType == RN_V || resultType == RN_VS ||
resultType == RN_VSN) ? true : false;
hr.setTextPayload(<@untainted>check generateHtml(election, r.jsonResult, sorted));
hr.setContentType("text/html");
return caller->ok(hr);
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions distributor/web/info.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

****** IMPORTANT *******
**
** The latest version of the subscriber JAR is subscriber-20200728-II.jar
** The latest version of the subscriber JAR is subscriber-20200731-I.jar
**
** Available at https://github.com/ECLK/Results-Dist/releases/tag/v2020-07-28-II
** Available at https://github.com/ECLK/Results-Dist/releases/tag/v2020-07-31-I
**
****** IMPORTANT *******

Run it as follows:

java -jar subscriber-20200728-II.jar [options]
java -jar subscriber-20200731-I.jar [options]

where options are:
-username=name my username for authentication
Expand Down
4 changes: 3 additions & 1 deletion subscriber/src/subscriber/constants.bal
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const LEVEL_NF = "NATIONAL-FINAL";
const WANT_IMAGE = "image=true";
const WANT_AWAIT_RESULTS = "await=true";

const MY_VERSION = "2020-07-28-II";
const MY_VERSION = "2020-07-31-I";

const UNDERSOCRE = "_";
const COLON = ":";
Expand All @@ -24,8 +24,10 @@ const ELECTION_TYPE_PARLIAMENTARY = "PARLIAMENTARY";

const RP_V = "RP_V";
const RE_VI = "RE_VI";
const RE_V = "RE_V";
const RE_S = "RE_S";
const RN_SI = "RN_SI";
const RN_V = "RN_V";
const RN_VS = "RN_VS";
const RN_VSN = "RN_VSN";
const RE_SC = "RE_SC";
Expand Down
6 changes: 4 additions & 2 deletions subscriber/src/subscriber/genhtml.bal
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ function generateParliamentaryResultHtml(string electionCode, map<json> result,
" POLLING DIVISION (" + <string>result.pd_code + ") RESULT"; }
RE_VI => { resTypeHeading = <string>result.ed_name + " ELECTORAL DISTRICT CUMULATIVE RESULTS AT " +
<string>result.timestamp; }
RE_V => { resTypeHeading = <string>result.ed_name + " ELECTORAL DISTRICT VOTES RESULT"; }
RE_S => { resTypeHeading = <string>result.ed_name + " ELECTORAL DISTRICT SEAT ALLOCATION RESULT"; }
RN_SI => { resTypeHeading = "ALL ISLAND CUMULATIVE SEAT ALLOCATION RESULTS AT " + <string>result.timestamp; }
RN_V => { resTypeHeading = "ALL ISLAND VOTES RESULT"; }
RN_VS => { resTypeHeading = "ALL ISLAND VOTES + SEAT ALLOCATION RESULT"; }
RN_VSN => { resTypeHeading = "ALL ISLAND VOTES + SEAT ALLOCATION + NATIONAL LIST ALLOCATION RESULT"; }
RE_SC => { resTypeHeading = <string>result.ed_name + " ELECTORAL DISTRICT SEATS ALLOCATED CANDIDATES"; }
Expand All @@ -110,7 +112,7 @@ function generateParliamentaryResultHtml(string electionCode, map<json> result,
"<th>Name of Party</th>" +
"<th class='text-center'>Party Abbreviaton</th>";

if ('type == RP_V || 'type == RE_VI || 'type == RN_VS || 'type == RN_VSN) {
if ('type == RP_V || 'type == RE_VI || 'type == RE_V || 'type == RN_V || 'type == RN_VS || 'type == RN_VSN) {
body += "<th class='text-right'>Votes Received</th>" +
"<th class='text-right'>Percentage</th>";
}
Expand All @@ -128,7 +130,7 @@ function generateParliamentaryResultHtml(string electionCode, map<json> result,
"<td>" + <string>pr.party_name + "</td>" +
"<td class='text-center'>" + <string>pr.party_code +"</td>";

if ('type == RP_V || 'type == RE_VI || 'type == RN_VS || 'type == RN_VSN) {
if ('type == RP_V || 'type == RE_VI || 'type == RE_V || 'type == RN_V || 'type == RN_VS || 'type == RN_VSN) {
body += "<td class='text-right'>" + commaFormatInt(<int>pr.vote_count) + "</td>" +
"<td class='text-right'>" + <string>pr.vote_percentage + "</td>";
}
Expand Down
Loading

0 comments on commit ce1330f

Please sign in to comment.