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

[BEAM-9000] Java Test Assertions without toString for GenericJson subclasses #10441

Merged
merged 2 commits into from
Dec 23, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ class BeamModulePlugin implements Plugin<Project> {
jackson_module_scala : "com.fasterxml.jackson.module:jackson-module-scala_2.11:$jackson_version",
jaxb_api : "javax.xml.bind:jaxb-api:$jaxb_api_version",
joda_time : "joda-time:joda-time:2.10.3",
jsonassert : "org.skyscreamer:jsonassert:1.5.0",
jsr305 : "com.google.code.findbugs:jsr305:3.0.2",
junit : "junit:junit:4.13-beta-3",
kafka : "org.apache.kafka:kafka_2.11:$kafka_version",
Expand Down
1 change: 1 addition & 0 deletions runners/google-cloud-dataflow-java/worker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ dependencies {
shadowTest project(path: ":runners:direct-java", configuration: "shadow")
shadowTest library.java.hamcrest_core
shadowTest library.java.hamcrest_library
shadowTest library.java.jsonassert
shadowTest library.java.junit
shadowTest library.java.mockito_core
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def excluded_dependencies = [
library.java.hamcrest_core, // Test only
library.java.hamcrest_library, // Test only
library.java.junit, // Test only
library.java.jsonassert // Test only
]

applyJavaNature(
Expand Down Expand Up @@ -224,6 +225,7 @@ dependencies {
shadowTest project(path: ":sdks:java:core", configuration: "shadowTest")
shadowTest library.java.hamcrest_core
shadowTest library.java.hamcrest_library
shadowTest library.java.jsonassert
shadowTest library.java.junit
shadowTest library.java.mockito_core
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
*/
package org.apache.beam.runners.dataflow.worker.fn.control;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.apache.beam.runners.dataflow.worker.testing.GenericJsonAssert.assertEqualsAsJson;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -56,7 +57,7 @@ public void tesTransformReturnsNullIfSpecValidationFails() {
mockSpecValidator, pcollectionNameMapping);
Optional<String> error = Optional.of("Error text");
when(mockSpecValidator.validate(any())).thenReturn(error);
assertEquals(null, testObject.transform(null));
assertNull(testObject.transform(null));
}

@Test
Expand Down Expand Up @@ -85,7 +86,7 @@ public void testTransformReturnsNullIfMonitoringInfoWithUnknownPCollectionLabelP
new ElementCountMonitoringInfoToCounterUpdateTransformer(
mockSpecValidator, pcollectionNameMapping);
when(mockSpecValidator.validate(any())).thenReturn(Optional.empty());
assertEquals(null, testObject.transform(monitoringInfo));
assertNull(testObject.transform(monitoringInfo));
}

@Test
Expand All @@ -106,12 +107,12 @@ public void testTransformReturnsValidCounterUpdateWhenValidMonitoringInfoReceive
when(mockSpecValidator.validate(any())).thenReturn(Optional.empty());

CounterUpdate result = testObject.transform(monitoringInfo);
assertNotEquals(null, result);
assertNotNull(result);

assertEquals(
"{cumulative=true, integer={highBits=0, lowBits=0}, "
+ "nameAndKind={kind=SUM, "
+ "name=transformedValue-ElementCount}}",
result.toString());
assertEqualsAsJson(
"{cumulative:true, integer:{highBits:0, lowBits:0}, "
+ "nameAndKind:{kind:'SUM', "
+ "name:'transformedValue-ElementCount'}}",
result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.apache.beam.runners.dataflow.worker.fn.control;

import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -86,6 +87,6 @@ public void testTransformReturnsNullOnUnknownUrn() {

CounterUpdate result = testObject.transform(monitoringInfo);

assertSame(null, result);
assertNull(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
*/
package org.apache.beam.runners.dataflow.worker.fn.control;

import static org.apache.beam.runners.dataflow.worker.testing.GenericJsonAssert.assertEqualsAsJson;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -68,7 +70,7 @@ public void testTransformReturnsNullIfSpecValidationFails() {
MonitoringInfo.newBuilder()
.setUrn("beam:metric:pardo_execution_time:start_bundle_msecs:v1:invalid")
.build();
assertEquals(null, testObject.transform(monitoringInfo));
assertNull(testObject.transform(monitoringInfo));
}

@Test
Expand Down Expand Up @@ -142,14 +144,13 @@ public void testTransformReturnsValidCounterUpdateWhenValidMSecMonitoringInfoRec
CounterUpdate result = testObject.transform(monitoringInfo);

// Validate
assertNotEquals(null, result);

assertEquals(
"{cumulative=true, integer={highBits=0, lowBits=0}, "
+ "structuredNameAndMetadata={metadata={kind=SUM}, "
+ "name={executionStepName=anyStageName, name=supportedCounter, origin=SYSTEM, "
+ "originalStepName=anyOriginalName}}}",
result.toString());
assertNotNull(result);
assertEqualsAsJson(
"{cumulative:true, integer:{highBits:0, lowBits:0}, "
+ "structuredNameAndMetadata:{metadata:{kind:'SUM'}, "
+ "name:{executionStepName:'anyStageName', name:'supportedCounter', origin:'SYSTEM', "
+ "originalStepName:'anyOriginalName'}}}",
result);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
*/
package org.apache.beam.runners.dataflow.worker.fn.control;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.apache.beam.runners.dataflow.worker.testing.GenericJsonAssert.assertEqualsAsJson;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -56,7 +57,7 @@ public void tesTransformReturnsNullIfSpecValidationFails() {
mockSpecValidator, pcollectionNameMapping);
Optional<String> error = Optional.of("Error text");
when(mockSpecValidator.validate(any())).thenReturn(error);
assertEquals(null, testObject.transform(null));
assertNull(testObject.transform(null));
}

@Test
Expand Down Expand Up @@ -85,7 +86,7 @@ public void testTransformReturnsNullIfMonitoringInfoWithUnknownPCollectionLabelP
new MeanByteCountMonitoringInfoToCounterUpdateTransformer(
mockSpecValidator, pcollectionNameMapping);
when(mockSpecValidator.validate(any())).thenReturn(Optional.empty());
assertEquals(null, testObject.transform(monitoringInfo));
assertNull(testObject.transform(monitoringInfo));
}

@Test
Expand All @@ -107,12 +108,12 @@ public void testTransformReturnsValidCounterUpdateWhenValidMonitoringInfoReceive

CounterUpdate result = testObject.transform(monitoringInfo);

assertNotEquals(null, result);
assertEquals(
"{cumulative=true, integerMean={count={highBits=0, lowBits=0}, "
+ "sum={highBits=0, lowBits=0}}, "
+ "nameAndKind={kind=MEAN, "
+ "name=transformedValue-MeanByteCount}}",
result.toString());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion was based on AbstractMap's toString(). This style is prone to unnecessary maintenance of the test code when upgrading dependencies. Dependencies may change the internal ordering of fields and trivial change in toString(). In fact, BEAM-8695 is suspended partly because of these tests.

assertNotNull(result);
assertEqualsAsJson(
"{cumulative:true, integerMean:{count:{highBits:0, lowBits:0}, "
+ "sum:{highBits:0, lowBits:0}}, "
+ "nameAndKind:{kind:'MEAN', "
+ "name:'transformedValue-MeanByteCount'}}",
result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
*/
package org.apache.beam.runners.dataflow.worker.fn.control;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.apache.beam.runners.dataflow.worker.testing.GenericJsonAssert.assertEqualsAsJson;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -58,7 +59,7 @@ public void testTransformReturnsNullIfSpecValidationFails() {
mockSpecValidator, stepContextMapping);
Optional<String> error = Optional.of("Error text");
when(mockSpecValidator.validate(any())).thenReturn(error);
assertEquals(null, testObject.transform(null));
assertNull(testObject.transform(null));
}

@Test
Expand Down Expand Up @@ -89,7 +90,7 @@ public void testTransformReturnsNullIfMonitoringInfoWithUnknownPTransformLabelPr
new UserDistributionMonitoringInfoToCounterUpdateTransformer(
mockSpecValidator, stepContextMapping);
when(mockSpecValidator.validate(any())).thenReturn(Optional.empty());
assertEquals(null, testObject.transform(monitoringInfo));
assertNull(testObject.transform(monitoringInfo));
}

@Test
Expand All @@ -114,15 +115,15 @@ public void testTransformReturnsValidCounterUpdateWhenValidUserMonitoringInfoRec
when(mockSpecValidator.validate(any())).thenReturn(Optional.empty());

CounterUpdate result = testObject.transform(monitoringInfo);
assertNotEquals(null, result);
assertNotNull(result);

assertEquals(
"{cumulative=true, distribution={count={highBits=0, lowBits=0}, "
+ "max={highBits=0, lowBits=0}, min={highBits=0, lowBits=0}, "
+ "sum={highBits=0, lowBits=0}}, "
+ "structuredNameAndMetadata={metadata={kind=DISTRIBUTION}, "
+ "name={name=anyName, origin=USER, originNamespace=anyNamespace, "
+ "originalStepName=anyOriginalName}}}",
result.toString());
assertEqualsAsJson(
"{cumulative:true, distribution:{count:{highBits:0, lowBits:0}, "
+ "max:{highBits:0, lowBits:0}, min:{highBits:0, lowBits:0}, "
+ "sum:{highBits:0, lowBits:0}}, "
+ "structuredNameAndMetadata:{metadata:{kind:'DISTRIBUTION'}, "
+ "name:{name:'anyName', origin:'USER', originNamespace:'anyNamespace', "
+ "originalStepName:'anyOriginalName'}}}",
result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
*/
package org.apache.beam.runners.dataflow.worker.fn.control;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.apache.beam.runners.dataflow.worker.testing.GenericJsonAssert.assertEqualsAsJson;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -57,7 +58,7 @@ public void testTransformReturnsNullIfSpecValidationFails() {
new UserMonitoringInfoToCounterUpdateTransformer(mockSpecValidator, stepContextMapping);
Optional<String> error = Optional.of("Error text");
when(mockSpecValidator.validate(any())).thenReturn(error);
assertEquals(null, testObject.transform(null));
assertNull(testObject.transform(null));
}

@Test
Expand Down Expand Up @@ -86,7 +87,7 @@ public void testTransformReturnsNullIfMonitoringInfoWithUnknownPTransformLabelPr
UserMonitoringInfoToCounterUpdateTransformer testObject =
new UserMonitoringInfoToCounterUpdateTransformer(mockSpecValidator, stepContextMapping);
when(mockSpecValidator.validate(any())).thenReturn(Optional.empty());
assertEquals(null, testObject.transform(monitoringInfo));
assertNull(testObject.transform(monitoringInfo));
}

@Test
Expand All @@ -110,13 +111,13 @@ public void testTransformReturnsValidCounterUpdateWhenValidUserMonitoringInfoRec
when(mockSpecValidator.validate(any())).thenReturn(Optional.empty());

CounterUpdate result = testObject.transform(monitoringInfo);
assertNotEquals(null, result);
assertNotNull(result);

assertEquals(
"{cumulative=true, integer={highBits=0, lowBits=0}, "
+ "structuredNameAndMetadata={metadata={kind=SUM}, "
+ "name={name=anyName, origin=USER, originNamespace=anyNamespace, "
+ "originalStepName=anyOriginalName}}}",
result.toString());
assertEqualsAsJson(
"{cumulative:true, integer:{highBits:0, lowBits:0}, "
+ "structuredNameAndMetadata:{metadata:{kind:'SUM'}, "
+ "name:{name:'anyName', origin:'USER', originNamespace:'anyNamespace', "
+ "originalStepName:'anyOriginalName'}}}",
result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.beam.runners.dataflow.worker.testing;

import com.google.api.client.json.GenericJson;
import com.google.api.client.json.jackson2.JacksonFactory;
import org.json.JSONException;
import org.skyscreamer.jsonassert.JSONAssert;

/** Assertions on {@link GenericJson} class. */
public class GenericJsonAssert {

private static final JacksonFactory jacksonFactory = JacksonFactory.getDefaultInstance();

/**
* Asserts that {@code actual} has the same JSON representation as {@code expectedJsonText}.
*
* @param expectedJsonText expected JSON string.
* @param actual actual object to compare its JSON representation.
*/
public static void assertEqualsAsJson(String expectedJsonText, GenericJson actual) {
actual.setFactory(jacksonFactory);

String actualJsonText = actual.toString();
try {
JSONAssert.assertEquals(expectedJsonText, actualJsonText, true);
} catch (JSONException ex) {
throw new IllegalArgumentException("Could not parse JSON", ex);
}
}
}