Skip to content

Commit

Permalink
#813 - fixed bug in equals method. format fields should not be nullab…
Browse files Browse the repository at this point in the history
…le. fixed wrong tests.
  • Loading branch information
petmongrels committed Nov 8, 2024
1 parent 4eaa4dd commit 0a4f644
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.avni.server.application;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.validation.constraints.Null;
import org.avni.server.domain.*;
import org.avni.server.common.ValidationResult;
import org.avni.server.framework.hibernate.DeclarativeRuleUserType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
@Embeddable
public class Format {

@Column(name = "valid_format_regex", nullable = false)
@Column(name = "valid_format_regex")
private String regex;

@Column(name = "valid_format_description_key", nullable = false)
@Column(name = "valid_format_description_key")
private String descriptionKey;

public Format(){

}

public Format(String regex, String descriptionKey){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,21 @@ public boolean equals(Object o) {
return CHSBaseEntity.equals(this, o);
}

public static boolean equals(IdHolder a, Object o) {
if (a == o) return true;
public static boolean equals(IdHolder a, Object other) {
if (a == other) return true;

LazyInitializer lazyInitializer2 = HibernateProxy.extractLazyInitializer(o);
LazyInitializer lazyInitializer2 = HibernateProxy.extractLazyInitializer(other);

if (o == null) return false;
if (other == null) return false;
if (lazyInitializer2 != null && a.getClass() != lazyInitializer2.getImplementationClass()) return false;
if (lazyInitializer2 == null && a.getClass() != other.getClass()) return false;

IdHolder that = lazyInitializer2 == null ? (IdHolder) o : (IdHolder) lazyInitializer2.getImplementation();
IdHolder otherIdHolder = lazyInitializer2 == null ? (IdHolder) other : (IdHolder) lazyInitializer2.getImplementation();

if (that.getId() == null && a.getId() == null) return that == a;
if (otherIdHolder.getId() == null && a.getId() == null && otherIdHolder.getUuid() == null && a.getUuid() == null) return otherIdHolder == a;

if (a.getId() != null ? !a.getId().equals(that.getId()) : that.getId() != null) return false;
return a.getUuid() != null ? a.getUuid().equals(that.getUuid()) : that.getUuid() == null;
if (a.getId() != null ? !a.getId().equals(otherIdHolder.getId()) : otherIdHolder.getId() != null) return false;
return a.getUuid() != null ? a.getUuid().equals(otherIdHolder.getUuid()) : otherIdHolder.getUuid() == null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,32 +80,40 @@ public Format getValidFirstNameFormat() {
return subjectType.getValidFirstNameFormat();
}

public Format getValidLastNameFormat(){
public Format getValidLastNameFormat() {
return subjectType.getValidLastNameFormat();
}

public String getIconFileS3Key() {
return subjectType.getIconFileS3Key();
}

public boolean isAllowProfilePicture() { return subjectType.isAllowProfilePicture(); }
public boolean isAllowProfilePicture() {
return subjectType.isAllowProfilePicture();
}

public boolean isAllowMiddleName() {
return subjectType.isAllowMiddleName();
}

public boolean isAllowMiddleName() { return subjectType.isAllowMiddleName();}
public boolean isLastNameOptional() {
return subjectType.isLastNameOptional();
}
public Format getValidMiddleNameFormat() { return subjectType.getValidMiddleNameFormat();}

public Format getValidMiddleNameFormat() {
return subjectType.getValidMiddleNameFormat();
}

@Override
public DateTime getLastModifiedDateTime() {
Auditable lastModified = getLastModified(getSubjectType());
return lastModified.equals(this)?super.getLastModifiedDateTime(): lastModified.getLastModifiedDateTime();
return lastModified.equals(this) ? super.getLastModifiedDateTime() : lastModified.getLastModifiedDateTime();
}

@Override
public User getLastModifiedBy() {
Auditable lastModified = getLastModified(getSubjectType());
return lastModified.equals(this)?super.getLastModifiedBy(): lastModified.getLastModifiedBy();
return lastModified.equals(this) ? super.getLastModifiedBy() : lastModified.getLastModifiedBy();
}

public boolean isDirectlyAssignable() {
Expand All @@ -117,5 +125,7 @@ public boolean isVoided() {
return subjectType.isVoided() || super.isVoided();
}

public JsonObject getSettings() { return subjectType.getSettings(); }
public JsonObject getSettings() {
return subjectType.getSettings();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void setOrganisationId(Long organisationId) {

@JsonIgnore
public Auditable getLastModified(Auditable auditable) {
return super.getLastModifiedDateTime().isAfter(auditable.getLastModifiedDateTime()) ? this : auditable;
return super.getLastModifiedDateTime().isBefore(auditable.getLastModifiedDateTime()) ? auditable : this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ public static long getMilliSecondsDuration(LocalDateTime start, LocalDateTime en
return java.time.Duration.between(start, end).toMillis();
}

public static LocalDate toJodaDate(ZonedDateTime zonedDateTime) {
return DateTimeUtil.toJodaDateTime(zonedDateTime).toLocalDate();
}

public static LocalDate toJodaDate(java.time.Instant instant) {
if (instant == null) {
return null;
}
return DateTimeUtil.toJodaDateTime(instant).toLocalDate();
}

Expand All @@ -65,12 +64,6 @@ public static java.time.Instant toInstant(LocalDate localDate) {
return localDate.toDate().toInstant();
}

public static DateTime toJodaDateTime(ZonedDateTime zonedDateTime) {
return new DateTime(
zonedDateTime.toInstant().toEpochMilli(),
DateTimeZone.forTimeZone(TimeZone.getTimeZone(zonedDateTime.getZone())));
}

public static java.time.Instant toInstant(DateTime createdDateTime) {
if (createdDateTime == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,41 @@
public class CHSBaseEntityTest {
@Test
public void equals() {
assertEquals(entity(1l), entity(1l));
assertNotEquals(entity(1l), entity(2l));
assertEquals(entity(1L), entity(1L));
assertEquals(entity(1L, "a"), entity(1L, "a"));
assertEquals(entity(null, "a"), entity(null, "a"));

assertNotEquals(entity(1L), entity(2L));
assertNotEquals(new CHSBaseEntity(), new CHSBaseEntity());
assertNotEquals(entity(1l), new CHSBaseEntity());
assertNotEquals(new CHSBaseEntity(), entity(1l));
assertNotEquals(entity(1L), new CHSBaseEntity());
assertNotEquals(new CHSBaseEntity(), entity(1L));
assertNotEquals(otherTypeOfEntity(1L), entity(1L));
assertNotEquals(new OtherTypeOfEntity(), new CHSBaseEntity());
assertEquals(entity(1L), entity(1L));
}

private static CHSBaseEntity entity(Long id) {
return entity(id, null);
}

private static CHSBaseEntity entity(long id) {
private static CHSBaseEntity entity(Long id, String uuid) {
CHSBaseEntity chsBaseEntity = new CHSBaseEntity();
chsBaseEntity.setId(id);
chsBaseEntity.setUuid(uuid);
return chsBaseEntity;
}

private static OtherTypeOfEntity otherTypeOfEntity(Long id) {
return otherTypeOfEntity(id, null);
}

private static OtherTypeOfEntity otherTypeOfEntity(Long id, String uuid) {
OtherTypeOfEntity chsBaseEntity = new OtherTypeOfEntity();
chsBaseEntity.setId(id);
chsBaseEntity.setUuid(uuid);
return chsBaseEntity;
}

static class OtherTypeOfEntity extends CHSBaseEntity {
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.avni.server.domain;

import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Test;

import java.util.HashSet;
Expand All @@ -9,18 +10,24 @@
import static org.junit.Assert.assertEquals;

public class OrganisationAwareEntityTest {
private User user1;
private User user2;


@Test
public void test_getLastModifiedForSubject(){
User user1 = new User();
@Before
public void setUp() {
user1 = new User();
user1.setId(1l);
user1.setUsername("user1");
user1.setName("user1");

User user2 = new User();
user2 = new User();
user2.setId(2l);
user2.setUsername("user2");
user2.setName("user2");
}

@Test
public void test_getLastModifiedForSubject() {
DateTime lastModifiedDateTime1 = new DateTime("2023-05-06T10:11:12.123");
DateTime lastModifiedDateTime2 = new DateTime("2023-05-05T10:11:12.123");

Expand All @@ -40,31 +47,22 @@ public void test_getLastModifiedForSubject(){
subjectType.setOperationalSubjectTypes(operationalSubjectTypeSet);
operationalSubjectType.setSubjectType(subjectType);

assertEquals(operationalSubjectType.getLastModifiedDateTime(),lastModifiedDateTime1);
assertEquals(operationalSubjectType.getLastModifiedBy(),user1);
assertEquals(operationalSubjectType.getLastModifiedDateTime(), lastModifiedDateTime1);
assertEquals(operationalSubjectType.getLastModifiedBy(), user1);

subjectType.setLastModifiedDateTime(lastModifiedDateTime2);
operationalSubjectType.setLastModifiedDateTime(lastModifiedDateTime1);

assertEquals(operationalSubjectType.getLastModifiedDateTime(),lastModifiedDateTime1);
assertEquals(operationalSubjectType.getLastModifiedBy(),user2);
assertEquals(operationalSubjectType.getLastModifiedDateTime(), lastModifiedDateTime1);
assertEquals(operationalSubjectType.getLastModifiedBy(), user2);

operationalSubjectType.setLastModifiedDateTime(lastModifiedDateTime2);
assertEquals(operationalSubjectType.getLastModifiedDateTime(),lastModifiedDateTime2);
assertEquals(operationalSubjectType.getLastModifiedBy(),user2);

assertEquals(operationalSubjectType.getLastModifiedDateTime(), lastModifiedDateTime2);
assertEquals(operationalSubjectType.getLastModifiedBy(), user2);
}

@Test
public void test_getLastModifiedForProgram(){
User user1 = new User();
user1.setUsername("user1");
user1.setName("user1");

User user2 = new User();
user2.setUsername("user2");
user2.setName("user2");

public void test_getLastModifiedForProgram() {
DateTime lastModifiedDateTime1 = new DateTime("2023-05-06T10:11:12.123");
DateTime lastModifiedDateTime2 = new DateTime("2023-05-05T10:11:12.123");

Expand All @@ -84,31 +82,23 @@ public void test_getLastModifiedForProgram(){
program.setOperationalPrograms(operationalProgramTypeSet);
operationalProgramType.setProgram(program);

assertEquals(operationalProgramType.getLastModifiedDateTime(),lastModifiedDateTime1);
assertEquals(operationalProgramType.getLastModifiedBy(),user1);
assertEquals(operationalProgramType.getLastModifiedDateTime(), lastModifiedDateTime1);
assertEquals(operationalProgramType.getLastModifiedBy(), user1);

program.setLastModifiedDateTime(lastModifiedDateTime2);
operationalProgramType.setLastModifiedDateTime(lastModifiedDateTime1);

assertEquals(operationalProgramType.getLastModifiedDateTime(),lastModifiedDateTime1);
assertEquals(operationalProgramType.getLastModifiedBy(),user2);
assertEquals(operationalProgramType.getLastModifiedDateTime(), lastModifiedDateTime1);
assertEquals(operationalProgramType.getLastModifiedBy(), user2);

operationalProgramType.setLastModifiedDateTime(lastModifiedDateTime2);
assertEquals(operationalProgramType.getLastModifiedDateTime(),lastModifiedDateTime2);
assertEquals(operationalProgramType.getLastModifiedBy(),user2);
assertEquals(operationalProgramType.getLastModifiedDateTime(), lastModifiedDateTime2);
assertEquals(operationalProgramType.getLastModifiedBy(), user2);

}

@Test
public void test_getLastModifiedForEncounter(){
User user1 = new User();
user1.setUsername("user1");
user1.setName("user1");

User user2 = new User();
user2.setUsername("user2");
user2.setName("user2");

public void test_getLastModifiedForEncounter() {
DateTime lastModifiedDateTime1 = new DateTime("2023-05-06T10:11:12.123");
DateTime lastModifiedDateTime2 = new DateTime("2023-05-05T10:11:12.123");

Expand All @@ -128,18 +118,18 @@ public void test_getLastModifiedForEncounter(){
encounterType.setOperationalEncounterTypes(operationalEncounterTypeSet);
operationalEncounterType.setEncounterType(encounterType);

assertEquals(operationalEncounterType.getLastModifiedDateTime(),lastModifiedDateTime1);
assertEquals(operationalEncounterType.getLastModifiedBy(),user1);
assertEquals(operationalEncounterType.getLastModifiedDateTime(), lastModifiedDateTime1);
assertEquals(operationalEncounterType.getLastModifiedBy(), user1);

encounterType.setLastModifiedDateTime(lastModifiedDateTime2);
operationalEncounterType.setLastModifiedDateTime(lastModifiedDateTime1);

assertEquals(operationalEncounterType.getLastModifiedDateTime(),lastModifiedDateTime1);
assertEquals(operationalEncounterType.getLastModifiedBy(),user2);
assertEquals(operationalEncounterType.getLastModifiedDateTime(), lastModifiedDateTime1);
assertEquals(operationalEncounterType.getLastModifiedBy(), user2);

operationalEncounterType.setLastModifiedDateTime(lastModifiedDateTime2);
assertEquals(operationalEncounterType.getLastModifiedDateTime(),lastModifiedDateTime2);
assertEquals(operationalEncounterType.getLastModifiedBy(),user2);
assertEquals(operationalEncounterType.getLastModifiedDateTime(), lastModifiedDateTime2);
assertEquals(operationalEncounterType.getLastModifiedBy(), user2);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.avni.server.dao.LocationRepository;
import org.avni.server.domain.*;
import org.avni.server.framework.security.UserContextHolder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -27,7 +28,6 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest({UserContextHolder.class})
public class AddressLevelServiceTest {
@Mock
Expand All @@ -39,12 +39,18 @@ public void setUp() {
Query query = Mockito.mock(Query.class);
when(entityManager.createNativeQuery(Mockito.anyString())).thenReturn(query);
when(entityManager.createNativeQuery(Mockito.anyString()).executeUpdate()).thenReturn(1);
PowerMockito.mockStatic(UserContextHolder.class);
Organisation org = mock(Organisation.class);
when(UserContextHolder.getOrganisation()).thenReturn(org);
UserContext userContext = new UserContext();
userContext.setOrganisation(org);
UserContextHolder.create(userContext);
when(org.getDbUser()).thenReturn("db-user");
}

@After
public void tearDown() {
UserContextHolder.clear();
}

@Test
public void shouldFetchDifferentAddressIdsWhenSubjectTypeChanges() throws JsonProcessingException {
LocationRepository locationRepository = mock(LocationRepository.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ public void shouldDeduplicateListOfGroupsSpecified() {
Group group1 = new Group();
Group group2 = new Group();
Group everyone = new Group();
everyone.setId(1L);
when(groupRepository.findByNameIgnoreCase("group1")).thenReturn(group1);
when(groupRepository.findByNameIgnoreCase("group2")).thenReturn(group2);
when(groupRepository.findByNameAndOrganisationId(Group.Everyone, orgId)).thenReturn(group1);
when(groupRepository.findByNameAndOrganisationId(Group.Everyone, orgId)).thenReturn(everyone);

userService.addToGroups(user, "group1,group2,group1");
verify(userGroupRepository, times(3)).save(userGroupArgumentCaptor.capture());
Expand Down
2 changes: 1 addition & 1 deletion avni-server-api/src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ spring.flyway.baseline-on-migrate=false
server.tomcat.accesslog.max-days=90

# JPA, Hibernate and Spring Data
spring.jpa.show-sql=true
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=none
#spring.jpa.properties.jadira.usertype.databaseZone=jvm
#spring.jpa.properties.jadira.usertype.javaZone=jvm
Expand Down

0 comments on commit 0a4f644

Please sign in to comment.