Skip to content

Commit

Permalink
#23 Add missing tests for model's equalsQueryString()
Browse files Browse the repository at this point in the history
Also add missing null check in implementation.

Signed-off-by: Sven Strittmatter <[email protected]>
  • Loading branch information
Weltraumschaf committed Jan 26, 2024
1 parent 9aaf930 commit cc47ad1
Show file tree
Hide file tree
Showing 28 changed files with 591 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public final class Endpoint implements Model {

@Override
public boolean equalsQueryString(Map<String, Object> queryParams) {
if (queryParams == null) {
return false;
}

return queryParams.containsKey("id") && queryParams.get("id").equals(this.id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ public boolean equalsQueryString(Map<String, Object> queryParams) {
if (queryParams == null) {
return false;
}

if (queryParams.containsKey("id") && queryParams.get("id") != null && queryParams.get("id").equals(this.id)) {
return true;
}

if (queryParams.containsKey("name") && queryParams.get("name") != null && queryParams.get("name").equals(this.name)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ public String getNumericalSeverity() {

@Override
public boolean equalsQueryString(Map<String, Object> queryParams) {
if (queryParams == null) {
return false;
}

return queryParams.containsKey("id") && queryParams.get("id").equals(this.id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@ public final class Group implements Model {

@Override
public boolean equalsQueryString(Map<String, Object> queryParams) {
if (queryParams == null) {
return false;
}

if (queryParams.containsKey("id") && queryParams.get("id").equals(this.id)) {
return true;
}

if (queryParams.containsKey("name") && queryParams.get("name").equals(this.name)) {
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,14 @@ public final class Product implements Model {

@Override
public boolean equalsQueryString(Map<String, Object> queryParams) {
if (queryParams == null) {
return false;
}

if (queryParams.containsKey("id") && queryParams.get("id").equals(this.id)) {
return true;
}

if (queryParams.containsKey("name") && queryParams.get("name").equals(this.name)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@ public final class ProductType implements Model {

@Override
public boolean equalsQueryString(Map<String, Object> queryParams) {
if (queryParams == null) {
return false;
}

if (queryParams.containsKey("id") && queryParams.get("id").equals(this.id)) {
return true;
}

if (queryParams.containsKey("name") && queryParams.get("name").equals(this.name)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.List;

// TODO: Why we do not have as many annotations as the other models here?
// TODO: Why does this class does not implement Model?
@Data
public final class Response<T> {
@JsonProperty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public final class RiskAcceptance implements Model {

@Override
public boolean equalsQueryString(Map<String, Object> queryParams) {
if (queryParams == null) {
return false;
}

return queryParams.containsKey("id") && queryParams.get("id").equals(this.id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.Data;

// TODO: Why we do not have as many annotations as the other models here?
// TODO: Why does this class does not implement Model?
@Data
public final class ScanFile {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,18 @@ public final class Test implements Model {

@Override
public boolean equalsQueryString(Map<String, Object> queryParams) {
if (queryParams == null) {
return false;
}

if (queryParams.containsKey("id") && queryParams.get("id").equals(this.id)) {
return true;
}

if (queryParams.containsKey("title") && queryParams.get("title").equals(this.title)) {
return true;
}

if (queryParams.containsKey("engagement") && queryParams.get("engagement").equals(this.engagement)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ public final class TestType implements Model {

@Override
public boolean equalsQueryString(Map<String, Object> queryParams) {
if (queryParams == null) {
return false;
}

if (queryParams.containsKey("id") && queryParams.get("id").equals(this.id)) {
return true;
}

if (queryParams.containsKey("name") && queryParams.get("name").equals(this.name)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,18 @@ public final class ToolConfig implements Model {

@Override
public boolean equalsQueryString(Map<String, Object> queryParams) {
if (queryParams == null) {
return false;
}

if (queryParams.containsKey("id") && queryParams.get("id").equals(this.id)) {
return true;
}

if (queryParams.containsKey("name") && queryParams.get("name").equals(this.name)) {
return true;
}

if (queryParams.containsKey("configuration_url") && queryParams.get("configuration_url").equals(this.configUrl)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ public final class ToolType implements Model {

@Override
public boolean equalsQueryString(Map<String, Object> queryParams) {
if (queryParams == null) {
return false;
}

if (queryParams.containsKey("id") && queryParams.get("id").equals(this.id)) {
return true;
}

if (queryParams.containsKey("name") && queryParams.get("name").equals(this.name)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@ public final class User implements Model {

@Override
public boolean equalsQueryString(Map<String, Object> queryParams) {
if (queryParams == null) {
return false;
}

if (queryParams.containsKey("id") && queryParams.get("id").equals(this.id)) {
return true;
}

if (queryParams.containsKey("username") && queryParams.get("username").equals(this.username)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ public final class UserProfile implements Model {

@Override
public boolean equalsQueryString(Map<String, Object> queryParams) {
// The user_profile endpoint does not have query parameters that's why this function will just return true
// The user_profile endpoint does not have query parameters that's
// why this function will just return true.
// TODO: All other implementations return false if null is given for queryParams.
// We should consider to change this API according to that.
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import nl.jqno.equalsverifier.Warning;
import org.junit.jupiter.api.Test;

import java.util.HashMap;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

/**
* Tests for {@link Endpoint}
*/
Expand All @@ -14,4 +19,22 @@ void equalsAndHashCode() {
.suppress(Warning.NONFINAL_FIELDS)
.verify();
}

@Test
void equalsQueryString_id() {
final var sut = Endpoint.builder().build();
assertThat(sut.equalsQueryString(null), is(false));

final var params = new HashMap<String, Object>();
assertThat(sut.equalsQueryString(params), is(false));

params.put("id", 42L);
assertThat(sut.equalsQueryString(params), is(false));

sut.setId(23L);
assertThat(sut.equalsQueryString(params), is(false));

sut.setId(42L);
assertThat(sut.equalsQueryString(params), is(true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import nl.jqno.equalsverifier.Warning;
import org.junit.jupiter.api.Test;

import java.util.HashMap;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

/**
* Tests for {@link Engagement}
*/
Expand All @@ -14,4 +19,40 @@ void equalsAndHashCode() {
.suppress(Warning.NONFINAL_FIELDS)
.verify();
}

@Test
void equalsQueryString_id() {
final var sut = Engagement.builder().build();
assertThat(sut.equalsQueryString(null), is(false));

final var params = new HashMap<String, Object>();
assertThat(sut.equalsQueryString(params), is(false));

params.put("id", 42L);
assertThat(sut.equalsQueryString(params), is(false));

sut.setId(23L);
assertThat(sut.equalsQueryString(params), is(false));

sut.setId(42L);
assertThat(sut.equalsQueryString(params), is(true));
}

@Test
void equalsQueryString_name() {
final var sut = Engagement.builder().build();
assertThat(sut.equalsQueryString(null), is(false));

final var params = new HashMap<String, Object>();
assertThat(sut.equalsQueryString(params), is(false));

params.put("name", "foobar");
assertThat(sut.equalsQueryString(params), is(false));

sut.setName("snafu");
assertThat(sut.equalsQueryString(params), is(false));

sut.setName("foobar");
assertThat(sut.equalsQueryString(params), is(true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
import nl.jqno.equalsverifier.Warning;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.HashMap;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

/**
* Tests for {@link Finding}
*/
Expand All @@ -14,4 +20,27 @@ void equalsAndHashCode() {
.suppress(Warning.NONFINAL_FIELDS)
.verify();
}

@Test
void equalsQueryString_id() {
final var sut = Finding.builder()
.title("title")
.description("description")
.foundBy(Collections.emptyList())
.severity(Finding.Severity.INFORMATIONAL)
.build();
assertThat(sut.equalsQueryString(null), is(false));

final var params = new HashMap<String, Object>();
assertThat(sut.equalsQueryString(params), is(false));

params.put("id", 42L);
assertThat(sut.equalsQueryString(params), is(false));

sut.setId(23L);
assertThat(sut.equalsQueryString(params), is(false));

sut.setId(42L);
assertThat(sut.equalsQueryString(params), is(true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import nl.jqno.equalsverifier.Warning;
import org.junit.jupiter.api.Test;

import java.util.HashMap;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

/**
* Tests for {@link Group}
*/
Expand All @@ -14,4 +19,44 @@ void equalsAndHashCode() {
.suppress(Warning.NONFINAL_FIELDS)
.verify();
}

@Test
void equalsQueryString_id() {
final var sut = Group.builder()
.name("")
.build();
assertThat(sut.equalsQueryString(null), is(false));

final var params = new HashMap<String, Object>();
assertThat(sut.equalsQueryString(params), is(false));

params.put("id", 42L);
assertThat(sut.equalsQueryString(params), is(false));

sut.setId(23L);
assertThat(sut.equalsQueryString(params), is(false));

sut.setId(42L);
assertThat(sut.equalsQueryString(params), is(true));
}

@Test
void equalsQueryString_name() {
final var sut = Group.builder()
.name("")
.build();
assertThat(sut.equalsQueryString(null), is(false));

final var params = new HashMap<String, Object>();
assertThat(sut.equalsQueryString(params), is(false));

params.put("name", "foobar");
assertThat(sut.equalsQueryString(params), is(false));

sut.setName("snafu");
assertThat(sut.equalsQueryString(params), is(false));

sut.setName("foobar");
assertThat(sut.equalsQueryString(params), is(true));
}
}
Loading

0 comments on commit cc47ad1

Please sign in to comment.