And the ministry of communications development in the name of muhammad al khorezmi tashkent university of information technologies


Download 175.21 Kb.
bet2/8
Sana09.04.2023
Hajmi175.21 Kb.
#1343718
1   2   3   4   5   6   7   8
Bog'liq
Mustaqil Ish-1

We should know that we need PasswordData and PasswordValidator objects to start validating passwords. We've already discussed PasswordData. Let's create PasswordValidator now.
Firstly, we should define a set of rules for password validation. We have to pass them to constructor while creating a PasswordValidator object:

PasswordValidator passwordValidator = new PasswordValidator(new LengthRule(5));

There are two ways of passing our password to a PasswordData object. We pass it to either the constructor or the setter method:


PasswordData passwordData = new PasswordData("1234");


PasswordData passwordData2 = new PasswordData();
passwordData.setPassword("1234");

We can validate our password by calling validate() method on PasswordValidator:


RuleResult validate = passwordValidator.validate(passwordData);
As a result, we'll get a RuleResult object.

3.3. Rule Result


RuleResult holds interesting information about a validation process. It comes as a result of the validate() method.
First of all, it can tell us whether the password is valid:
Assert.assertEquals(false, validate.isValid());


Moreover, we can learn what errors are returned when the password is invalid. Error codes and validation descriptions are kept in RuleResultDetail:

RuleResultDetail ruleResultDetail = validate.getDetails().get(0);
Assert.assertEquals("TOO_SHORT", ruleResultDetail.getErrorCode());
Assert.assertEquals(5, ruleResultDetail.getParameters().get("minimumLength"));
Assert.assertEquals(5, ruleResultDetail.getParameters().get("maximumLength"));

Finally, we can explore the password validation's metadata with RuleResultMetadata:




Integer lengthCount = validate
.getMetadata()
.getCounts()
.get(RuleResultMetadata.CountCategory.Length);
Assert.assertEquals(Integer.valueOf(4), lengthCount);



Download 175.21 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling