# Adding SIN parser and generator into Katalon

How would I add this SIN parser and generator into Katalon: GitHub - wealthsimple/social-insurance-number: Canadian SIN (Social Insurance Number) parser and generator

I will let the Modulators tell if you can add the “parser and generator” to KS, however, if you want to make your own Keyword, you only need to:

1. add the sum of all odd positions of the SIN except the last one (the last position is a calculated number–see step 4),
2. add the sum of twice (x 2) of each even position (if the multiplied number is larger than 9, subtract 9 from the number),
3. take a modulus of 10 of the addition of both the even and odd calculation,
4. subtract the resultant of the modulus from 10–this is the last number of the SIN. So if the remainder of the modulus from Step 3 was 4, then the subtraction would give: 10 - 4 or a final number of 6. Edit: If final number is 10, then final number is 0 (zero).
Maybe like:

Something that returns true or false if last reference number of SIN is the correct digit.

@Keyword
public boolean verifySINMatch (String sin) {

int test = 0;
int total = 0;
def value = "";
if (!sin.matches("\\d{9}")) {
return false;
} else {
def ref = sin.substring(sin.length() - 1, sin.length());

for (int pos = 0; pos < sin.length() - 1; pos++) {
value = sin.substring(pos, pos + 1);

switch (pos) {
case 0: case 2: case 4: case 6:
// since strings start count at 0, not 1, these are odds
total += Integer.parseInt(value);

break;
case 1: case 3: case 5: case 7:
// since strings start count at 0, not 1, these are evens
test = Integer.parseInt(value) * 2;
test = (test > 9) ? test - 9 : test;
total += test;

break;
}
}

// if resultant here is zero, then make it 10 so final below is zero
test = ((total % 10) == 0) ? 10 : (total % 10);

def finalRef = 10 - test;

return WebUI.verifyEqual(finalRef, ref, FailureHandling.OPTIONAL)
}
}

You can create another Keyword to generate a SIN–just concatenate together 8 random number generated numbers and then calculate the final one.

Maybe like:
@Keyword
public String generateSIN() {

int upperLimit = 9
int choice = 0
String sin = ""

Random rand = new Random()

for (int cnt = 0; cnt < 8; cnt++) {
choice = rand.nextInt(upperLimit)
sin = sin.concat(choice.toString())
}

sin = sin.concat(calculateLastDigit(sin))

return sin
}

private String calculateLastDigit(String partSin) {
int test = 0
int total = 0
def value = ""

for (int pos = 0; pos < partSin.length(); pos++) {
value = partSin.substring(pos, pos + 1)

switch (pos) {
case 0: case 2: case 4: case 6:
// since strings start count at 0, not 1, these are odds
total += Integer.parseInt(value)

break

case 1: case 3: case 5: case 7:
// since strings start count at 0, not 1, these are evens
test = Integer.parseInt(value) * 2
test = (test > 9) ? test - 9 : test
total += test

break
}
}
// if resultant here is zero, then make it 10 so final below is zero
test = ((total % 10) == 0) ? 10 : (total % 10)

def finalRef = 10 - test

return finalRef.toString()
}

Hi @grylion54 (Mike),
Thank you very much for your detailed response. I will give your solution a whirl.

Best regards,
Dave