org.springframework.data.projection.SpelAwareProxyProjectionFactory Java Examples

The following examples show how to use org.springframework.data.projection.SpelAwareProxyProjectionFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PartTreeDatastoreQueryTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
private void queryWithMockResult(String queryName, List results, Method m, boolean mockOptionalNullable,
		ProjectionInformation projectionInformation) {
	when(this.queryMethod.getName()).thenReturn(queryName);
	doReturn(new DefaultParameters(m))
			.when(this.queryMethod).getParameters();
	if (mockOptionalNullable) {
		DefaultRepositoryMetadata mockMetadata = mock(DefaultRepositoryMetadata.class);
		doReturn(m.getReturnType()).when(mockMetadata).getReturnedDomainClass(m);
		DatastoreQueryMethod datastoreQueryMethod =
				new DatastoreQueryMethod(m,
						mockMetadata,
						mock(SpelAwareProxyProjectionFactory.class));
		doReturn(datastoreQueryMethod.isOptionalReturnType())
				.when(this.queryMethod).isOptionalReturnType();
		doReturn(datastoreQueryMethod.isNullable())
				.when(this.queryMethod).isNullable();
	}

	this.partTreeDatastoreQuery = createQuery(false, false, projectionInformation);
	when(this.datastoreTemplate.queryKeysOrEntities(any(), Mockito.<Class<Trade>>any()))
			.thenReturn(new DatastoreResultsIterable<>(
					results != null ? results.iterator() : Collections.emptyIterator(), null));
}
 
Example #2
Source File: SpelQueryCreatorUnitTests.java    From spring-data-keyvalue with Apache License 2.0 6 votes vote down vote up
private KeyValueQuery<SpelExpression> createQueryForMethodWithArgs(String methodName, Object... args)
		throws NoSuchMethodException, SecurityException {

	Class<?>[] argTypes = new Class<?>[args.length];
	if (!ObjectUtils.isEmpty(args)) {

		for (int i = 0; i < args.length; i++) {
			argTypes[i] = args[i].getClass();
		}
	}

	Method method = PersonRepository.class.getMethod(methodName, argTypes);
	doReturn(Person.class).when(metadataMock).getReturnedDomainClass(method);

	PartTree partTree = new PartTree(method.getName(), method.getReturnType());
	SpelQueryCreator creator = new SpelQueryCreator(partTree, new ParametersParameterAccessor(
			new QueryMethod(method, metadataMock, new SpelAwareProxyProjectionFactory()).getParameters(), args));

	KeyValueQuery<SpelExpression> q = creator.createQuery();
	q.getCriteria().setEvaluationContext(
			SimpleEvaluationContext.forReadOnlyDataBinding().withRootObject(args).withInstanceMethods().build());

	return q;
}
 
Example #3
Source File: SpelQueryEngineUnitTests.java    From spring-data-keyvalue with Apache License 2.0 6 votes vote down vote up
private static SpelCriteria createQueryForMethodWithArgs(String methodName, Object... args) throws Exception {

		List<Class<?>> types = new ArrayList<>(args.length);

		for (Object arg : args) {
			types.add(arg.getClass());
		}

		Method method = PersonRepository.class.getMethod(methodName, types.toArray(new Class<?>[types.size()]));
		RepositoryMetadata metadata = mock(RepositoryMetadata.class);
		doReturn(method.getReturnType()).when(metadata).getReturnedDomainClass(method);

		PartTree partTree = new PartTree(method.getName(), method.getReturnType());
		SpelQueryCreator creator = new SpelQueryCreator(partTree, new ParametersParameterAccessor(
				new QueryMethod(method, metadata, new SpelAwareProxyProjectionFactory()).getParameters(), args));

		return new SpelCriteria(creator.createQuery().getCriteria(),
				SimpleEvaluationContext.forReadOnlyDataBinding().withInstanceMethods().withRootObject(args).build());
	}
 
Example #4
Source File: AggregateQueryProvider2Test.java    From mongodb-aggregate-query-support with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "queryMethods", expectedExceptions = {JsonParseException.class})
public void testCreateAggregateQuery(String methodName) throws Exception {
  AggregateQuerySupportingRepositoryFactory factory = new AggregateQuerySupportingRepositoryFactory(mongoOperations,
                                                                                                    queryExecutor);
  QueryLookupStrategy lookupStrategy = factory.getQueryLookupStrategy(CREATE_IF_NOT_FOUND, evaluationContextProvider);

  Method method = TestAggregateRepository22.class.getMethod(methodName);
  RepositoryMetadata repositoryMetadata = new DefaultRepositoryMetadata(TestAggregateRepository22.class);
  ProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory();
  RepositoryQuery query = lookupStrategy.resolveQuery(method, repositoryMetadata, projectionFactory, null);
  assertNotNull(query);
  Object object = query.execute(new Object[0]);
  assertNull(object);
}
 
Example #5
Source File: ReactiveAggregateQueryProviderTest.java    From mongodb-aggregate-query-support with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "queryMethods", expectedExceptions = {JsonParseException.class})
public void testCreateAggregateQuery(String methodName) throws Exception {
  ReactiveAggregateQuerySupportingRepositoryFactory factory = new ReactiveAggregateQuerySupportingRepositoryFactory(mongoOperations,
                                                                                                                    queryExecutor);
  Optional<QueryLookupStrategy> lookupStrategy = factory.getQueryLookupStrategy(CREATE_IF_NOT_FOUND, evaluationContextProvider);

  Assert.isTrue(lookupStrategy.isPresent(), "Lookup strategy must not be null");
  Method method = ReactiveTestAggregateRepository22.class.getMethod(methodName);
  RepositoryMetadata repositoryMetadata = new DefaultRepositoryMetadata(ReactiveTestAggregateRepository22.class);
  ProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory();
  RepositoryQuery query = lookupStrategy.get().resolveQuery(method, repositoryMetadata, projectionFactory, null);
  assertNotNull(query);
  Object object = query.execute(new Object[0]);
  assertNull(object);
}
 
Example #6
Source File: AggregateQueryProvider2Test.java    From mongodb-aggregate-query-support with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "queryMethods", expectedExceptions = {JsonParseException.class})
public void testCreateAggregateQuery(String methodName) throws Exception {
  NonReactiveAggregateQuerySupportingRepositoryFactory factory =
      new NonReactiveAggregateQuerySupportingRepositoryFactory(mongoOperations, queryExecutor);
  Optional<QueryLookupStrategy> lookupStrategy = factory.getQueryLookupStrategy(CREATE_IF_NOT_FOUND, evaluationContextProvider);
  assertTrue(lookupStrategy.isPresent(), "Expecting non null lookupStrategy");
  Method method = TestAggregateRepository22.class.getMethod(methodName);
  RepositoryMetadata repositoryMetadata = new DefaultRepositoryMetadata(TestAggregateRepository22.class);
  ProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory();
  RepositoryQuery query = lookupStrategy.get().resolveQuery(method, repositoryMetadata, projectionFactory, null);
  assertNotNull(query);
  Object object = query.execute(new Object[0]);
  assertNull(object);
}