-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserRegistration.java
185 lines (173 loc) · 6.19 KB
/
UserRegistration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.capgemini.userregistration;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.Scanner;
public class UserRegistration {
private static final Logger LOG = LogManager.getLogger(UserRegistration.class);
public boolean validateFirstName(String firstName) {
return firstName.matches("^[A-Z]{1}[a-z]{2,}$");
}
public boolean validateLastName(String lastName) {
return lastName.matches("^[A-Z]{1}[a-z]{2,}$");
}
public boolean validateEmail(String emailId) {
return emailId.matches("^[a-zA-Z0-9_]+([.+-]{1}[a-zA-Z0-9_]+)*[@]{1}[a-zA-Z0-
9]+[.]{1}[a-zA-Z0-9]{2,}([.]{1}[a-zA-Z]{2,})?$");
}
public boolean validateMobileNo(String mobileNo) {
return mobileNo.matches("^[0-9]{2}[ ][0-9]{10}$");
}
public boolean validatePassword(String password) {
return password.matches("(?=^.{8,}$)(?=.[A-Z])(?=.[0-9])[a-zA-Z0-9][@#$%_][0-9azA-Z]");
}
public static void main( String[] args ) {
UserRegistration userRegistration = new UserRegistration();
Scanner sc = new Scanner(System.in);
LOG.info("First Name: ");
String firstName = sc.nextLine();
if(userRegistration.validateFirstName(firstName))
LOG.info("Valid First Name");
else
LOG.info("Invalid First Name");
LOG.info("Last Name: ");
String lastName = sc.nextLine();
if(userRegistration.validateLastName(lastName))
LOG.info("Valid Last Name");
else
LOG.info("Invalid Last Name");
LOG.info("Email ID: ");
String emailId = sc.nextLine();
if(userRegistration.validateEmail(emailId))
LOG.info("Valid Email Id");
else
LOG.info("Invalid Email Id");
LOG.info("Mobile No: ");
String mobileNo = sc.nextLine();
if(userRegistration.validateMobileNo(mobileNo))
LOG.info("Valid Mobile No");
else
LOG.info("Invalid Mobile No");
LOG.info("Password: ");
String password = sc.nextLine();
if(userRegistration.validatePassword(password))
LOG.info("Valid Password");
else
LOG.info("Invalid Password");
}
}
package com.capgemini.userregistration;
import org.junit.Assert;
import org.junit.Test;
public class UserRegistrationTest {
@Test
public void givenFirstName_WhenProper_ShouldReturnTrue() {
UserRegistration userValidator = new UserRegistration();
boolean result = userValidator.validateFirstName("Manasi");
Assert.assertTrue(result);
}
@Test
public void givenFirstName_WhenNotStartCapital_ShouldReturnFalse() {
UserRegistration userValidator = new UserRegistration();
boolean result = userValidator.validateFirstName("Singh");
Assert.assertFalse(result);
}
@Test
public void givenFirstName_WhenContainOtherThanLetters_ShouldReturnFalse() {
UserRegistration userValidator = new UserRegistration();
boolean result = userValidator.validateFirstName("m@nasi");
Assert.assertFalse(result);
}
@Test
public void givenLastName_WhenProper_ShouldReturnTrue() {
UserRegistration userValidator = new UserRegistration();
boolean result = userValidator.validateLastName("Singh");
Assert.assertTrue(result);
}
@Test
public void givenLastName_WhenNotStartCapital_ShouldReturnFalse() {
UserRegistration userValidator = new UserRegistration();
boolean result = userValidator.validateLastName("mans&hs");
Assert.assertFalse(result);
}
@Test
public void givenLastName_WhenContainOtherThanLetters_ShouldReturnFalse() {
UserRegistration userValidator = new UserRegistration();
boolean result = userValidator.validateLastName("asnbm*bkj");
Assert.assertFalse(result);
}
@Test
public void givenEmailId_WhenPoper_ShouldReturnTrue() {
UserRegistration userValidator = new UserRegistration();
boolean result = userValidator.validateEmail("[email protected]");
Assert.assertTrue(result);
}
@Test
public void givenEmailId_WhenNotAtTheSymbol_ShouldReturnFalse() {
UserRegistration userValidator = new UserRegistration();
boolean result = userValidator.validateEmail("manasi.com");
Assert.assertFalse(result);
}
@Test
public void givenMobileNo_WhenProper_ShouldReturnTrue() {
UserRegistration userValidator = new UserRegistration();
boolean result = userValidator.validateMobileNo("91 8458587325");
Assert.assertTrue(result);
}
@Test
public void givenMobileNo_WhenNotProper_ShouldReturnFalse() {
UserRegistration userValidator = new UserRegistration();
boolean result = userValidator.validateMobileNo("21477784");
Assert.assertFalse(result);
}
@Test
public void givenPassword_WhenProper_ShouldReturnTrue() {
UserRegistration userValidator = new UserRegistration();
boolean result = userValidator.validatePassword("Manasi@29230");
Assert.assertTrue(result);
}
@Test
public void givenPassword_WhenNotProper_ShouldReturnFalse() {
UserRegistration userValidator = new UserRegistration();
boolean result = userValidator.validatePassword("maask89");
Assert.assertFalse(result);
}
}
package com.capgemini.userregistration;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(Parameterized.class)
public class ValidateEmail {
private String givenEmailId;
private boolean expectedValidation;
/**
* @param givenEmailId
* @param expectedValidation
*/
public ValidateEmail(String givenEmailId, boolean expectedValidation) {
super();
this.givenEmailId = givenEmailId;
this.expectedValidation = expectedValidation;
}
@Parameterized.Parameters
public static Collection input() {
return Arrays.asList(new Object[][] {{"[email protected]", true}, {"[email protected]", true}, {"[email protected]", true}, {"[email protected]", true},
{"[email protected]", true}, {"abc", false}, {"[email protected]", false}, {"[email protected]", false},
{"abc()@gmail.com", false}, {"abc@%.com", false}, {"[email protected]", false},
{"[email protected]", false}});
}
/**
*
*/
@Test
public void validateEmail() {
UserRegistration emailValidator = new UserRegistration();
assertEquals(expectedValidation, emailValidator.validateEmail(givenEmailId));
}
}