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

override equals method for TypedGraphQLError #702

Merged
merged 2 commits into from Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}
}