DATACOUCH-190 - Document SpEL support example.

This commit is contained in:
Simon Baslé
2016-01-15 18:35:04 +01:00
parent 15d0d27d71
commit 6876e179f3
4 changed files with 169 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
package org.springframework.data.couchbase.repository.spel;
import java.util.Collections;
import java.util.Map;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.IntegrationTestApplicationConfig;
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
import org.springframework.data.repository.query.EvaluationContextProvider;
import org.springframework.data.repository.query.ExtensionAwareEvaluationContextProvider;
import org.springframework.data.repository.query.spi.EvaluationContextExtension;
import org.springframework.data.repository.query.spi.EvaluationContextExtensionSupport;
@Configuration
@EnableCouchbaseRepositories
public class SpelConfig extends IntegrationTestApplicationConfig {
@Bean
public EvaluationContextExtension customSpelExtension() {
return new CustomSpelExtension();
}
public static class CustomSpelExtension extends EvaluationContextExtensionSupport {
/**
* Returns the identifier of the extension. The id can be leveraged by users to fully qualify property lookups and
* thus overcome ambiguities in case multiple extensions expose properties with the same name.
*
* @return the extension id, must not be {@literal null}.
*/
@Override
public String getExtensionId() {
return "custom";
}
@Override
public Map<String, Object> getProperties() {
return Collections.<String, Object>singletonMap("oneCustomer", "uname-3");
}
}
}

View File

@@ -0,0 +1,16 @@
package org.springframework.data.couchbase.repository.spel;
import java.util.List;
import org.springframework.data.couchbase.core.query.N1qlPrimaryIndexed;
import org.springframework.data.couchbase.core.query.Query;
import org.springframework.data.couchbase.repository.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@N1qlPrimaryIndexed
public interface SpelRepository extends CrudRepository<User, String> {
@Query("#{#n1ql.selectEntity} WHERE #{#n1ql.filter} AND username = \"#{oneCustomer}\"")
List<User> findCustomUsers();
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright 2013 the original author or authors.
*
* 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.springframework.data.couchbase.repository.spel;
import static org.junit.Assert.*;
import java.util.List;
import com.couchbase.client.java.Bucket;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.couchbase.repository.SimpleCouchbaseRepositoryListener;
import org.springframework.data.couchbase.repository.User;
import org.springframework.data.couchbase.repository.config.RepositoryOperationsMapping;
import org.springframework.data.couchbase.repository.support.CouchbaseRepositoryFactory;
import org.springframework.data.couchbase.repository.support.IndexManager;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.data.repository.query.EvaluationContextProvider;
import org.springframework.data.repository.query.spi.EvaluationContextExtension;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Simon Baslé
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpelConfig.class)
@TestExecutionListeners(SimpleCouchbaseRepositoryListener.class)
public class SpelRepositoryTests {
@Autowired
private Bucket client;
@Autowired
private RepositoryOperationsMapping operationsMapping;
@Autowired
private IndexManager indexManager;
@Autowired
private SpelRepository repository;
@Test
public void testSpelExtensionResolved() {
List<User> users = repository.findCustomUsers();
assertEquals(1, users.size());
assertEquals("testuser-3", users.get(0).getKey());
assertEquals("uname-3", users.get(0).getUsername());
}
}