Skip to content

Commit ce1330f

Browse files
authored
Merge pull request #165 from chamil321/test-fail
Introduce RE_V and RN_V result types
2 parents 6b53487 + 909ac9c commit ce1330f

File tree

13 files changed

+205
-55
lines changed

13 files changed

+205
-55
lines changed

distributor/src/distributor/constants.bal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ const ELECTION_TYPE_PARLIAMENTARY = "PARLIAMENTARY";
55

66
const RP_V = "RP_V";
77
const RE_VI = "RE_VI";
8+
const RE_V = "RE_V";
89
const RE_S = "RE_S";
910
const RN_SI = "RN_SI";
11+
const RN_V = "RN_V";
1012
const RN_VS = "RN_VS";
1113
const RN_VSN = "RN_VSN";
1214
const RE_SC = "RE_SC";

distributor/src/distributor/genhtml.bal

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,10 @@ function generateParliamentaryResultHtml(string electionCode, map<json> result,
108108
" POLLING DIVISION (" + <string>result.pd_code + ") RESULT"; }
109109
RE_VI => { resTypeHeading = <string>result.ed_name + " ELECTORAL DISTRICT CUMULATIVE RESULTS AT " +
110110
<string>result.timestamp; }
111+
RE_V => { resTypeHeading = <string>result.ed_name + " ELECTORAL DISTRICT VOTES RESULT"; }
111112
RE_S => { resTypeHeading = <string>result.ed_name + " ELECTORAL DISTRICT SEAT ALLOCATION RESULT"; }
112113
RN_SI => { resTypeHeading = "ALL ISLAND CUMULATIVE SEAT ALLOCATION RESULTS AT " + <string>result.timestamp; }
114+
RN_V => { resTypeHeading = "ALL ISLAND VOTES RESULT"; }
113115
RN_VS => { resTypeHeading = "ALL ISLAND VOTES + SEAT ALLOCATION RESULT"; }
114116
RN_VSN => { resTypeHeading = "ALL ISLAND VOTES + SEAT ALLOCATION + NATIONAL LIST ALLOCATION RESULT"; }
115117
RE_SC => { resTypeHeading = <string>result.ed_name + " ELECTORAL DISTRICT SEATS ALLOCATED CANDIDATES"; }
@@ -125,7 +127,7 @@ function generateParliamentaryResultHtml(string electionCode, map<json> result,
125127
"<th>Name of Party</th>" +
126128
"<th class='text-center'>Party Abbreviaton</th>";
127129

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

147-
if ('type == RP_V || 'type == RE_VI || 'type == RN_VS || 'type == RN_VSN) {
149+
if ('type == RP_V || 'type == RE_VI || 'type == RE_V || 'type == RN_V || 'type == RN_VS || 'type == RN_VSN) {
148150
body += "<td class='text-right'>" + commaFormatInt(<int>pr.vote_count) + "</td>" +
149151
"<td class='text-right'>" + <string>pr.vote_percentage + "</td>";
150152
}
@@ -238,7 +240,7 @@ function sortParliamentaryByPartyResults(json[] unsorted, string resultType) ret
238240
return sortByPartyResultsBySeatCount(unsorted);
239241
}
240242
}
241-
// RN_VS, RN_VSN
243+
// RN_V, RN_VS, RN_VSN
242244
return sortByPartyResultsByVoteCount(unsorted);
243245
}
244246

