Skip to content

Commit

Permalink
Merge pull request #702 from clandry94/typed-graphql-error-equals-fix
Browse files Browse the repository at this point in the history
override equals method for TypedGraphQLError
  • Loading branch information
berngp committed Nov 2, 2021
2 parents 15d4b56 + fe33db7 commit b4bfa3e
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
Expand Up @@ -244,6 +244,22 @@ public String toString() {
'}';
}

@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null) return false;
if (obj.getClass() != this.getClass()) return false;

TypedGraphQLError e = (TypedGraphQLError)obj;

if (!message.equals(e.message)) return false;
if (!locations.equals(e.locations)) return false;
if (!path.equals(e.path)) return false;
if (!extensions.equals(e.extensions)) return false;

return true;
}

public static class Builder {
private String message;
private List<Object> path;
Expand Down
@@ -0,0 +1,81 @@
/*
* Copyright 2021 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 specific language governing permissions and
* limitations under the License.
*/

package com.netflix.graphql.types.errors;

import graphql.execution.ResultPath;
import graphql.language.SourceLocation;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.HashMap;

public class TypedGraphQLErrorTest {
@Test
void equalsTest() {
TypedGraphQLError baseError = TypedGraphQLError.newPermissionDeniedBuilder()
.message("org.springframework.security.access.AccessDeniedException: Access is denied")
.path(ResultPath.parse("/graphqlEndpoint"))
.build();

TypedGraphQLError sameError = TypedGraphQLError.newPermissionDeniedBuilder()
.message("org.springframework.security.access.AccessDeniedException: Access is denied")
.path(ResultPath.parse("/graphqlEndpoint"))
.build();

Assertions.assertEquals(baseError, sameError);
}

@Test
void notEqualsTest() {
TypedGraphQLError baseError = TypedGraphQLError.newPermissionDeniedBuilder()
.message("org.springframework.security.access.AccessDeniedException: Access is denied")
.path(ResultPath.parse("/graphqlEndpoint"))
.build();

TypedGraphQLError differentMessageError = TypedGraphQLError.newPermissionDeniedBuilder()
.message("Different Error Message")
.path(ResultPath.parse("/graphqlEndpoint"))
.build();

Assertions.assertNotEquals(baseError, differentMessageError);

TypedGraphQLError differentLocationsError = TypedGraphQLError.newPermissionDeniedBuilder()
.message("Different Error Message")
.location(new SourceLocation(0, 0))
.path(ResultPath.parse("/graphqlEndpoint"))
.build();

Assertions.assertNotEquals(baseError, differentLocationsError);

TypedGraphQLError differentPathError = TypedGraphQLError.newPermissionDeniedBuilder()
.message("org.springframework.security.access.AccessDeniedException: Access is denied")
.path(ResultPath.parse("/differentGraphQlEndpoint"))
.build();

Assertions.assertNotEquals(baseError, differentPathError);

TypedGraphQLError differentExtensionsError = TypedGraphQLError.newPermissionDeniedBuilder()
.message("org.springframework.security.access.AccessDeniedException: Access is denied")
.path(ResultPath.parse("/differentGraphQlEndpoint"))
.extensions(new HashMap<String, Object>() {{
put("key", "value");
}})
.build();

Assertions.assertNotEquals(baseError, differentExtensionsError);
}
}

0 comments on commit b4bfa3e

Please sign in to comment.