You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2021/06/22 14:06:12 UTC

[sling-org-apache-sling-graphql-core] branch master updated: SLING-10503 - prepare for testing log messages

This is an automated email from the ASF dual-hosted git repository.

bdelacretaz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-graphql-core.git


The following commit(s) were added to refs/heads/master by this push:
     new 1fc360c  SLING-10503 - prepare for testing log messages
1fc360c is described below

commit 1fc360cfc6f309120ba9757c51f68d1302f2a354
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Tue Jun 22 16:04:26 2021 +0200

    SLING-10503 - prepare for testing log messages
---
 pom.xml                                            |  6 +++
 .../engine/DefaultQueryExecutorLoggingTest.java    | 56 ++++++++++++++++++++
 .../apache/sling/graphql/core/util/LogCapture.java | 61 ++++++++++++++++++++++
 src/test/resources/logback.xml                     | 31 +++++++++++
 4 files changed, 154 insertions(+)

diff --git a/pom.xml b/pom.xml
index 20696bc..6423f05 100644
--- a/pom.xml
+++ b/pom.xml
@@ -309,6 +309,12 @@
       <version>4.5.10</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>ch.qos.logback</groupId>
+      <artifactId>logback-classic</artifactId>
+      <version>1.2.3</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <reporting>
diff --git a/src/test/java/org/apache/sling/graphql/core/engine/DefaultQueryExecutorLoggingTest.java b/src/test/java/org/apache/sling/graphql/core/engine/DefaultQueryExecutorLoggingTest.java
new file mode 100644
index 0000000..8a66efa
--- /dev/null
+++ b/src/test/java/org/apache/sling/graphql/core/engine/DefaultQueryExecutorLoggingTest.java
@@ -0,0 +1,56 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.sling.graphql.core.engine;
+
+import org.apache.sling.graphql.core.mocks.CharacterTypeResolver;
+import org.apache.sling.graphql.core.mocks.EchoDataFetcher;
+import org.apache.sling.graphql.core.mocks.TestUtil;
+import org.apache.sling.graphql.core.util.LogCapture;
+import org.junit.Before;
+import org.junit.Test;
+
+import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+public class DefaultQueryExecutorLoggingTest extends ResourceQueryTestBase {
+
+    private LogCapture capture;
+    
+    protected void setupAdditionalServices() {
+        TestUtil.registerSlingTypeResolver(context.bundleContext(), "character/resolver", new CharacterTypeResolver());
+        TestUtil.registerSlingDataFetcher(context.bundleContext(), "echoNS/echo", new EchoDataFetcher(null));
+    }
+
+    @Before
+    public void setupCapture() {
+        capture = new LogCapture(DefaultQueryExecutor.class.getPackage().getName(), true);
+        capture.start();
+    }
+
+    @Test
+    public void basicTest() throws Exception {
+        final String json = queryJSON("{ currentResource { resourceType } }");
+        assertThat(json, hasJsonPath("$.data.currentResource.resourceType", equalTo(resource.getResourceType())));
+
+        capture.assertContains("GraphQL Schema used for our tests");
+        capture.assertContains("Executing query");
+    }
+
+}
diff --git a/src/test/java/org/apache/sling/graphql/core/util/LogCapture.java b/src/test/java/org/apache/sling/graphql/core/util/LogCapture.java
new file mode 100644
index 0000000..7a4a063
--- /dev/null
+++ b/src/test/java/org/apache/sling/graphql/core/util/LogCapture.java
@@ -0,0 +1,61 @@
+/*
+ * 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.sling.graphql.core.util;
+
+import static org.junit.Assert.fail;
+
+import java.util.function.Predicate;
+import java.util.regex.Pattern;
+import java.util.stream.Stream;
+
+import org.slf4j.LoggerFactory;
+
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.Logger;
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import ch.qos.logback.core.read.ListAppender;
+
+/** Capture logs for testing */
+public class LogCapture extends ListAppender<ILoggingEvent> {
+    private final boolean verboseFailure;
+
+    public LogCapture(String loggerName, boolean verboseFailure) {
+        this.verboseFailure = verboseFailure;
+        Logger logger = (Logger) LoggerFactory.getLogger(loggerName);
+        logger.setLevel(Level.ALL);
+        setContext((LoggerContext) LoggerFactory.getILoggerFactory());
+        logger.addAppender(this);
+    }
+
+    public void assertContains(String substring) {
+        final boolean result = this.list.stream()
+            .filter(event -> event.getFormattedMessage().contains(substring))
+            .findFirst()
+            .isPresent()
+        ;
+        if(!result) {
+            if(verboseFailure) {
+                fail(String.format("No log message contains [%s] in log\n%s", substring, this.list.toString()));
+            } else {
+                fail(String.format("No log message contains [%s]", substring));
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/test/resources/logback.xml b/src/test/resources/logback.xml
new file mode 100644
index 0000000..254317a
--- /dev/null
+++ b/src/test/resources/logback.xml
@@ -0,0 +1,31 @@
+<!--
+  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.
+-->
+<configuration>
+  <appender name="file" class="ch.qos.logback.core.FileAppender">
+    <file>target/test.log</file>
+    <append>true</append>
+    <encoder>
+      <pattern>%date level=%level thread=%thread logger=%logger sourcefile=%file line=%line %mdc message=%msg%n</pattern>
+    </encoder>
+  </appender>
+
+  <root level="INFO">
+    <appender-ref ref="file" />
+  </root>
+
+  <logger name="org.apache.sling.graphql" level="DEBUG"/>
+</configuration>
\ No newline at end of file