Skip to content

Commit eec5247

Browse files
committed
DATAREST-93 - More cleanups, more fixes.
1 parent 7a11514 commit eec5247

File tree

11 files changed

+187
-21
lines changed

11 files changed

+187
-21
lines changed

spring-data-rest-example/src/main/java/org/springframework/data/rest/example/mongodb/MongoDbRepositoryConfig.java

+11-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.net.UnknownHostException;
44

5-
import com.mongodb.Mongo;
65
import org.springframework.context.annotation.Bean;
76
import org.springframework.context.annotation.ComponentScan;
87
import org.springframework.context.annotation.Configuration;
@@ -11,20 +10,23 @@
1110
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
1211
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
1312

13+
import com.mongodb.Mongo;
14+
1415
/**
1516
* @author Jon Brisbin
1617
*/
1718
@Configuration
18-
@ComponentScan(basePackageClasses = MongoDbRepositoryConfig.class)
19+
@ComponentScan
1920
@EnableMongoRepositories
2021
public class MongoDbRepositoryConfig {
2122

22-
@Bean public MongoDbFactory mongoDbFactory() throws UnknownHostException {
23-
return new SimpleMongoDbFactory(new Mongo("localhost"), "spring-data-rest-example");
24-
}
25-
26-
@Bean public MongoTemplate mongoTemplate() throws UnknownHostException {
27-
return new MongoTemplate(mongoDbFactory());
28-
}
23+
@Bean
24+
public MongoDbFactory mongoDbFactory() throws UnknownHostException {
25+
return new SimpleMongoDbFactory(new Mongo("localhost"), "spring-data-rest-example");
26+
}
2927

28+
@Bean
29+
public MongoTemplate mongoTemplate() throws UnknownHostException {
30+
return new MongoTemplate(mongoDbFactory());
31+
}
3032
}

