Skip to content
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

Fix equals and hashCode of ActivationHistoryEntity #1119

Merged
merged 6 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<!-- Other Dependencies -->
<commons-text.version>1.11.0</commons-text.version>
<logstash.version>7.4</logstash.version>
<equalsverifier.version>3.15.3</equalsverifier.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -168,6 +169,13 @@
<artifactId>tomcat-embed-el</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<version>${equalsverifier.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
5 changes: 5 additions & 0 deletions powerauth-java-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,22 +202,27 @@ public void setActivationName(final String activationName) {
}

@Override
public int hashCode() {
return Objects.hash(id);
}

@Override
public boolean equals(Object obj) {
if (null == obj) {
public boolean equals(final Object o) {
if (null == o) {
return false;
} else if (this == obj) {
} else if (this == o) {
return true;
} else if (!this.getClass().equals(ProxyUtils.getUserClass(obj))) {
} else if (!this.getClass().equals(ProxyUtils.getUserClass(o))) {
return false;
} else {
final ActivationHistoryEntity that = (ActivationHistoryEntity) obj;
return null != this.getId() && this.getId().equals(that.getId());
final ActivationHistoryEntity that = (ActivationHistoryEntity) o;
return Objects.equals(getActivationId(), that.getActivationId()) && Objects.equals(getTimestampCreated(), that.getTimestampCreated());
}
}

@Override
public int hashCode() {
return Objects.hash(getActivationId(), timestampCreated);
}

// TODO (racansky, 2023-11-08) remove when activation equals and hashCode implemented correctly
private String getActivationId() {
return getActivation() == null ? null : getActivation().getActivationId();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* PowerAuth Server and related software components
* Copyright (C) 2023 Wultra s.r.o.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.getlime.security.powerauth.app.server.database.model.entity;

import lombok.extern.slf4j.Slf4j;
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
import org.junit.jupiter.api.Test;

import java.util.Date;
import java.util.List;

/**
* Test for {@link ActivationHistoryEntity}.
*
* @author Lubos Racansky, [email protected]
*/
@Slf4j
class ActivationHistoryEntityTest {

@Test
void testEqualsContract() {
final ApplicationEntity application1 = new ApplicationEntity();
application1.setId("app1");
final ApplicationEntity application2 = new ApplicationEntity();
application2.setId("app2");

final ApplicationVersionEntity applicationVersion1 = new ApplicationVersionEntity();
applicationVersion1.setId("v1");
final ApplicationVersionEntity applicationVersion2 = new ApplicationVersionEntity();
applicationVersion2.setId("v2");

final ActivationHistoryEntity activationHistory1 = new ActivationHistoryEntity();
activationHistory1.setTimestampCreated(new Date(1));
final ActivationHistoryEntity activationHistory2 = new ActivationHistoryEntity();
activationHistory2.setTimestampCreated(new Date(2));

EqualsVerifier.forClass(ActivationHistoryEntity.class)
.withOnlyTheseFields("activation", "timestampCreated")
// TODO (racansky, 2023-11-09) equals and hashCode is using getActivation().getActivationId() but still getting false positive; https://jqno.nl/equalsverifier/manual/jpa-entities/
.suppress(Warning.JPA_GETTER)
.withPrefabValues(ApplicationEntity.class, application1, application2)
.withPrefabValues(ApplicationVersionEntity.class, applicationVersion1, applicationVersion2)
.withPrefabValues(List.class, List.of(activationHistory1), List.of(activationHistory2))
.verify();
}
}