Skip to content

Commit

Permalink
#84 - Code refactoring - removed usage of FunctionRenderingSupport type
Browse files Browse the repository at this point in the history
  • Loading branch information
starnowski committed Dec 31, 2023
1 parent 73ee63b commit 2fabd94
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.github.starnowski.posjsonhelper.core.HibernateContext;
import org.hibernate.query.sqm.NodeBuilder;
import org.hibernate.query.sqm.function.FunctionRenderingSupport;
import org.hibernate.query.sqm.function.FunctionRenderer;
import org.hibernate.query.sqm.function.SelfRenderingSqmFunction;
import org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers;
import org.hibernate.query.sqm.tree.SqmCopyContext;
Expand All @@ -24,7 +24,7 @@ public CastOperatorFunction(NodeBuilder nodeBuilder, SqmExpression<?> argument,

public CastOperatorFunction(NodeBuilder nodeBuilder, SqmExpression<?> argument, SqmExpression<String> type, HibernateContext hibernateContext) {
super(nodeBuilder.getQueryEngine().getSqmFunctionRegistry().findFunctionDescriptor(hibernateContext.getCastFunctionOperator()),
(FunctionRenderingSupport) nodeBuilder.getQueryEngine().getSqmFunctionRegistry().findFunctionDescriptor(hibernateContext.getCastFunctionOperator()),
(FunctionRenderer) nodeBuilder.getQueryEngine().getSqmFunctionRegistry().findFunctionDescriptor(hibernateContext.getCastFunctionOperator()),
contactParameters(argument, type),
null,
null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
/**
* Posjsonhelper library is an open-source project that adds support of
* Hibernate query for https://www.postgresql.org/docs/10/functions-json.html)
*
* Copyright (C) 2023 Szymon Tarnowski
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
* Posjsonhelper library is an open-source project that adds support of
* Hibernate query for https://www.postgresql.org/docs/10/functions-json.html)
* <p>
* Copyright (C) 2023 Szymon Tarnowski
* <p>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* <p>
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* <p>
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package com.github.starnowski.posjsonhelper.hibernate6.operators;

import com.github.starnowski.posjsonhelper.core.HibernateContext;
import org.hibernate.query.criteria.JpaExpression;
import org.hibernate.query.sqm.NodeBuilder;
import org.hibernate.query.sqm.function.FunctionRenderer;
import org.hibernate.query.sqm.function.FunctionRenderingSupport;
import org.hibernate.query.sqm.function.SelfRenderingSqmFunction;
import org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers;
import org.hibernate.query.sqm.tree.expression.SqmExpression;
import org.hibernate.type.StandardBasicTypes;

import java.io.Serializable;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import jakarta.persistence.criteria.Path
import org.hibernate.query.spi.QueryEngine
import org.hibernate.query.sqm.NodeBuilder
import org.hibernate.query.sqm.function.FunctionRenderer
import org.hibernate.query.sqm.function.FunctionRenderingSupport
import org.hibernate.query.sqm.function.SqmFunctionDescriptor
import org.hibernate.query.sqm.function.SqmFunctionRegistry
import org.hibernate.query.sqm.internal.SqmCriteriaNodeBuilder
Expand All @@ -17,79 +16,80 @@ import org.mockito.Mockito
import spock.lang.Specification
import spock.lang.Unroll

abstract class AbstractJsonBExtractPathTest <T extends AbstractJsonBExtractPath> extends Specification {
abstract class AbstractJsonBExtractPathTest<T extends AbstractJsonBExtractPath> extends Specification {

@Unroll
def "should pass correct path #path"(){
def "should pass correct path #path"() {
given:
SqmCriteriaNodeBuilder nodeBuilder = Mock(SqmCriteriaNodeBuilder)
SqmBasicValuedSimplePath referencedPathSource = Mock(SqmBasicValuedSimplePath)
SqmCriteriaNodeBuilder nodeBuilder = Mock(SqmCriteriaNodeBuilder)
SqmBasicValuedSimplePath referencedPathSource = Mock(SqmBasicValuedSimplePath)

QueryEngine queryEngine = Mockito.mock(QueryEngine)
SqmFunctionRegistry sqmFunctionRegistry = Mock(SqmFunctionRegistry)
TestInterfaceThatImplementsSqmFunctionDescriptorAndFunctionRenderingSupport ti = Mock(TestInterfaceThatImplementsSqmFunctionDescriptorAndFunctionRenderingSupport)
Mockito.when(queryEngine.getSqmFunctionRegistry()).thenReturn(sqmFunctionRegistry)
TypeConfiguration typeConfiguration = Mock(TypeConfiguration)
BasicTypeRegistry basicTypeRegistry = Mock(BasicTypeRegistry)
org.hibernate.type.BasicType basicType = Mock(org.hibernate.type.BasicType)
List<? extends SqmTypedNode<?>> expectedArguments = new ArrayList<>()
expectedArguments.add(referencedPathSource)
for (String p : path) {
org.hibernate.query.sqm.tree.expression.SqmExpression argument = Mock(org.hibernate.query.sqm.tree.expression.SqmExpression)
expectedArguments.add(argument)
nodeBuilder.value(p) >> argument
}
QueryEngine queryEngine = Mockito.mock(QueryEngine)
SqmFunctionRegistry sqmFunctionRegistry = Mock(SqmFunctionRegistry)
TestInterfaceThatImplementsSqmFunctionDescriptorAndFunctionRenderingSupport ti = Mock(TestInterfaceThatImplementsSqmFunctionDescriptorAndFunctionRenderingSupport)
Mockito.when(queryEngine.getSqmFunctionRegistry()).thenReturn(sqmFunctionRegistry)
TypeConfiguration typeConfiguration = Mock(TypeConfiguration)
BasicTypeRegistry basicTypeRegistry = Mock(BasicTypeRegistry)
org.hibernate.type.BasicType basicType = Mock(org.hibernate.type.BasicType)
List<? extends SqmTypedNode<?>> expectedArguments = new ArrayList<>()
expectedArguments.add(referencedPathSource)
for (String p : path) {
org.hibernate.query.sqm.tree.expression.SqmExpression argument = Mock(org.hibernate.query.sqm.tree.expression.SqmExpression)
expectedArguments.add(argument)
nodeBuilder.value(p) >> argument
}

when:
T tested = prepareTestObject(referencedPathSource, nodeBuilder, path)
T tested = prepareTestObject(referencedPathSource, nodeBuilder, path)

then:
2 * nodeBuilder.getQueryEngine() >> queryEngine
2 * sqmFunctionRegistry.findFunctionDescriptor(expectedFunctionName()) >> ti
1 * nodeBuilder.getTypeConfiguration() >> typeConfiguration
1 * typeConfiguration.getBasicTypeRegistry() >> basicTypeRegistry
1 * basicTypeRegistry.resolve(StandardBasicTypes.STRING) >> basicType
tested.nodeBuilder().is(nodeBuilder)
tested.getFunctionName() == expectedFunctionName()
tested.getArguments() == expectedArguments
2 * nodeBuilder.getQueryEngine() >> queryEngine
2 * sqmFunctionRegistry.findFunctionDescriptor(expectedFunctionName()) >> ti
1 * nodeBuilder.getTypeConfiguration() >> typeConfiguration
1 * typeConfiguration.getBasicTypeRegistry() >> basicTypeRegistry
1 * basicTypeRegistry.resolve(StandardBasicTypes.STRING) >> basicType
tested.nodeBuilder().is(nodeBuilder)
tested.getFunctionName() == expectedFunctionName()
tested.getArguments() == expectedArguments

where:
path << [["some", "property"], ["child1", "grandson1"]]
path << [["some", "property"], ["child1", "grandson1"]]
}

@Unroll
def "should throw exception when path argument is empty or null #path"() {
given:
SqmCriteriaNodeBuilder nodeBuilder = Mock(SqmCriteriaNodeBuilder)
SqmBasicValuedSimplePath referencedPathSource = Mock(SqmBasicValuedSimplePath)
SqmCriteriaNodeBuilder nodeBuilder = Mock(SqmCriteriaNodeBuilder)
SqmBasicValuedSimplePath referencedPathSource = Mock(SqmBasicValuedSimplePath)

QueryEngine queryEngine = Mockito.mock(QueryEngine)
SqmFunctionRegistry sqmFunctionRegistry = Mock(SqmFunctionRegistry)
TestInterfaceThatImplementsSqmFunctionDescriptorAndFunctionRenderingSupport ti = Mock(TestInterfaceThatImplementsSqmFunctionDescriptorAndFunctionRenderingSupport)
Mockito.when(queryEngine.getSqmFunctionRegistry()).thenReturn(sqmFunctionRegistry)
TypeConfiguration typeConfiguration = Mock(TypeConfiguration)
BasicTypeRegistry basicTypeRegistry = Mock(BasicTypeRegistry)
org.hibernate.type.BasicType basicType = Mock(org.hibernate.type.BasicType)
QueryEngine queryEngine = Mockito.mock(QueryEngine)
SqmFunctionRegistry sqmFunctionRegistry = Mock(SqmFunctionRegistry)
TestInterfaceThatImplementsSqmFunctionDescriptorAndFunctionRenderingSupport ti = Mock(TestInterfaceThatImplementsSqmFunctionDescriptorAndFunctionRenderingSupport)
Mockito.when(queryEngine.getSqmFunctionRegistry()).thenReturn(sqmFunctionRegistry)
TypeConfiguration typeConfiguration = Mock(TypeConfiguration)
BasicTypeRegistry basicTypeRegistry = Mock(BasicTypeRegistry)
org.hibernate.type.BasicType basicType = Mock(org.hibernate.type.BasicType)

when:
prepareTestObject(referencedPathSource, nodeBuilder, path)
prepareTestObject(referencedPathSource, nodeBuilder, path)

then:
nodeBuilder.getQueryEngine() >> queryEngine
sqmFunctionRegistry.findFunctionDescriptor(expectedFunctionName()) >> ti
nodeBuilder.getTypeConfiguration() >> typeConfiguration
typeConfiguration.getBasicTypeRegistry() >> basicTypeRegistry
basicTypeRegistry.resolve(StandardBasicTypes.STRING) >> basicType
def ex = thrown(IllegalArgumentException)
ex.message == "Path argument can not be null or empty list"
nodeBuilder.getQueryEngine() >> queryEngine
sqmFunctionRegistry.findFunctionDescriptor(expectedFunctionName()) >> ti
nodeBuilder.getTypeConfiguration() >> typeConfiguration
typeConfiguration.getBasicTypeRegistry() >> basicTypeRegistry
basicTypeRegistry.resolve(StandardBasicTypes.STRING) >> basicType
def ex = thrown(IllegalArgumentException)
ex.message == "Path argument can not be null or empty list"

where:
path << [null, []]
path << [null, []]
}

protected abstract T prepareTestObject(Path referencedPathSource, NodeBuilder nodeBuilder, List<String> path)

protected abstract String expectedFunctionName()

private static interface TestInterfaceThatImplementsSqmFunctionDescriptorAndFunctionRenderingSupport extends SqmFunctionDescriptor, FunctionRenderer {}
private static interface TestInterfaceThatImplementsSqmFunctionDescriptorAndFunctionRenderingSupport extends SqmFunctionDescriptor, FunctionRenderer {
}
}
Loading

0 comments on commit 2fabd94

Please sign in to comment.