Making basic inserts with mapping work.
This commit is contained in:
@@ -20,9 +20,10 @@
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.couchbase.spring.config;
|
||||
package com.couchbase.spring;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
import com.couchbase.spring.config.AbstractCouchbaseConfiguration;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
@@ -32,12 +33,13 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
@Configuration
|
||||
public class TestApplicationConfig {
|
||||
public class TestApplicationConfig extends AbstractCouchbaseConfiguration {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
@Bean
|
||||
@Override
|
||||
public CouchbaseClient couchbaseClient() throws IOException {
|
||||
String defaultHost = "http://127.0.0.1:8091/pools";
|
||||
String host = env.getProperty("couchbase.host", defaultHost);
|
||||
@@ -46,4 +48,5 @@ public class TestApplicationConfig {
|
||||
String pass = env.getProperty("couchbase.password", "");
|
||||
return new CouchbaseClient(Arrays.asList(URI.create(host)), bucket, pass);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,7 +23,7 @@
|
||||
package com.couchbase.spring.cache;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
import com.couchbase.spring.config.TestApplicationConfig;
|
||||
import com.couchbase.spring.TestApplicationConfig;
|
||||
import java.util.HashMap;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
package com.couchbase.spring.cache;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
import com.couchbase.spring.config.TestApplicationConfig;
|
||||
import com.couchbase.spring.TestApplicationConfig;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* Copyright (C) 2009-2012 Couchbase, Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.couchbase.spring.config;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
import com.couchbase.spring.TestApplicationConfig;
|
||||
import com.couchbase.spring.core.mapping.Document;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* Unit test for {@link AbstractCouchbaseConfiguration}
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = TestApplicationConfig.class)
|
||||
public class AbstractCouchbaseConfigurationTest {
|
||||
|
||||
@Autowired
|
||||
private CouchbaseClient client;
|
||||
|
||||
@Test
|
||||
public void usesConfigClassPackageAsBaseMappingPackage() throws Exception {
|
||||
AbstractCouchbaseConfiguration config = new SampleCouchbaseConfiguration();
|
||||
|
||||
assertEquals(config.getMappingBasePackage(),
|
||||
SampleCouchbaseConfiguration.class.getPackage().getName());
|
||||
assertEquals(config.getInitialEntitySet().size(), 1);
|
||||
assertTrue(config.getInitialEntitySet().contains(Entity.class));
|
||||
}
|
||||
|
||||
class SampleCouchbaseConfiguration extends AbstractCouchbaseConfiguration {
|
||||
@Bean
|
||||
@Override
|
||||
public CouchbaseClient couchbaseClient() throws Exception {
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
||||
@Document
|
||||
static class Entity {
|
||||
}
|
||||
}
|
||||
66
src/test/java/com/couchbase/spring/core/Beer.java
Normal file
66
src/test/java/com/couchbase/spring/core/Beer.java
Normal file
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* Copyright (C) 2009-2012 Couchbase, Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.couchbase.spring.core;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
/**
|
||||
* Test class for persisting and loading from {@link CouchbaseTemplate}.
|
||||
*/
|
||||
public class Beer {
|
||||
|
||||
@Id
|
||||
private final String id;
|
||||
|
||||
private String name;
|
||||
|
||||
private boolean active = true;
|
||||
|
||||
public Beer(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Beer [id=" + id + ", name=" + name + ", active=" + active + "]";
|
||||
}
|
||||
|
||||
public Beer setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Beer setActive(boolean active) {
|
||||
this.active = active;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean getActive() {
|
||||
return active;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* Copyright (C) 2009-2012 Couchbase, Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.couchbase.spring.core;
|
||||
|
||||
import com.couchbase.client.CouchbaseClient;
|
||||
import com.couchbase.spring.TestApplicationConfig;
|
||||
import net.spy.memcached.internal.GetFuture;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = TestApplicationConfig.class)
|
||||
public class CouchbaseTemplateTest {
|
||||
|
||||
@Autowired
|
||||
private CouchbaseClient client;
|
||||
|
||||
@Autowired
|
||||
private CouchbaseTemplate template;
|
||||
|
||||
@Test
|
||||
public void insertsSimpleEntityCorrectly() throws Exception {
|
||||
String id = "beers:awesome-stout";
|
||||
String name = "The Awesome Stout";
|
||||
boolean active = false;
|
||||
Beer beer = new Beer(id).setName(name).setActive(active);
|
||||
|
||||
template.insert(beer);
|
||||
Object result = (String) client.get(id);
|
||||
|
||||
String expected = "{\"active\":" + active + ",\"name\":\"" + name + "\"}";
|
||||
assertNotNull(result);
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user