package fi.hsl.parkandride.core.service;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class PasswordValidatorTest {
    private final String password;
    private final boolean isExpectedToBeValid;

    public PasswordValidatorTest(String password, boolean isExpectedToBeValid) {
        this.password = password;
        this.isExpectedToBeValid = isExpectedToBeValid;
    }

    @Parameters
    public static Object[][] data() {
        return new Object[][]{
                { "i2E45678", true },          // min length
                { "i2E456789012345", true },   // max length
                { "å2Ä%&$)[^@#$%_-", true },   // å,ä,ö + special chars

                { null, false},
                { "12E45678", false },         // missing lowercase letter
                { "i2345678", false },         // missing uppercase letter
                { "i2E4 5678", false },        // whitespace inside
                { "i2E4567", false },          // too short
                { "i2E4567890123456", false }, // too long
        };
    }

    @Test
    public void testPassword() {
        assertIsValid(
                () -> PasswordValidator.validate(password),
                isExpectedToBeValid,
                password + " was " + (!isExpectedToBeValid ? "valid" : "invalid")
        );
    }

    private static void assertIsValid(Runnable r, boolean isValid, String message) {
        try {
            r.run();
            if (!isValid) {
                Assert.fail(message);
            }
        } catch (ValidationException e) {
            if (isValid) {
                Assert.fail(message);
            }
        }
    }
}
@@ -35,16 +35,104 @@
 rvice;%0A%0A
+import static org.junit.Assert.assertFalse;%0Aimport static org.junit.Assert.assertTrue;%0A%0A
 import o
@@ -1342,30 +1342,21 @@
         
-assertIsValid(
+try %7B
 %0A       
@@ -1363,18 +1363,8 @@
     
-     () -%3E
  Pas
@@ -1396,17 +1396,17 @@
 assword)
-,
+;
 %0A       
@@ -1414,48 +1414,40 @@
     
-    isExpectedToBeValid,%0A               
+assertTrue(%22Invalid password '%22+
  pas
@@ -1459,19 +1459,22 @@
  + %22
+'
  was 
-%22 + (!
+valid%22, 
 isEx
@@ -1492,370 +1492,147 @@
 alid
- ? %22valid%22 : %22invalid%22)%0A        );%0A    %7D%0A%0A    private static void assertIsValid(Runnable r, boolean isValid, String message) %7B%0A        try %7B%0A            r.run();%0A            if (!isValid) %7B%0A                Assert.fail(message);%0A            %7D%0A        %7D catch (ValidationException e) %7B%0A            if (isValid) %7B%0A                Assert.fail(message);%0A            %7D
+);%0A        %7D catch (ValidationException e) %7B%0A            assertFalse(%22Valid password '%22 + password + %22' was invalid%22, isExpectedToBeValid);
 %0A   

simplify password validator test