distributor/src/distributor/messenger.bal

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,15 @@ function sendSMS(string message, string resultId) {
6565
}
6666
string logMessage = "Sending SMS for " + resultId;
6767
log:printInfo(logMessage);
68-
foreach string targetMobile in mobileSubscribers {
69-
log:printInfo(logMessage + " to " + targetMobile);
70-
var response = smsClient->sendSms(sourceDepartment, message, targetMobile);
71-
if response is error {
72-
log:printError("Message sending failed for \'" + targetMobile + "\'", response);
68+
string[] keys = mobileSubscribers.keys();
69+
foreach string userName in keys {
70+
string? targetMobile = mobileSubscribers[userName];
71+
if !(targetMobile is ()) {
72+
log:printInfo(logMessage + " to " + targetMobile);
73+
var response = smsClient->sendSms(sourceDepartment, message, targetMobile);
74+
if response is error {
75+
log:printError("Message sending failed for \'" + targetMobile + "\'", response);
76+
}
7377
}
7478
}
7579
}
@@ -81,7 +85,7 @@ function sendSMS(string message, string resultId) {
8185
function validate(string mobileNo) returns string|error {
8286
string mobile = <@untained> mobileNo.trim();
8387

84-
if (mobile.startsWith("+94") && mobile.length() == 12) {
88+
if (mobile.startsWith("+") && mobile.length() == 12) {
8589
mobile = mobile.substring(1);
8690
}
8791

@@ -91,18 +95,19 @@ function validate(string mobileNo) returns string|error {
9195
log:printError(errorMsg);
9296
return error(ERROR_REASON, message = errorMsg);
9397
}
94-
95-
if (mobile.startsWith("0") && mobile.length() == 10) {
96-
return mobile;
97-
}
98-
if (mobile.startsWith("94") && mobile.length() == 11) {
99-
return mobile;
100-
}
101-
// Allow only the local mobile numbers to register via public API. International number are avoided.
102-
log:printError("Invalid mobile number : " + mobile);
103-
return error(ERROR_REASON, message = "Invalid mobile number. Resend the request as follows: If the " +
104-
"mobile no is 0771234567, send POST request to \'/sms\' with JSON payload " +
105-
"\'{\"username\":\"myuser\", \"mobile\":\"0771234567\"}\'");
98+
return mobile;
99+
100+
//if (mobile.startsWith("0") && mobile.length() == 10) {
101+
// return mobile;
102+
//}
103+
//if (mobile.startsWith("94") && mobile.length() == 11) {
104+
// return mobile;
105+
//}
106+
//// Allow only the local mobile numbers to register via public API. International number are avoided.
107+
//log:printError("Invalid mobile number : " + mobile);
108+
//return error(ERROR_REASON, message = "Invalid mobile number. Resend the request as follows: If the " +
109+
// "mobile no is 0771234567, send POST request to \'/sms\' with JSON payload " +
110+
// "\'{\"username\":\"myuser\", \"mobile\":\"0771234567\"}\'");
106111
}
107112

108113
# Register recipient in the mobileSubscribers list and persist in the smsRecipients db table.

distributor/src/distributor/tests/resources/contact1.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"username":"newuser1",
4-
"mobile":"0711234561"
4+
"mobile":"07112345611111111"
55
},
66
{
77
"username":"newuser2",

distributor/src/distributor/tests/resources/contact2.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"username":"newuser1",
4-
"mobile":"+00771234562"
4+
"mobile":"+00771234567976902"
55
},
66
{
77
"username":"newuser2",

distributor/src/distributor/tests/sms_publisher_test.bal

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function testSubscriberRegistration() {
100100
// Test bulk success registration
101101
req = new;
102102
req.setFileAsPayload("src/distributor/tests/resources/contact1.json");
103-
response = httpEndpoint->post("/sms/addall", req);
103+
response = httpEndpoint->post("/sms/all", req);
104104
if (response is http:Response) {
105105
var result = response.getTextPayload();
106106
if (result is string) {
@@ -115,11 +115,11 @@ function testSubscriberRegistration() {
115115
// Test bulk registration with invalid nos
116116
req = new;
117117
req.setFileAsPayload("src/distributor/tests/resources/contact2.json");
118-
response = httpEndpoint->post("/sms/addall", req);
118+
response = httpEndpoint->post("/sms/all", req);
119119
if (response is http:Response) {
120120
var result = response.getTextPayload();
121121
if (result is string) {
122-
test:assertEquals(result, "Validation failed: invalid recipient mobile no: newuser1:+00771234562");
122+
test:assertEquals(result, "Validation failed: invalid recipient mobile no: newuser1:+00771234567976902");
123123
} else {
124124
test:assertFail(msg = "Invalid response message:");
125125
}
@@ -130,7 +130,7 @@ function testSubscriberRegistration() {
130130
// Test bulk registration with malformed JSON
131131
req = new;
132132
req.setFileAsPayload("src/distributor/tests/resources/contact3.json");
133-
response = httpEndpoint->post("/sms/addall", req);
133+
response = httpEndpoint->post("/sms/all", req);
134134
if (response is http:Response) {
135135
var result = response.getTextPayload();
136136
if (result is string) {
@@ -156,7 +156,7 @@ function testResetRecipients() {
156156
}
157157
});
158158
http:Request req = new;
159-
var response = httpEndpoint->delete("/sms/reset", req);
159+
var response = httpEndpoint->delete("/sms/all", req);
160160
if (response is http:Response) {
161161
var result = response.getTextPayload();
162162
if (result is string) {
@@ -182,15 +182,15 @@ function testValidateFunction() {
182182
test:assertTrue(validate("+94716181195") == "94716181195", msg = "Failed assertion : 94716181195");
183183

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

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

distributor/src/distributor/website.bal

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,9 @@ service mediaWebsite on mediaListener {
122122
return caller->ok(r.jsonResult);
123123
} else if format == "html" {
124124
http:Response hr = new;
125-
boolean sorted = (r.jsonResult.'type == RN_SI || r.jsonResult.'type == RN_VS ||
126-
r.jsonResult.'type == RN_VSN) ? true : false;
125+
string resultType = r.jsonResult.'type.toString();
126+
boolean sorted = (resultType == RN_SI || resultType == RN_V || resultType == RN_VS ||
127+
resultType == RN_VSN) ? true : false;
127128
hr.setTextPayload(<@untainted>check generateHtml(election, r.jsonResult, sorted));
128129
hr.setContentType("text/html");
129130
return caller->ok(hr);

distributor/web/info.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
****** IMPORTANT *******
44
**
5-
** The latest version of the subscriber JAR is subscriber-20200728-II.jar
5+
** The latest version of the subscriber JAR is subscriber-20200731-I.jar
66
**
7-
** Available at https://github.com/ECLK/Results-Dist/releases/tag/v2020-07-28-II
7+
** Available at https://github.com/ECLK/Results-Dist/releases/tag/v2020-07-31-I
88
**
99
****** IMPORTANT *******
1010

1111
Run it as follows:
1212

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

1515
where options are:
1616
-username=name my username for authentication

subscriber/src/subscriber/constants.bal

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const LEVEL_NF = "NATIONAL-FINAL";
99
const WANT_IMAGE = "image=true";
1010
const WANT_AWAIT_RESULTS = "await=true";
1111

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

1414
const UNDERSOCRE = "_";
1515
const COLON = ":";
@@ -24,8 +24,10 @@ const ELECTION_TYPE_PARLIAMENTARY = "PARLIAMENTARY";
2424

2525
const RP_V = "RP_V";
2626
const RE_VI = "RE_VI";
27+
const RE_V = "RE_V";
2728
const RE_S = "RE_S";
2829
const RN_SI = "RN_SI";
30+
const RN_V = "RN_V";
2931
const RN_VS = "RN_VS";
3032
const RN_VSN = "RN_VSN";
3133
const RE_SC = "RE_SC";

0 commit comments

Comments
 (0)