Skip to content

Commit

Permalink
Replace hamcrest with assertj (#5911)
Browse files Browse the repository at this point in the history
* optimize imports

* optimize imports

* Hamcrest replacement

* Hamcrest replacement

* Test failures

* migrate hapi-fhir-base

* docs

* Few more cutovers

* Full rewrite of all assert cases

* Full rewrite of all assert cases

* Partial cutover

* assert library conversion

* wip

* fix imports

* remove equalto

* once-over again

* minor cutover

* wip

* wip

* wip

* wip

* wip

* Remove hamcrest asserts replace with assertj

* wip

* Partial hamcrest rip-out

* More hamcrest stripping

* wip

* Partial completion

* spotless

* it compiles now

* fixing compile issues

* fixing compile issues

* fixing compile issues

* fixing compile issues

* fixing compile issues

* fixing compile issues

* fixing compile issues

* fixing compile issues

* fixing compile issues

* fixing compile issues

* fixing compile issues

* fixing compile issues

* fixing compile issues

* fix compile issues

* fix compile issues

* hapi-fhir now compiles

* fix test

* fix test

* fix test

* fix tests

* fix tests

* fix tests

* fix test

* fix test

* fix tests

* fix tests

* fix tests

* yay all tests pass now!

* revert assertEquals change

* revert assertEquals change

* organize imports

* organize imports

* organize imports

* post merge cleanup

* organize imports

* more cleanup

* more cleanup

* removing hamcrest stragglers

* removing hamcrest stragglers

* removing hamcrest stragglers

* removing hamcrest stragglers

* removing hamcrest stragglers

* removing hamcrest stragglers

* removing hamcrest stragglers

* removing hamcrest stragglers

* removing hamcrest stragglers

* removing hamcrest stragglers

* removing hamcrest stragglers

* organize imports

* organize imports

* remove final hamcrest stuff

* remove final hamcrest usages

* remove final hamcrest usages

* Revert "remove final hamcrest usages"

This reverts commit eaf5107.

* fix compile issues from a hacky search/replace

* compiles with fixmes

* fix tests

* fix test

* fix test

* fix post merge compile issues

* fix post merge compile issues

* fix post merge compile issues

* fix post merge compile issues

* fix post merge compile issues

* fix post merge test issues

* fix test

* fix test

* replace hamcrest with assertj

* replace hamcrest with assertj

* replace hamcrest with assertj

* replace hamcrest with assertj

* replace hamcrest with assertj

* replace hamcrest with assertj

* replace hamcrest with assertj

* replace hamcrest with assertj

* replace hamcrest with assertj

* replace hamcrest with assertj

* replace hamcrest with assertj

* replace hamcrest with assertj

* replace hamcrest with assertj

* replace hamcrest with assertj

* all but mdm tests

* final hamcrest removed from hapi-fhir yay!

* revert mdm changes. it's too hard for now

* revert cdr breaking change

* revert cdr breaking changes

* revert cdr breaking changes

* revert cdr breaking changes

* revert cdr breaking changes

* revert changes that break cdr

* merge master with compile errors

* review feedback

* review feedback

* review feedback: revert bad regexp change

* fix test

* fix test

* convert new hamcrest entries

* convert new hamcrest entries

---------

Co-authored-by: Tadgh <[email protected]>
  • Loading branch information
fil512 and tadgh committed Jun 5, 2024
1 parent 575b689 commit cdf5b60
Show file tree
Hide file tree
Showing 2,145 changed files with 26,333 additions and 28,469 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void testParserXml() {
ctx.newXmlParser().encodeResourceToString(p);
fail();
} catch (ca.uhn.fhir.context.ConfigurationException e) {
assertEquals(Msg.code(1754) + "Unable to initialize StAX - XML processing is disabled",e.getMessage());
assertEquals(Msg.code(1754) + "Unable to initialize StAX - XML processing is disabled", e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@
import java.nio.charset.StandardCharsets;
import java.util.Date;

import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand Down Expand Up @@ -126,7 +124,7 @@ public void testBinaryCreateWithFhirContentType() throws Exception {
Request request = capt.getAllValues().get(0);
ourLog.info(request.headers().toString());

assertThat(request.url().toString(), startsWith("https://example.com/fhir/Binary"));
assertThat(request.url().toString()).startsWith("https://example.com/fhir/Binary");
validateUserAgent(capt);

assertEquals(Constants.CT_FHIR_JSON_NEW + ";charset=utf-8", request.body().contentType().toString().toLowerCase().replace(" ", ""));
Expand Down Expand Up @@ -201,7 +199,7 @@ public void testBinaryCreateWithNoContentType() throws Exception {

assertEquals(Constants.CT_FHIR_JSON_NEW + ";charset=utf-8", request.body().contentType().toString().toLowerCase().replace(" ", ""));
assertEquals(Constants.HEADER_ACCEPT_VALUE_JSON_NON_LEGACY, request.header("Accept"));
assertArrayEquals(new byte[] { 0, 1, 2, 3, 4 }, ourCtx.newJsonParser().parseResource(Binary.class, extractBodyAsString(capt)).getContent());
assertThat(ourCtx.newJsonParser().parseResource(Binary.class, extractBodyAsString(capt)).getContent()).containsExactly(new byte[]{0, 1, 2, 3, 4});

}

Expand Down Expand Up @@ -270,7 +268,7 @@ public void testCreateWithPreferRepresentationServerReturnsResource() {
assertNull(outcome.getOperationOutcome());
assertNotNull(outcome.getResource());

assertEquals(1, capt.getAllValues().size());
assertThat(capt.getAllValues()).hasSize(1);
assertEquals("<div xmlns=\"https://www.w3.org/1999/xhtml\">FINAL VALUE</div>", ((Patient) outcome.getResource()).getText().getDivAsString());
assertEquals("https://example.com/fhir/Patient?_format=json", capt.getAllValues().get(0).url().toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.junit.jupiter.params.provider.CsvSource;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class RuntimeSearchParamTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package ca.uhn.fhir.context.phonetic;

import ca.uhn.fhir.util.PhoneticEncoderUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.startsWith;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class PhoneticEncoderTest {
private static final Logger ourLog = LoggerFactory.getLogger(PhoneticEncoderTest.class);
Expand All @@ -24,14 +22,14 @@ public class PhoneticEncoderTest {
@EnumSource(PhoneticEncoderEnum.class)
public void testEncodeAddress(PhoneticEncoderEnum thePhoneticEncoderEnum) {
IPhoneticEncoder encoder = PhoneticEncoderUtil.getEncoder(thePhoneticEncoderEnum.name());
Assertions.assertNotNull(encoder);
assertNotNull(encoder);
String encoded = encoder.encode(ADDRESS_LINE);
ourLog.info("{}: {}", thePhoneticEncoderEnum.name(), encoded);
if (thePhoneticEncoderEnum == PhoneticEncoderEnum.NUMERIC) {
assertEquals(NUMBER + SUITE, encoded);
} else {
assertThat(encoded, startsWith(NUMBER + " "));
assertThat(encoded, endsWith(" " + SUITE));
assertThat(encoded).startsWith(NUMBER + " ");
assertThat(encoded).endsWith(" " + SUITE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

import java.util.Set;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.matchesPattern;
import static org.hamcrest.Matchers.not;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class HapiLocalizerTest {
Expand All @@ -18,15 +15,15 @@ public void testEscapePatterns() {

assertEquals("some message", loc.newMessageFormat("some message").format(new Object[]{}));
assertEquals("var1 {var2} var3 {var4}", loc.newMessageFormat("var1 {var2} var3 {var4}").format(new Object[]{}));
assertEquals("var1 A var3 B", loc.newMessageFormat("var1 {0} var3 {1}").format(new Object[]{ "A", "B"}));
assertEquals("var1 A var3 B", loc.newMessageFormat("var1 {0} var3 {1}").format(new Object[]{"A", "B"}));
}


@Test
public void testAllKeys() {
HapiLocalizer svc = new HapiLocalizer();
Set<String> allKeys = svc.getAllKeys();
assertThat(allKeys, not(empty()));
assertThat(allKeys).isNotEmpty();

for (String next : allKeys) {
svc.getMessage(next);
Expand All @@ -37,7 +34,7 @@ public void testAllKeys() {
public void testGetVersion() {
HapiLocalizer svc = new HapiLocalizer();
String version = svc.getMessage("hapi.version");
assertThat(version, matchesPattern("[0-9]+.*"));
assertThat(version).matches("[0-9]+.*");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

public class InterceptorServiceTest {

Expand Down Expand Up @@ -185,7 +188,7 @@ public void testRegisterHookFails() {
// good
}

assertEquals(initialSize, svc.getGlobalInterceptorsForUnitTest().size());
assertThat(svc.getGlobalInterceptorsForUnitTest()).hasSize(initialSize);

}

Expand All @@ -206,29 +209,29 @@ public void testManuallyRegisterInterceptor() {
MyTestInterceptorManual myInterceptorManual = new MyTestInterceptorManual();
svc.registerInterceptor(myInterceptorManual);
List<Object> globalInterceptors = svc.getGlobalInterceptorsForUnitTest();
assertEquals(3, globalInterceptors.size());
assertThat(globalInterceptors).hasSize(3);
assertInstanceOf(MyTestInterceptorOne.class, globalInterceptors.get(0), globalInterceptors.get(0).getClass().toString());
assertInstanceOf(MyTestInterceptorManual.class, globalInterceptors.get(1), globalInterceptors.get(1).getClass().toString());
assertInstanceOf(MyTestInterceptorTwo.class, globalInterceptors.get(2), globalInterceptors.get(2).getClass().toString());

// Try to register again (should have no effect
svc.registerInterceptor(myInterceptorManual);
globalInterceptors = svc.getGlobalInterceptorsForUnitTest();
assertEquals(3, globalInterceptors.size());
assertThat(globalInterceptors).hasSize(3);
assertInstanceOf(MyTestInterceptorOne.class, globalInterceptors.get(0), globalInterceptors.get(0).getClass().toString());
assertInstanceOf(MyTestInterceptorManual.class, globalInterceptors.get(1), globalInterceptors.get(1).getClass().toString());
assertInstanceOf(MyTestInterceptorTwo.class, globalInterceptors.get(2), globalInterceptors.get(2).getClass().toString());

// Make sure we have the right invokers in the right order
List<Object> invokers = svc.getInterceptorsWithInvokersForPointcut(Pointcut.TEST_RB);
assertSame(interceptor0, invokers.get(0));
assertSame(myInterceptorManual, invokers.get(1));
assertSame(interceptor1, invokers.get(2));
assertThat(invokers.get(0)).isSameAs(interceptor0);
assertThat(invokers.get(1)).isSameAs(myInterceptorManual);
assertThat(invokers.get(2)).isSameAs(interceptor1);

// Finally, unregister it
svc.unregisterInterceptor(myInterceptorManual);
globalInterceptors = svc.getGlobalInterceptorsForUnitTest();
assertEquals(2, globalInterceptors.size());
assertThat(globalInterceptors).hasSize(2);
assertInstanceOf(MyTestInterceptorOne.class, globalInterceptors.get(0), globalInterceptors.get(0).getClass().toString());
assertInstanceOf(MyTestInterceptorTwo.class, globalInterceptors.get(1), globalInterceptors.get(1).getClass().toString());

Expand All @@ -255,10 +258,10 @@ public void testInvokeGlobalInterceptorMethods() {
assertTrue(outcome);
}

assertThat(myInvocations, contains("MyTestInterceptorOne.testRb", "MyTestInterceptorTwo.testRb"));
assertSame("A", interceptor0.myLastString0);
assertSame("A", interceptor1.myLastString0);
assertSame("B", interceptor1.myLastString1);
assertThat(myInvocations).containsExactly("MyTestInterceptorOne.testRb", "MyTestInterceptorTwo.testRb");
assertThat(interceptor0.myLastString0).isSameAs("A");
assertThat(interceptor1.myLastString0).isSameAs("A");
assertThat(interceptor1.myLastString1).isSameAs("B");
}

@Test
Expand All @@ -275,10 +278,10 @@ public void testInvokeAnonymousInterceptorMethods() {
assertTrue(outcome);
}

assertThat(myInvocations, contains("MyTestAnonymousInterceptorOne.testRb", "MyTestAnonymousInterceptorTwo.testRb"));
assertSame("A", interceptor0.myLastString0);
assertSame("A", interceptor1.myLastString0);
assertSame("B", interceptor1.myLastString1);
assertThat(myInvocations).containsExactly("MyTestAnonymousInterceptorOne.testRb", "MyTestAnonymousInterceptorTwo.testRb");
assertThat(interceptor0.myLastString0).isSameAs("A");
assertThat(interceptor1.myLastString0).isSameAs("A");
assertThat(interceptor1.myLastString1).isSameAs("B");
}

@Test
Expand All @@ -293,10 +296,10 @@ public void testInvokeUsingSupplierArg() {
boolean outcome = svc.callHooks(Pointcut.TEST_RB, new HookParams("A", "B"));
assertTrue(outcome);

assertThat(myInvocations, contains("MyTestInterceptorOne.testRb", "MyTestInterceptorTwo.testRb"));
assertSame("A", interceptor0.myLastString0);
assertSame("A", interceptor1.myLastString0);
assertSame("B", interceptor1.myLastString1);
assertThat(myInvocations).containsExactly("MyTestInterceptorOne.testRb", "MyTestInterceptorTwo.testRb");
assertThat(interceptor0.myLastString0).isSameAs("A");
assertThat(interceptor1.myLastString0).isSameAs("A");
assertThat(interceptor1.myLastString1).isSameAs("B");
}

@Test
Expand All @@ -313,10 +316,10 @@ public void testInvokeGlobalInterceptorMethods_MethodAbortsProcessing() {
boolean outcome = svc.callHooks(Pointcut.TEST_RB, new HookParams("A", "B"));
assertFalse(outcome);

assertThat(myInvocations, contains("MyTestInterceptorOne.testRb"));
assertSame("A", interceptor0.myLastString0);
assertSame(null, interceptor1.myLastString0);
assertSame(null, interceptor1.myLastString1);
assertThat(myInvocations).containsExactly("MyTestInterceptorOne.testRb");
assertThat(interceptor0.myLastString0).isSameAs("A");
assertThat(interceptor1.myLastString0).isSameAs(null);
assertThat(interceptor1.myLastString1).isSameAs(null);
}

@Test
Expand Down Expand Up @@ -440,7 +443,7 @@ public void testCallHooksInvokedWithWrongParameters() {
svc.callHooks(Pointcut.TEST_RB, params);
fail();
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), containsString("Invalid params for pointcut " + Pointcut.TEST_RB + " - Wanted java.lang.String,java.lang.String but found "));
assertThat(e.getMessage()).contains("Invalid params for pointcut " + Pointcut.TEST_RB + " - Wanted java.lang.String,java.lang.String but found ");
}
}

Expand Down Expand Up @@ -521,7 +524,7 @@ void testBooleanWithNoHooks_returnsTrue() {
InterceptorService svc = new InterceptorService();

assertTrue(svc.callHooks(Pointcut.TEST_RB, params));
assertTrue(svc.ifHasCallHooks(Pointcut.TEST_RB, ()->params));
assertTrue(svc.ifHasCallHooks(Pointcut.TEST_RB, () -> params));
}

@Test
Expand All @@ -531,7 +534,7 @@ void testBooleanWithAllHooksReturnTrue_returnsTrue() {
svc.registerInterceptor(new BooleanHook(true));

assertTrue(svc.callHooks(Pointcut.TEST_RB, params));
assertTrue(svc.ifHasCallHooks(Pointcut.TEST_RB, ()->params));
assertTrue(svc.ifHasCallHooks(Pointcut.TEST_RB, () -> params));
}

@Test
Expand All @@ -542,7 +545,7 @@ void testBooleanWithAHookReturnFalse_returnsFalse() {
svc.registerInterceptor(new BooleanHook(true));

assertFalse(svc.callHooks(Pointcut.TEST_RB, params));
assertFalse(svc.ifHasCallHooks(Pointcut.TEST_RB, ()->params));
assertFalse(svc.ifHasCallHooks(Pointcut.TEST_RB, () -> params));
}


Expand All @@ -551,7 +554,7 @@ void testObjectWithNoHooks_returnsNull() {
InterceptorService svc = new InterceptorService();

assertNull(svc.callHooksAndReturnObject(Pointcut.TEST_RO, params));
assertNull(svc.ifHasCallHooksAndReturnObject(Pointcut.TEST_RO, ()->params));
assertNull(svc.ifHasCallHooksAndReturnObject(Pointcut.TEST_RO, () -> params));
}

@Test
Expand All @@ -561,7 +564,7 @@ void testObjectWithAllHooksReturnNull_returnsNull() {
svc.registerInterceptor(new ObjectHook<>(null));

assertNull(svc.callHooksAndReturnObject(Pointcut.TEST_RO, params));
assertNull(svc.ifHasCallHooksAndReturnObject(Pointcut.TEST_RO, ()->params));
assertNull(svc.ifHasCallHooksAndReturnObject(Pointcut.TEST_RO, () -> params));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

import java.time.LocalDate;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class RequestPartitionIdTest {
Expand All @@ -26,8 +25,8 @@ public void testHashCode() {
@Test
public void testEquals() {
assertEquals(RequestPartitionId.fromPartitionId(123, LocalDate.of(2020, 1, 1)), RequestPartitionId.fromPartitionId(123, LocalDate.of(2020, 1, 1)));
assertNotEquals(RequestPartitionId.fromPartitionId(123, LocalDate.of(2020, 1, 1)), null);
assertNotEquals(RequestPartitionId.fromPartitionId(123, LocalDate.of(2020, 1, 1)), "123");
assertNotNull(RequestPartitionId.fromPartitionId(123, LocalDate.of(2020, 1, 1)));
assertThat("123").isNotEqualTo(RequestPartitionId.fromPartitionId(123, LocalDate.of(2020, 1, 1)));
}

@Test
Expand All @@ -47,13 +46,13 @@ public void testSerDeserSer() throws JsonProcessingException {
{
RequestPartitionId start = RequestPartitionId.fromPartitionId(123, LocalDate.of(2020, 1, 1));
String json = assertSerDeserSer(start);
assertThat(json, containsString("\"partitionDate\":[2020,1,1]"));
assertThat(json, containsString("\"partitionIds\":[123]"));
assertThat(json).contains("\"partitionDate\":[2020,1,1]");
assertThat(json).contains("\"partitionIds\":[123]");
}
{
RequestPartitionId start = RequestPartitionId.forPartitionIdsAndNames(Lists.newArrayList("Name1", "Name2"), null, null);
String json = assertSerDeserSer(start);
assertThat(json, containsString("partitionNames\":[\"Name1\",\"Name2\"]"));
assertThat(json).contains("partitionNames\":[\"Name1\",\"Name2\"]");
}
assertSerDeserSer(RequestPartitionId.allPartitions());
assertSerDeserSer(RequestPartitionId.defaultPartition());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -67,15 +67,14 @@ public void setUserData_acceptsStringNumberAndBooleansOnly(TestParam theParams)
if (theParams.isExpectedToWork()) {
parameters.setUserData(key, testValue);
assertFalse(parameters.getUserData().isEmpty());
assertEquals(testValue, parameters.getUserData().get(key));
assertThat(parameters.getUserData()).containsEntry(key, testValue);
} else {
try {
parameters.setUserData(key, testValue);
fail();
} catch (IllegalArgumentException ex) {
String dataType = testValue.getClass().getName();
assertTrue(ex.getMessage().contains("Invalid data type provided " + dataType),
ex.getMessage());
assertThat(ex.getMessage().contains("Invalid data type provided " + dataType)).as(ex.getMessage()).isTrue();
assertTrue(parameters.getUserData().isEmpty());
}
}
Expand All @@ -92,8 +91,7 @@ public void setUserData_invalidKey_throws() {
parameters.setUserData(key, "test");
fail();
} catch (IllegalArgumentException ex) {
assertTrue(ex.getMessage().contains("Invalid key; key must be non-empty, non-null"),
ex.getMessage());
assertThat(ex.getMessage().contains("Invalid key; key must be non-empty, non-null")).as(ex.getMessage()).isTrue();
}
}
}
Expand All @@ -106,7 +104,7 @@ public void setUserData_nullValue_removes() {

// test
parameters.setUserData(key, "test");
assertTrue(parameters.getUserData().containsKey(key));
assertThat(parameters.getUserData()).containsKey(key);

parameters.setUserData(key, null);
assertFalse(parameters.getUserData().containsKey(key));
Expand Down
Loading

0 comments on commit cdf5b60

Please sign in to comment.