spring-data-rest-repository/src/main/java/org/springframework/data/rest/repository/mapping/ResourceMappingFactory.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ public String getRel() {
129129
*/
130130
@Override
131131
public String getSingleResourceRel() {
132-
return null;
132+
133+
String rel = getRel();
134+
return rel == null ? null : String.format("%s.%s", rel, rel);
133135
}
134136

135137
/*

spring-data-rest-repository/src/test/java/org/springframework/data/rest/repository/domain/jpa/AnnotatedPersonRepository.java

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.springframework.data.rest.repository.domain.jpa;
22

33
import org.springframework.data.repository.CrudRepository;
4+
import org.springframework.data.repository.NoRepositoryBean;
45
import org.springframework.data.rest.repository.annotation.RestResource;
56

67
/**
@@ -9,5 +10,6 @@
910
* @author Jon Brisbin
1011
*/
1112
@RestResource(rel = "people", exported = false)
13+
@NoRepositoryBean
1214
public interface AnnotatedPersonRepository extends CrudRepository<Person, Long> {
1315
}

spring-data-rest-repository/src/test/java/org/springframework/data/rest/repository/domain/jpa/PersonLoader.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
public class PersonLoader implements InitializingBean {
1212

1313
@Autowired
14-
private AnnotatedPersonRepository people;
14+
PersonRepository people;
1515

1616
@Override public void afterPropertiesSet() throws Exception {
1717
people.save(new Person("John", "Doe"));
1818
}
19-
2019
}

spring-data-rest-repository/src/test/java/org/springframework/data/rest/repository/mapping/ResourceMappingsIntegrationTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ public void setUp() {
5555
public void foo() {
5656

5757
assertThat(mappings, is(Matchers.<ResourceMetadata> iterableWithSize(2)));
58-
assertThat(mappings.getMappingFor(Person.class).isExported(), is(false));
58+
assertThat(mappings.getMappingFor(Person.class).isExported(), is(true));
5959
}
6060
}

spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AbstractWebIntegrationTests.java

+62-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
2121
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
2222

23+
import java.util.List;
24+
2325
import org.junit.Before;
26+
import org.junit.Test;
2427
import org.junit.runner.RunWith;
2528
import org.springframework.beans.factory.annotation.Autowired;
2629
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
@@ -33,16 +36,17 @@
3336
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3437
import org.springframework.test.context.web.WebAppConfiguration;
3538
import org.springframework.test.web.servlet.MockMvc;
39+
import org.springframework.test.web.servlet.MvcResult;
40+
import org.springframework.test.web.servlet.ResultActions;
41+
import org.springframework.test.web.servlet.ResultMatcher;
3642
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
37-
import org.springframework.transaction.annotation.Transactional;
3843
import org.springframework.web.context.WebApplicationContext;
3944

4045
/**
4146
* @author Oliver Gierke
4247
*/
4348
@RunWith(SpringJUnit4ClassRunner.class)
4449
@WebAppConfiguration
45-
@Transactional
4650
@ContextConfiguration(classes = RepositoryRestMvcConfiguration.class)
4751
public abstract class AbstractWebIntegrationTests {
4852

@@ -70,6 +74,39 @@ protected MockHttpServletResponse request(Link link) throws Exception {
7074
protected MockHttpServletResponse request(String href) throws Exception {
7175
return request(href, MediaType.APPLICATION_JSON);
7276
}
77+
78+
protected ResultActions follow(Link link) throws Exception {
79+
return mvc.perform(get(link.getHref()));
80+
}
81+
82+
protected List<Link> discover(String rel) throws Exception {
83+
return discover(new Link("/"), rel);
84+
}
85+
86+
protected Link discoverUnique(String rel) throws Exception {
87+
88+
List<Link> discover = discover(rel);
89+
assertThat(discover, hasSize(1));
90+
return discover.get(0);
91+
}
92+
93+
protected List<Link> discover(Link root, String rel) throws Exception {
94+
String s = mvc
95+
.perform(get(root.getHref()))
96+
.andExpect(status().isOk())
97+
.andExpect(hasLinkWithRel(rel))
98+
.andReturn().getResponse().getContentAsString();
99+
return links.findLinksWithRel(rel, s);
100+
}
101+
102+
protected Link discoverUnique(Link root, String rel) throws Exception {
103+
String s = mvc
104+
.perform(get(root.getHref()))
105+
.andExpect(status().isOk())
106+
.andExpect(hasLinkWithRel(rel))
107+
.andReturn().getResponse().getContentAsString();
108+
return links.findLinkWithRel(rel, s);
109+
}
73110

74111
protected Link assertHasLinkWithRel(String rel, MockHttpServletResponse response) throws Exception {
75112

@@ -89,4 +126,27 @@ protected void assertDoesNotHaveLinkWithRel(String rel, MockHttpServletResponse
89126

90127
assertThat("Expected not to find link with rel " + rel + " but found " + link + "!", link, is(nullValue()));
91128
}
129+
130+
protected ResultMatcher hasLinkWithRel(final String rel) {
131+
132+
return new ResultMatcher() {
133+
@Override
134+
public void match(MvcResult result) throws Exception {
135+
String s = result.getResponse().getContentAsString();
136+
assertThat(links.findLinkWithRel(rel, s), notNullValue());
137+
}
138+
};
139+
}
140+
141+
@Test
142+
public void exposesRootResource() throws Exception {
143+
144+
ResultActions actions = mvc.perform(get("/")).andExpect(status().isOk());
145+
146+
for (String rel : expectedRootLinkRels()) {
147+
actions.andExpect(hasLinkWithRel(rel));
148+
}
149+
}
150+
151+
protected abstract Iterable<String> expectedRootLinkRels();
92152
}

spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java

+13
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,32 @@
1515
*/
1616
package org.springframework.data.rest.webmvc.jpa;
1717

18+
import java.util.Arrays;
19+
1820
import org.junit.Test;
1921
import org.springframework.data.rest.webmvc.AbstractWebIntegrationTests;
2022
import org.springframework.hateoas.Link;
2123
import org.springframework.mock.web.MockHttpServletResponse;
2224
import org.springframework.test.context.ContextConfiguration;
25+
import org.springframework.transaction.annotation.Transactional;
2326

2427
/**
2528
* Web integration tests specific to JPA.
2629
*
2730
* @author Oliver Gierke
2831
*/
2932
@ContextConfiguration(classes = JpaRepositoryConfig.class)
33+
@Transactional
3034
public class JpaWebTests extends AbstractWebIntegrationTests {
35+
36+
/*
37+
* (non-Javadoc)
38+
* @see org.springframework.data.rest.webmvc.AbstractWebIntegrationTests#expectedRootLinkRels()
39+
*/
40+
@Override
41+
protected Iterable<String> expectedRootLinkRels() {
42+
return Arrays.asList("people");
43+
}
3144

3245
@Test
3346
public void accessPersons() throws Exception {

spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntitySerializationTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ public void serializesPersonEntity() throws IOException, InterruptedException {
6868

6969
String s = writer.toString();
7070

71-
Link fatherLink = linkDiscoverer.findLinkWithRel("father", s);
71+
Link fatherLink = linkDiscoverer.findLinkWithRel("people.people.father", s);
7272
assertThat(fatherLink.getHref(), endsWith(new UriTemplate("/{id}/father").expand(person.getId()).toString()));
7373

74-
Link siblingLink = linkDiscoverer.findLinkWithRel("siblings", s);
74+
Link siblingLink = linkDiscoverer.findLinkWithRel("people.people.siblings", s);
7575
assertThat(siblingLink.getHref(), endsWith(new UriTemplate("/{id}/siblings").expand(person.getId()).toString()));
7676
}
7777
}

spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/MongoDbRepositoryConfig.java

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2012-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.springframework.data.rest.webmvc.mongodb;
217

318
import java.net.UnknownHostException;
@@ -15,7 +30,7 @@
1530
* @author Jon Brisbin
1631
*/
1732
@Configuration
18-
@ComponentScan(basePackageClasses = MongoDbRepositoryConfig.class)
33+
@ComponentScan
1934
@EnableMongoRepositories
2035
public class MongoDbRepositoryConfig {
2136

@@ -26,5 +41,4 @@ public class MongoDbRepositoryConfig {
2641
@Bean public MongoTemplate mongoTemplate() throws UnknownHostException {
2742
return new MongoTemplate(mongoDbFactory());
2843
}
29-
3044
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.rest.webmvc.mongodb;
17+
18+
import static org.hamcrest.Matchers.*;
19+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
20+
21+
import java.util.Arrays;
22+
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.data.rest.webmvc.AbstractWebIntegrationTests;
28+
import org.springframework.hateoas.Link;
29+
import org.springframework.test.context.ContextConfiguration;
30+
31+
/**
32+
*
33+
* @author Oliver Gierke
34+
*/
35+
@ContextConfiguration(classes = MongoDbRepositoryConfig.class)
36+
public class MongoWebTests extends AbstractWebIntegrationTests {
37+
38+
@Autowired ProfileRepository repository;
39+
40+
@Before
41+
public void populateProfiles() {
42+
43+
Profile twitter = new Profile();
44+
twitter.setPerson(1L);
45+
twitter.setType("Twitter");
46+
47+
Profile linkedIn = new Profile();
48+
linkedIn.setPerson(1L);
49+
linkedIn.setType("LinkedIn");
50+
51+
repository.save(Arrays.asList(twitter, linkedIn));
52+
}
53+
54+
@After
55+
public void cleanUp() {
56+
repository.deleteAll();
57+
}
58+
59+
/*
60+
* (non-Javadoc)
61+
* @see org.springframework.data.rest.webmvc.AbstractWebIntegrationTests#expectedRootLinkRels()
62+
*/
63+
@Override
64+
protected Iterable<String> expectedRootLinkRels() {
65+
return Arrays.asList("profile");
66+
}
67+
68+
@Test
69+
public void foo() throws Exception {
70+
71+
Link profileLink = discoverUnique("profile");
72+
follow(profileLink).andExpect(jsonPath("$.content").value(hasSize(2)));
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package org.springframework.data.rest.webmvc.mongodb;
22

3-
import org.springframework.data.repository.CrudRepository;
3+
import org.springframework.data.repository.PagingAndSortingRepository;
44

55
/**
66
* @author Jon Brisbin
77
*/
8-
public interface ProfileRepository extends CrudRepository<Profile, String> {
8+
public interface ProfileRepository extends PagingAndSortingRepository<Profile, String> {
99
}

0 commit comments

Comments
 (0)