From 0d7c2df2a6faf4eaf070dd34474c81ea30d1125c Mon Sep 17 00:00:00 2001 From: Michael Nitschinger Date: Sun, 28 Jul 2013 11:47:23 +0200 Subject: [PATCH] DATACOUCH-24 - Adding testing for view-bases ops in template & repository. --- .../couchbase/core/CouchbaseTemplateTest.java | 19 +++++- .../core/CouchbaseTemplateViewListener.java | 66 ++++++++++++++++++ .../CouchbaseRepositoryViewListener.java | 68 +++++++++++++++++++ .../SimpleCouchbaseRepositoryTest.java | 33 ++++++++- .../util/BucketCreationListener.java | 20 +++++- 5 files changed, 201 insertions(+), 5 deletions(-) create mode 100644 src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateViewListener.java create mode 100644 src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryViewListener.java diff --git a/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateTest.java b/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateTest.java index ab7ce0a5..b5589300 100644 --- a/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateTest.java +++ b/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateTest.java @@ -17,6 +17,8 @@ package org.springframework.data.couchbase.core; 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.Test; import org.junit.runner.RunWith; @@ -40,7 +42,7 @@ import static org.junit.Assert.*; */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = TestApplicationConfig.class) -@TestExecutionListeners(BucketCreationListener.class) +@TestExecutionListeners({BucketCreationListener.class, CouchbaseTemplateViewListener.class}) public class CouchbaseTemplateTest { private CouchbaseClient client; @@ -177,6 +179,21 @@ public class CouchbaseTemplateTest { assertEquals(name, found.getName()); assertEquals(active, found.getActive()); } + + @Test + public void shouldLoadAndMapViewDocs() { + Query query = new Query(); + query.setStale(Stale.FALSE); + + final List 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. diff --git a/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateViewListener.java b/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateViewListener.java new file mode 100644 index 00000000..1e610afd --- /dev/null +++ b/src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateViewListener.java @@ -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); + } + +} diff --git a/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryViewListener.java b/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryViewListener.java new file mode 100644 index 00000000..3cbbf8c6 --- /dev/null +++ b/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryViewListener.java @@ -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); + } + +} diff --git a/src/test/java/org/springframework/data/couchbase/repository/SimpleCouchbaseRepositoryTest.java b/src/test/java/org/springframework/data/couchbase/repository/SimpleCouchbaseRepositoryTest.java index f20ea728..2c37852a 100644 --- a/src/test/java/org/springframework/data/couchbase/repository/SimpleCouchbaseRepositoryTest.java +++ b/src/test/java/org/springframework/data/couchbase/repository/SimpleCouchbaseRepositoryTest.java @@ -17,6 +17,8 @@ package org.springframework.data.couchbase.repository; 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.Test; import org.junit.runner.RunWith; @@ -40,7 +42,7 @@ import static org.junit.Assert.*; */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = TestApplicationConfig.class) -@TestExecutionListeners(BucketCreationListener.class) +@TestExecutionListeners({BucketCreationListener.class, CouchbaseRepositoryViewListener.class}) public class SimpleCouchbaseRepositoryTest { private CouchbaseClient client; @@ -56,17 +58,19 @@ public class SimpleCouchbaseRepositoryTest { @Autowired private String couchbasePassword; + private UserRepository repository; + @Before public void setup() throws Exception { client = new CouchbaseClient(Arrays.asList(new URI(couchbaseHost)), couchbaseBucket, couchbasePassword); template = new CouchbaseTemplate(client); + RepositoryFactorySupport factory = new CouchbaseRepositoryFactory(template); + repository = factory.getRepository(UserRepository.class); } @Test public void simpleCrud() { String key = "my_unique_user_key"; - RepositoryFactorySupport factory = new CouchbaseRepositoryFactory(template); - UserRepository repository = factory.getRepository(UserRepository.class); User instance = new User(key, "foobar"); repository.save(instance); @@ -81,4 +85,27 @@ public class SimpleCouchbaseRepositoryTest { 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 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()); + } + } diff --git a/src/test/java/org/springframework/data/couchbase/util/BucketCreationListener.java b/src/test/java/org/springframework/data/couchbase/util/BucketCreationListener.java index 590dd98a..4327eae3 100644 --- a/src/test/java/org/springframework/data/couchbase/util/BucketCreationListener.java +++ b/src/test/java/org/springframework/data/couchbase/util/BucketCreationListener.java @@ -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; import com.couchbase.client.CouchbaseClient; @@ -8,7 +24,9 @@ import org.springframework.test.context.support.DependencyInjectionTestExecution import java.net.URI; import java.util.Arrays; - +/** + * @author Michael Nitschinger + */ public class BucketCreationListener extends DependencyInjectionTestExecutionListener { private CouchbaseClient client;