From 2cfd5cf985174685ec38f825113ffa8184ddcc40 Mon Sep 17 00:00:00 2001 From: Zhen Date: Mon, 26 Oct 2015 17:17:12 +0100 Subject: [PATCH] Return an empty stats if no stat is included in summary Added a test to verify behaviours when empty result summary returned --- .../java/org/neo4j/driver/ResultSummary.java | 10 +-- .../summary/SimpleUpdateStatistics.java | 2 + .../internal/summary/SummaryBuilder.java | 4 +- .../driver/internal/SummaryBuilderTest.java | 81 +++++++++++++++++++ 4 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 driver/src/test/java/org/neo4j/driver/internal/SummaryBuilderTest.java diff --git a/driver/src/main/java/org/neo4j/driver/ResultSummary.java b/driver/src/main/java/org/neo4j/driver/ResultSummary.java index 394829550c..4016e0430f 100644 --- a/driver/src/main/java/org/neo4j/driver/ResultSummary.java +++ b/driver/src/main/java/org/neo4j/driver/ResultSummary.java @@ -24,9 +24,9 @@ * The result summary of running a statement. The result summary interface can be used to investigate * details about the result, like the type of query run, how many and which kinds of updates have been executed, * and query plan and profiling information if available. - *

+ * * The result summary is only available after all result records have been consumed. - *

+ * * Keeping the result summary around does not influence the lifecycle of any associated session and/or transaction. */ public interface ResultSummary @@ -59,8 +59,7 @@ public interface ResultSummary /** * This describes how the database will execute your statement. * - * @throws IllegalStateException if {@link #hasPlan()} is false - * @return statement plan for the executed statement if available + * @return statement plan for the executed statement if available, otherwise null */ Plan plan(); @@ -71,8 +70,7 @@ public interface ResultSummary * information about what each step of the plan did. That more in-depth version of the statement plan becomes * available here. * - * @throws IllegalStateException if {@link #hasProfile()} is false - * @return profiled statement plan for the executed statement if available + * @return profiled statement plan for the executed statement if available, otherwise null */ ProfiledPlan profile(); diff --git a/driver/src/main/java/org/neo4j/driver/internal/summary/SimpleUpdateStatistics.java b/driver/src/main/java/org/neo4j/driver/internal/summary/SimpleUpdateStatistics.java index 3f9924a65c..c392578a2f 100644 --- a/driver/src/main/java/org/neo4j/driver/internal/summary/SimpleUpdateStatistics.java +++ b/driver/src/main/java/org/neo4j/driver/internal/summary/SimpleUpdateStatistics.java @@ -22,6 +22,8 @@ public class SimpleUpdateStatistics implements UpdateStatistics { + public static final SimpleUpdateStatistics EMPTY_STATS = + new SimpleUpdateStatistics( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ); private final int nodesCreated; private final int nodesDeleted; private final int relationshipsCreated; diff --git a/driver/src/main/java/org/neo4j/driver/internal/summary/SummaryBuilder.java b/driver/src/main/java/org/neo4j/driver/internal/summary/SummaryBuilder.java index f36ff9595d..2507b54da0 100644 --- a/driver/src/main/java/org/neo4j/driver/internal/summary/SummaryBuilder.java +++ b/driver/src/main/java/org/neo4j/driver/internal/summary/SummaryBuilder.java @@ -32,6 +32,8 @@ import org.neo4j.driver.exceptions.ClientException; import org.neo4j.driver.internal.spi.StreamCollector; +import static org.neo4j.driver.internal.summary.SimpleUpdateStatistics.EMPTY_STATS; + public class SummaryBuilder implements StreamCollector { private final Statement statement; @@ -136,7 +138,7 @@ public Statement statement() @Override public UpdateStatistics updateStatistics() { - return statistics; + return statistics == null ? EMPTY_STATS : statistics; } @Override diff --git a/driver/src/test/java/org/neo4j/driver/internal/SummaryBuilderTest.java b/driver/src/test/java/org/neo4j/driver/internal/SummaryBuilderTest.java new file mode 100644 index 0000000000..b320d70910 --- /dev/null +++ b/driver/src/test/java/org/neo4j/driver/internal/SummaryBuilderTest.java @@ -0,0 +1,81 @@ +/** + * Copyright (c) 2002-2015 "Neo Technology," + * Network Engine for Objects in Lund AB [http://neotechnology.com] + * + * This file is part of Neo4j. + * + * 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 org.neo4j.driver.internal; + +import org.junit.Test; + +import org.neo4j.driver.ResultSummary; +import org.neo4j.driver.Statement; +import org.neo4j.driver.UpdateStatistics; +import org.neo4j.driver.internal.summary.SimpleUpdateStatistics; +import org.neo4j.driver.internal.summary.SummaryBuilder; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.mock; + +public class SummaryBuilderTest +{ + @Test + public void shouldReturnEmptyStatisticsIfNotProvided() throws Throwable + { + // Given + SummaryBuilder builder = new SummaryBuilder( mock( Statement.class ) ); + + // When + ResultSummary summary = builder.build(); + UpdateStatistics stats = summary.updateStatistics(); + + // Then + assertEquals( stats, new SimpleUpdateStatistics( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ) ); + } + + + @Test + public void shouldReturnNullIfNoPlanProfileProvided() throws Throwable + { + // Given + SummaryBuilder builder = new SummaryBuilder( mock( Statement.class ) ); + + // When + ResultSummary summary = builder.build(); + + // Then + assertThat( summary.hasPlan(), equalTo( false ) ); + assertThat( summary.hasProfile(), equalTo( false ) ); + assertNull( summary.plan() ); + assertNull( summary.profile() ); + } + + + @Test + public void shouldReturnNullIfNoStatementTypeProvided() throws Throwable + { + // Given + SummaryBuilder builder = new SummaryBuilder( mock( Statement.class ) ); + + // When + ResultSummary summary = builder.build(); + + // Then + assertNull( summary.statementType() ); + } +}