Skip to content

Commit

Permalink
#23 Reduce code duplication in equalsQueryString() implememtations
Browse files Browse the repository at this point in the history
Signed-off-by: Sven Strittmatter <[email protected]>
  • Loading branch information
Weltraumschaf committed Jan 26, 2024
1 parent 324a497 commit cb75f1f
Show file tree
Hide file tree
Showing 16 changed files with 291 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@AllArgsConstructor
@EqualsAndHashCode
@JsonInclude(JsonInclude.Include.NON_NULL)
public final class Endpoint implements Model {
public final class Endpoint implements Model, HasId {
@JsonProperty
private long id;

Expand Down Expand Up @@ -49,10 +49,11 @@ public final class Endpoint implements Model {

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
@AllArgsConstructor
@EqualsAndHashCode
@JsonInclude(JsonInclude.Include.NON_NULL)
public final class Engagement implements Model {
public final class Engagement implements Model, HasId, HasName {
@JsonProperty("branch_tag")
private String branch;

@JsonProperty
private Long id;
private long id;

@JsonProperty
private String name;
Expand Down Expand Up @@ -94,15 +94,15 @@ public final class Engagement implements Model {

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

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

if (queryParams.containsKey("name") && queryParams.get("name") != null && queryParams.get("name").equals(this.name)) {
if (QueryParamsComparator.isNameEqual(this, queryParams)) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
@AllArgsConstructor
@EqualsAndHashCode
@JsonInclude(JsonInclude.Include.NON_NULL)
public final class Finding implements Model {
public final class Finding implements Model, HasId {
@JsonProperty
private long id;

Expand Down Expand Up @@ -121,11 +121,11 @@ public String getNumericalSeverity() {

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

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

public enum Severity {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@AllArgsConstructor
@EqualsAndHashCode
@JsonInclude(JsonInclude.Include.NON_NULL)
public final class Group implements Model {
public final class Group implements Model, HasId, HasName {
@JsonProperty
private long id;

Expand All @@ -36,15 +36,15 @@ public final class Group implements Model {

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

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-FileCopyrightText: the secureCodeBox authors
//
// SPDX-License-Identifier: Apache-2.0

package io.securecodebox.persistence.defectdojo.model;

/**
* Interface to mark {@link Model models} which have an id
* <p>
* This type is package private because it is an implementation detail of the models and
* z should not be used outside of this package.
* </p>
*/
interface HasId {
long getId();

void setId(long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-FileCopyrightText: the secureCodeBox authors
//
// SPDX-License-Identifier: Apache-2.0

package io.securecodebox.persistence.defectdojo.model;

/**
* Interface to mark {@link Model models} which have a name
* <p>
* This type is package private because it is an implementation detail of the models and
* z should not be used outside of this package.
* </p>
*/
interface HasName {
String getName();

void setName(String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@AllArgsConstructor
@EqualsAndHashCode
@JsonInclude(JsonInclude.Include.NON_NULL)
public final class Product implements Model {
public final class Product implements Model, HasId, HasName {
@JsonProperty
private long id;

Expand Down Expand Up @@ -53,15 +53,15 @@ public final class Product implements Model {

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@AllArgsConstructor
@EqualsAndHashCode
@JsonInclude(JsonInclude.Include.NON_NULL)
public final class ProductType implements Model {
public final class ProductType implements Model, HasId, HasName {
@JsonProperty
private long id;

Expand All @@ -32,15 +32,15 @@ public final class ProductType implements Model {

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

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// SPDX-FileCopyrightText: the secureCodeBox authors
//
// SPDX-License-Identifier: Apache-2.0

package io.securecodebox.persistence.defectdojo.model;

import java.util.Map;

/**
* Pure static helper class
* <p>
* This type is package private because it is an implementation detail of the models and
* should not be used outside of this package.
* </p>
*/
final class QueryParamsComparator {

static final String QUERY_PARAM_KEY_FOR_ID = "id";
static final String QUERY_PARAM_KEY_FOR_NAME = "name";

private QueryParamsComparator() {
super();
}

static boolean isNull(Object o) {
return o == null;
}

static boolean isIdEqual(HasId model, Map<String, Object> queryParams) {
if (isNull(model)) {
return false;
}

if (isNull(queryParams)) {
return false;
}

if (!queryParams.containsKey(QUERY_PARAM_KEY_FOR_ID)) {
return false;
}

// FIXME: Since th generic type for value is Object, possible NPE here!
return queryParams.get(QUERY_PARAM_KEY_FOR_ID).equals(model.getId());
}

static boolean isNameEqual(HasName model, Map<String, Object> queryParams) {
if (isNull(model)) {
return false;
}

if (isNull(queryParams)) {
return false;
}

if (!queryParams.containsKey(QUERY_PARAM_KEY_FOR_NAME)) {
return false;
}

if (isNull(queryParams.get(QUERY_PARAM_KEY_FOR_NAME))) {
return false;
}

return queryParams.get(QUERY_PARAM_KEY_FOR_NAME).equals(model.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@AllArgsConstructor
@EqualsAndHashCode
@JsonInclude(JsonInclude.Include.NON_NULL)
public final class RiskAcceptance implements Model {
public final class RiskAcceptance implements Model, HasId {
@JsonProperty
private long id;

Expand Down Expand Up @@ -58,10 +58,10 @@ public final class RiskAcceptance implements Model {

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

return queryParams.containsKey("id") && queryParams.get("id").equals(this.id);
return QueryParamsComparator.isIdEqual(this, queryParams);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@AllArgsConstructor
@EqualsAndHashCode
@JsonInclude(JsonInclude.Include.NON_NULL)
public final class Test implements Model {
public final class Test implements Model, HasId {
@JsonProperty
private long id;

Expand Down Expand Up @@ -63,11 +63,11 @@ public final class Test implements Model {

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@AllArgsConstructor
@EqualsAndHashCode
@JsonInclude(JsonInclude.Include.NON_NULL)
public final class TestType implements Model {
public final class TestType implements Model, HasId, HasName {
@JsonProperty
private long id;

Expand All @@ -33,15 +33,15 @@ public final class TestType implements Model {

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@AllArgsConstructor
@EqualsAndHashCode
@JsonInclude(JsonInclude.Include.NON_NULL)
public final class ToolConfig implements Model {
public final class ToolConfig implements Model, HasId, HasName {
@JsonProperty
private long id;

Expand All @@ -38,15 +38,15 @@ public final class ToolConfig implements Model {

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@AllArgsConstructor
@EqualsAndHashCode
@JsonInclude(JsonInclude.Include.NON_NULL)
public final class ToolType implements Model {
public final class ToolType implements Model, HasId, HasName {
@JsonProperty
private long id;

Expand All @@ -29,15 +29,15 @@ public final class ToolType implements Model {

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

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

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

Expand Down
Loading

0 comments on commit cb75f1f

Please sign in to comment.