Added a couple more methods to the logback assert test helper (#6004)

* add negative method

* add throwable matcher for logback

* logback asserts

* merge master

* update StaticLogbackTestExtension usage
This commit is contained in:
Ken Stevens 2024-06-15 15:27:54 -04:00 committed by GitHub
parent 57d1815b46
commit c1c6d4fc49
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 92 additions and 0 deletions

View File

@ -1,3 +1,22 @@
/*-
* #%L
* HAPI FHIR - Core Library
* %%
* Copyright (C) 2014 - 2024 Smile CDR, 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.
* #L%
*/
package ca.uhn.fhir.parser; package ca.uhn.fhir.parser;
import ca.uhn.fhir.parser.path.EncodeContextPath; import ca.uhn.fhir.parser.path.EncodeContextPath;

View File

@ -1,3 +1,22 @@
/*-
* #%L
* HAPI FHIR JPA Server Test Utilities
* %%
* Copyright (C) 2014 - 2024 Smile CDR, 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.
* #L%
*/
package ca.uhn.fhir.jpa.term; package ca.uhn.fhir.jpa.term;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;

View File

@ -83,4 +83,37 @@ public class LogEventIterableAssert extends AbstractIterableAssert<LogEventItera
protected LogEventIterableAssert newAbstractIterableAssert(Iterable<? extends ILoggingEvent> iterable) { protected LogEventIterableAssert newAbstractIterableAssert(Iterable<? extends ILoggingEvent> iterable) {
return new LogEventIterableAssert((Collection<ILoggingEvent>) iterable); return new LogEventIterableAssert((Collection<ILoggingEvent>) iterable);
} }
public LogEventIterableAssert doesNotHaveEventWithMessage(String thePartial) {
isNotNull();
matches(logEvents -> logEvents.stream()
.map(ILoggingEvent::getFormattedMessage)
.noneMatch(message -> message.contains(thePartial)),
"Log Events should have no message with "+ thePartial + " in it.");
return this;
}
public LogEventIterableAssert hasEventWithLevelAndMessageAndThrew(Level theLevel, String thePartial, String theExceptionMessage) {
isNotNull();
matches(logEvents -> logEvents.stream()
.filter(message -> message.getMessage().contains(thePartial))
.filter(message -> message.getLevel() == theLevel)
.anyMatch(message -> message.getThrowableProxy().getMessage().contains(theExceptionMessage)),
"Log Events should have at least one message with "+ thePartial + " in it.");
return this;
}
public LogEventIterableAssert doesNotHaveEventWithLevelAndMessage(Level theLevel, String thePartial) {
isNotNull();
matches(logEvents -> logEvents.stream()
.filter(e -> e.getLevel() == theLevel)
.map(ILoggingEvent::getFormattedMessage)
.noneMatch(message -> message.contains(thePartial)),
"Log Events should have no " + theLevel.toString() + " message with "+ thePartial + " in it.");
return this;
}
} }

View File

@ -19,6 +19,8 @@
*/ */
package ca.uhn.test.util; package ca.uhn.test.util;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.filter.ThresholdFilter;
import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.classic.spi.ILoggingEvent;
import org.junit.jupiter.api.extension.AfterAllCallback; import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback; import org.junit.jupiter.api.extension.BeforeAllCallback;
@ -42,6 +44,16 @@ public class StaticLogbackTestExtension implements BeforeAllCallback, AfterAllCa
myLogbackTestExtension = new LogbackTestExtension(); myLogbackTestExtension = new LogbackTestExtension();
} }
public static StaticLogbackTestExtension withThreshold(Level theLevel) {
LogbackTestExtension logbackTestExtension = new LogbackTestExtension();
logbackTestExtension.setUp(theLevel);
ThresholdFilter thresholdFilter = new ThresholdFilter();
thresholdFilter.setLevel(theLevel.levelStr);
logbackTestExtension.getAppender().addFilter(thresholdFilter);
return new StaticLogbackTestExtension(logbackTestExtension);
}
@Override @Override
public void beforeAll(ExtensionContext theExtensionContext) throws Exception { public void beforeAll(ExtensionContext theExtensionContext) throws Exception {
myLogbackTestExtension.beforeEach(theExtensionContext); myLogbackTestExtension.beforeEach(theExtensionContext);
@ -55,4 +67,13 @@ public class StaticLogbackTestExtension implements BeforeAllCallback, AfterAllCa
public List<ILoggingEvent> filterLoggingEventsWithMessageEqualTo(String theMessageText) { public List<ILoggingEvent> filterLoggingEventsWithMessageEqualTo(String theMessageText) {
return myLogbackTestExtension.filterLoggingEventsWithMessageEqualTo(theMessageText); return myLogbackTestExtension.filterLoggingEventsWithMessageEqualTo(theMessageText);
} }
/**
* Returns a copy to avoid concurrent modification errors.
* @return A copy of the log events so far.
*/
public java.util.List<ILoggingEvent> getLogEvents() {
return myLogbackTestExtension.getLogEvents();
}
} }