-
Notifications
You must be signed in to change notification settings - Fork 909
Add unit tests for Person bean #13963
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ | |
| *Unless required by applicable law or agreed to in writing, | ||
| *software distributed under the License is distributed on an | ||
| *"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| *KIND, either express or implied. See the License for the | ||
| *KIND, either express or implied. See the License for the | ||
| *specific language governing permissions and limitations | ||
| *under the License. | ||
| */ | ||
|
|
@@ -30,70 +30,94 @@ | |
|
|
||
| @Service | ||
| public class PeopleService { | ||
| private ConcurrentMap< String, Person> persons = new ConcurrentHashMap< String, Person >(); | ||
| public void setInitPeople() | ||
| { | ||
|
|
||
| for(int count =1;count <=10; count ++) | ||
| { | ||
| Person newPerson= new Person(); | ||
| newPerson.setEmail(String.format("test-%d@wso2.com",count)); | ||
| newPerson.setFirstName(String.format("testUser%d",count)); | ||
| newPerson.setLastName(String.format("testLasrName%d",count)); | ||
| persons.putIfAbsent(String.format("test-%d@wso2.com",count),newPerson) ; | ||
|
|
||
| private ConcurrentMap<String, Person> persons = new ConcurrentHashMap<>(); | ||
|
|
||
| public void setInitPeople() { | ||
| for (int count = 1; count <= 10; count++) { | ||
| Person newPerson = new Person(); | ||
| newPerson.setEmail(String.format("test-%d@wso2.com", count)); | ||
| newPerson.setFirstName(String.format("testUser%d", count)); | ||
| newPerson.setLastName(String.format("testLasrName%d", count)); | ||
| persons.putIfAbsent(String.format("test-%d@wso2.com", count), newPerson); | ||
| } | ||
| } | ||
| public Collection< Person > getPeople( int page, int pageSize ) { | ||
| Collection< Person > person = new ArrayList< Person >( pageSize ); | ||
|
|
||
| for( int index = 0; index < pageSize; ++index ) { | ||
| person.add( new Person( String.format( "person+%d@at.com", ( pageSize * ( page - 1 ) + index + 1 ) ) ) ); | ||
| public Collection<Person> getPeople(int page, int pageSize) { | ||
|
|
||
| Collection<Person> person = new ArrayList<>(pageSize); | ||
|
|
||
| for (int index = 0; index < pageSize; ++index) { | ||
| person.add(new Person( | ||
| String.format("person+%d@at.com", | ||
| (pageSize * (page - 1) + index + 1)))); | ||
|
Comment on lines
+50
to
+53
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing validation for With 🛡️ Proposed guard public Collection<Person> getPeople(int page, int pageSize) {
+ if (page < 1) {
+ throw new IllegalArgumentException("page must be >= 1");
+ }
+ if (pageSize < 0) {
+ throw new IllegalArgumentException("pageSize must be >= 0");
+ }
Collection<Person> person = new ArrayList<>(pageSize);🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| setInitPeople(); | ||
| return person; | ||
| } | ||
|
Comment on lines
+46
to
58
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The
The idiomatic Spring pattern is to annotate ♻️ Proposed fix — move initialization to `@PostConstruct`+import javax.annotation.PostConstruct;
...
+ `@PostConstruct`
public void setInitPeople() {
for (int count = 1; count <= 10; count++) {
...
}
}
public Collection<Person> getPeople(int page, int pageSize) {
Collection<Person> person = new ArrayList<>(pageSize);
for (int index = 0; index < pageSize; ++index) {
person.add(new Person(
String.format("person+%d@at.com",
(pageSize * (page - 1) + index + 1))));
}
- setInitPeople();
return person;
}🤖 Prompt for AI Agents |
||
|
|
||
| public Person addPerson( String email ) { | ||
| public Person addPerson(String email) { | ||
|
|
||
| return new Person( email ); | ||
| if (email == null || email.isEmpty()) { | ||
| throw new IllegalArgumentException("Email cannot be null or empty"); | ||
| } | ||
|
|
||
| return new Person(email); | ||
| } | ||
|
Comment on lines
+60
to
67
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: find . -type f -name "PeopleService.java" | head -5Repository: wso2/product-apim Length of output: 222 🏁 Script executed: cat -n ./all-in-one-apim/modules/integration/tests-common/backend-service/jaxrs-app/src/main/java/org/wso2/am/integration/services/jaxrs/peoplesample/PeopleService.javaRepository: wso2/product-apim Length of output: 4764 🏁 Script executed: #!/bin/bash
# Find REST resource files that might call these addPerson methods
find . -type f -name "*Resource.java" -o -name "*Controller.java" | grep -i peopleRepository: wso2/product-apim Length of output: 43 🏁 Script executed: #!/bin/bash
# Find all Java files that reference PeopleService
rg -l "PeopleService" --type=javaRepository: wso2/product-apim Length of output: 694 🏁 Script executed: cat -n ./all-in-one-apim/modules/integration/tests-common/backend-service/jaxrs-app/src/main/java/org/wso2/am/integration/services/jaxrs/peoplesample/PeopleRestService.javaRepository: wso2/product-apim Length of output: 6224 🏁 Script executed: #!/bin/bash
# Search for all calls to addPerson to confirm usage patterns
rg -n "addPerson\s*\(" --type=java -A2 -B2Repository: wso2/product-apim Length of output: 6320
The single-argument 🐛 Proposed fix — persist the person (mirroring the 3-arg overload) public Person addPerson(String email) {
if (email == null || email.isEmpty()) {
throw new IllegalArgumentException("Email cannot be null or empty");
}
- return new Person(email);
+ final Person person = new Person(email);
+ if (persons.putIfAbsent(email, person) != null) {
+ throw new PersonAlreadyExistsException(email);
+ }
+ return person;
}🤖 Prompt for AI Agents |
||
|
|
||
| public Person getByEmail( final String email ) { | ||
| final Person person = persons.get( email ); | ||
| if( person == null ) { | ||
| throw new PersonNotFoundException( email ); | ||
| public Person getByEmail(final String email) { | ||
|
|
||
| if (email == null || email.isEmpty()) { | ||
| throw new IllegalArgumentException("Email cannot be null or empty"); | ||
| } | ||
|
|
||
| final Person person = persons.get(email); | ||
|
|
||
| if (person == null) { | ||
| throw new PersonNotFoundException(email); | ||
| } | ||
|
|
||
| return person; | ||
| } | ||
|
|
||
| public boolean checkPersonByEmail(final String email) | ||
| { | ||
| boolean personExists= true; | ||
| final Person person = persons.get( email ); | ||
| if( person == null ) { | ||
| personExists=false; | ||
| public boolean checkPersonByEmail(final String email) { | ||
|
|
||
| if (email == null || email.isEmpty()) { | ||
| return false; | ||
| } | ||
| return personExists; | ||
|
|
||
| final Person person = persons.get(email); | ||
| return person != null; | ||
| } | ||
|
|
||
| public Person addPerson( final String email, final String firstName, final String lastName ) { | ||
| final Person person = new Person( email ); | ||
| person.setFirstName( firstName ); | ||
| person.setLastName( lastName ); | ||
| public Person addPerson(final String email, | ||
| final String firstName, | ||
| final String lastName) { | ||
|
|
||
| if( persons.putIfAbsent( email, person ) != null ) { | ||
| throw new PersonAlreadyExistsException( email ); | ||
| if (email == null || email.isEmpty()) { | ||
| throw new IllegalArgumentException("Email cannot be null or empty"); | ||
| } | ||
|
|
||
| return person; | ||
| } | ||
| final Person person = new Person(email); | ||
| person.setFirstName(firstName); | ||
| person.setLastName(lastName); | ||
|
|
||
| public void removePerson( final String email ) { | ||
| if( persons.remove( email ) == null ) { | ||
| throw new PersonNotFoundException( email ); | ||
| if (persons.putIfAbsent(email, person) != null) { | ||
| throw new PersonAlreadyExistsException(email); | ||
| } | ||
|
|
||
| return person; | ||
| } | ||
|
|
||
| public void removePerson(final String email) { | ||
|
|
||
| if (email == null || email.isEmpty()) { | ||
| throw new IllegalArgumentException("Email cannot be null or empty"); | ||
| } | ||
|
|
||
| if (persons.remove(email) == null) { | ||
| throw new PersonNotFoundException(email); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package org.wso2.am.integration.services.jaxrs.peoplesample.bean; | ||
|
|
||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
|
|
||
| public class PersonTest { | ||
|
|
||
| @Test | ||
| public void testPersonConstructorAndGetters() { | ||
|
|
||
| Person person = new Person(); | ||
|
|
||
| person.setEmail("john@example.com"); | ||
| person.setFirstName("John"); | ||
| person.setLastName("Doe"); | ||
|
|
||
| Assert.assertEquals("john@example.com", person.getEmail()); | ||
| Assert.assertEquals("John", person.getFirstName()); | ||
| Assert.assertEquals("Doe", person.getLastName()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPersonEquality() { | ||
|
|
||
| Person p1 = new Person(); | ||
| p1.setEmail("test@example.com"); | ||
|
|
||
| Person p2 = new Person(); | ||
| p2.setEmail("test@example.com"); | ||
|
|
||
| Assert.assertEquals(p1.getEmail(), p2.getEmail()); | ||
|
Comment on lines
+23
to
+31
|
||
| } | ||
|
Comment on lines
+22
to
+32
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Two concrete issues:
To meaningfully test equality, assert on the 🐛 Proposed fix- `@Test`
- public void testPersonEquality() {
-
- Person p1 = new Person();
- p1.setEmail("test@example.com");
-
- Person p2 = new Person();
- p2.setEmail("test@example.com");
-
- Assert.assertEquals(p1.getEmail(), p2.getEmail());
- }
+ `@Test`
+ public void testPersonEqualityBySameEmail() {
+ Person p1 = new Person();
+ p1.setEmail("test@example.com");
+
+ Person p2 = new Person();
+ p2.setEmail("test@example.com");
+
+ // Comparing email field values (expected, actual)
+ Assert.assertEquals("test@example.com", p1.getEmail());
+ Assert.assertEquals("test@example.com", p2.getEmail());
+ }
+
+ `@Test`
+ public void testPersonEqualityByObject() {
+ // Uncomment if Person overrides equals()/hashCode()
+ // Person p1 = new Person();
+ // p1.setEmail("test@example.com");
+ // Person p2 = new Person();
+ // p2.setEmail("test@example.com");
+ // Assert.assertEquals(p1, p2);
+ }🤖 Prompt for AI Agents |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo:
"testLasrName%d"→"testLastName%d"The misspelled last-name template will be stored in the
personsmap and could cause test assertions onlastNamefields to fail unexpectedly.🐛 Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents