DATACOUCH-24 - Adding testing for view-bases ops in template & repository.
This commit is contained in:
@@ -17,6 +17,8 @@
|
|||||||
package org.springframework.data.couchbase.core;
|
package org.springframework.data.couchbase.core;
|
||||||
|
|
||||||
import com.couchbase.client.CouchbaseClient;
|
import com.couchbase.client.CouchbaseClient;
|
||||||
|
import com.couchbase.client.protocol.views.Query;
|
||||||
|
import com.couchbase.client.protocol.views.Stale;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@@ -40,7 +42,7 @@ import static org.junit.Assert.*;
|
|||||||
*/
|
*/
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = TestApplicationConfig.class)
|
@ContextConfiguration(classes = TestApplicationConfig.class)
|
||||||
@TestExecutionListeners(BucketCreationListener.class)
|
@TestExecutionListeners({BucketCreationListener.class, CouchbaseTemplateViewListener.class})
|
||||||
public class CouchbaseTemplateTest {
|
public class CouchbaseTemplateTest {
|
||||||
|
|
||||||
private CouchbaseClient client;
|
private CouchbaseClient client;
|
||||||
@@ -177,6 +179,21 @@ public class CouchbaseTemplateTest {
|
|||||||
assertEquals(name, found.getName());
|
assertEquals(name, found.getName());
|
||||||
assertEquals(active, found.getActive());
|
assertEquals(active, found.getActive());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldLoadAndMapViewDocs() {
|
||||||
|
Query query = new Query();
|
||||||
|
query.setStale(Stale.FALSE);
|
||||||
|
|
||||||
|
final List<Beer> beers = template.findByView("test_beers", "by_name", query, Beer.class);
|
||||||
|
assertEquals(101, beers.size());
|
||||||
|
|
||||||
|
for(Beer beer : beers) {
|
||||||
|
assertNotNull(beer.getId());
|
||||||
|
assertNotNull(beer.getName());
|
||||||
|
assertNotNull(beer.getActive());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A sample document with just an id and property.
|
* A sample document with just an id and property.
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
* 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.core;
|
||||||
|
|
||||||
|
import com.couchbase.client.CouchbaseClient;
|
||||||
|
import com.couchbase.client.protocol.views.DesignDocument;
|
||||||
|
import com.couchbase.client.protocol.views.ViewDesign;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.test.context.TestContext;
|
||||||
|
import org.springframework.test.context.support.AbstractTestExecutionListener;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Michael Nitschinger
|
||||||
|
*/
|
||||||
|
public class CouchbaseTemplateViewListener extends AbstractTestExecutionListener {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void beforeTestClass(final TestContext testContext) throws Exception {
|
||||||
|
CouchbaseClient client = bootstrapClient(testContext.getApplicationContext());
|
||||||
|
populateTestData(client);
|
||||||
|
createAndWaitForDesignDocs(client);
|
||||||
|
}
|
||||||
|
|
||||||
|
private CouchbaseClient bootstrapClient(ApplicationContext context) throws Exception {
|
||||||
|
String host = (String) context.getBean("couchbaseHost");
|
||||||
|
String bucket = (String) context.getBean("couchbaseBucket");
|
||||||
|
String password = (String) context.getBean("couchbasePassword");
|
||||||
|
|
||||||
|
return new CouchbaseClient(Arrays.asList(new URI(host)), bucket, password);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void populateTestData(CouchbaseClient client) {
|
||||||
|
CouchbaseTemplate template = new CouchbaseTemplate(client);
|
||||||
|
|
||||||
|
for(int i=0;i < 100; i++) {
|
||||||
|
Beer b = new Beer("testbeer-" + i).setName("MyBeer " + i).setActive(true);
|
||||||
|
template.save(b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createAndWaitForDesignDocs(CouchbaseClient client) {
|
||||||
|
DesignDocument designDoc = new DesignDocument("test_beers");
|
||||||
|
String mapFunction = "function (doc, meta) { if(doc._class == "
|
||||||
|
+ "\"org.springframework.data.couchbase.core.Beer\") { emit(doc.name, null); } }";
|
||||||
|
designDoc.setView(new ViewDesign("by_name", mapFunction));
|
||||||
|
client.createDesignDoc(designDoc);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import com.couchbase.client.CouchbaseClient;
|
||||||
|
import com.couchbase.client.protocol.views.DesignDocument;
|
||||||
|
import com.couchbase.client.protocol.views.ViewDesign;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.data.couchbase.core.Beer;
|
||||||
|
import org.springframework.data.couchbase.core.CouchbaseTemplate;
|
||||||
|
import org.springframework.test.context.TestContext;
|
||||||
|
import org.springframework.test.context.support.AbstractTestExecutionListener;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Michael Nitschinger
|
||||||
|
*/
|
||||||
|
public class CouchbaseRepositoryViewListener extends AbstractTestExecutionListener {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void beforeTestClass(final TestContext testContext) throws Exception {
|
||||||
|
CouchbaseClient client = bootstrapClient(testContext.getApplicationContext());
|
||||||
|
populateTestData(client);
|
||||||
|
createAndWaitForDesignDocs(client);
|
||||||
|
}
|
||||||
|
|
||||||
|
private CouchbaseClient bootstrapClient(ApplicationContext context) throws Exception {
|
||||||
|
String host = (String) context.getBean("couchbaseHost");
|
||||||
|
String bucket = (String) context.getBean("couchbaseBucket");
|
||||||
|
String password = (String) context.getBean("couchbasePassword");
|
||||||
|
|
||||||
|
return new CouchbaseClient(Arrays.asList(new URI(host)), bucket, password);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void populateTestData(CouchbaseClient client) {
|
||||||
|
CouchbaseTemplate template = new CouchbaseTemplate(client);
|
||||||
|
|
||||||
|
for(int i=0;i < 100; i++) {
|
||||||
|
User u = new User("testuser-" + i, "uname" + i);
|
||||||
|
template.save(u);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createAndWaitForDesignDocs(CouchbaseClient client) {
|
||||||
|
DesignDocument designDoc = new DesignDocument("user");
|
||||||
|
String mapFunction = "function (doc, meta) { if(doc._class == "
|
||||||
|
+ "\"org.springframework.data.couchbase.repository.User\") { emit(null, null); } }";
|
||||||
|
designDoc.setView(new ViewDesign("all", mapFunction, "_count"));
|
||||||
|
client.createDesignDoc(designDoc);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -17,6 +17,8 @@
|
|||||||
package org.springframework.data.couchbase.repository;
|
package org.springframework.data.couchbase.repository;
|
||||||
|
|
||||||
import com.couchbase.client.CouchbaseClient;
|
import com.couchbase.client.CouchbaseClient;
|
||||||
|
import com.couchbase.client.protocol.views.Query;
|
||||||
|
import com.couchbase.client.protocol.views.Stale;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@@ -40,7 +42,7 @@ import static org.junit.Assert.*;
|
|||||||
*/
|
*/
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = TestApplicationConfig.class)
|
@ContextConfiguration(classes = TestApplicationConfig.class)
|
||||||
@TestExecutionListeners(BucketCreationListener.class)
|
@TestExecutionListeners({BucketCreationListener.class, CouchbaseRepositoryViewListener.class})
|
||||||
public class SimpleCouchbaseRepositoryTest {
|
public class SimpleCouchbaseRepositoryTest {
|
||||||
|
|
||||||
private CouchbaseClient client;
|
private CouchbaseClient client;
|
||||||
@@ -56,17 +58,19 @@ public class SimpleCouchbaseRepositoryTest {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private String couchbasePassword;
|
private String couchbasePassword;
|
||||||
|
|
||||||
|
private UserRepository repository;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() throws Exception {
|
public void setup() throws Exception {
|
||||||
client = new CouchbaseClient(Arrays.asList(new URI(couchbaseHost)), couchbaseBucket, couchbasePassword);
|
client = new CouchbaseClient(Arrays.asList(new URI(couchbaseHost)), couchbaseBucket, couchbasePassword);
|
||||||
template = new CouchbaseTemplate(client);
|
template = new CouchbaseTemplate(client);
|
||||||
|
RepositoryFactorySupport factory = new CouchbaseRepositoryFactory(template);
|
||||||
|
repository = factory.getRepository(UserRepository.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void simpleCrud() {
|
public void simpleCrud() {
|
||||||
String key = "my_unique_user_key";
|
String key = "my_unique_user_key";
|
||||||
RepositoryFactorySupport factory = new CouchbaseRepositoryFactory(template);
|
|
||||||
UserRepository repository = factory.getRepository(UserRepository.class);
|
|
||||||
User instance = new User(key, "foobar");
|
User instance = new User(key, "foobar");
|
||||||
repository.save(instance);
|
repository.save(instance);
|
||||||
|
|
||||||
@@ -81,4 +85,27 @@ public class SimpleCouchbaseRepositoryTest {
|
|||||||
assertFalse(repository.exists(key));
|
assertFalse(repository.exists(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldFindAll() {
|
||||||
|
// do a non-stale query to populate data for testing.
|
||||||
|
client.query(client.getView("user", "all"), new Query().setStale(Stale.FALSE));
|
||||||
|
|
||||||
|
Iterable<User> allUsers = repository.findAll();
|
||||||
|
int size = 0;
|
||||||
|
for (User u : allUsers) {
|
||||||
|
size++;
|
||||||
|
assertNotNull(u.getKey());
|
||||||
|
assertNotNull(u.getUsername());
|
||||||
|
}
|
||||||
|
assertEquals(100, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldCount() {
|
||||||
|
// do a non-stale query to populate data for testing.
|
||||||
|
client.query(client.getView("user", "all"), new Query().setStale(Stale.FALSE));
|
||||||
|
|
||||||
|
assertEquals(100, repository.count());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,19 @@
|
|||||||
|
/*
|
||||||
|
* 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.util;
|
package org.springframework.data.couchbase.util;
|
||||||
|
|
||||||
import com.couchbase.client.CouchbaseClient;
|
import com.couchbase.client.CouchbaseClient;
|
||||||
@@ -8,7 +24,9 @@ import org.springframework.test.context.support.DependencyInjectionTestExecution
|
|||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Michael Nitschinger
|
||||||
|
*/
|
||||||
public class BucketCreationListener extends DependencyInjectionTestExecutionListener {
|
public class BucketCreationListener extends DependencyInjectionTestExecutionListener {
|
||||||
|
|
||||||
private CouchbaseClient client;
|
private CouchbaseClient client;
|
||||||
|
|||||||
Reference in New Issue
Block a user