Skip to content

Commit cde067c

Browse files
author
Paul Warren
committed
Add rest integration tests for h2, msql and sqlserver
1 parent 98b172e commit cde067c

File tree

10 files changed

+238
-655
lines changed

10 files changed

+238
-655
lines changed

spring-content-rest/pom.xml

+15
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,31 @@
133133
<artifactId>hibernate-entitymanager</artifactId>
134134
<scope>test</scope>
135135
</dependency>
136+
<dependency>
137+
<groupId>com.h2database</groupId>
138+
<artifactId>h2</artifactId>
139+
<scope>test</scope>
140+
</dependency>
136141
<dependency>
137142
<groupId>org.hsqldb</groupId>
138143
<artifactId>hsqldb</artifactId>
139144
<scope>test</scope>
140145
</dependency>
146+
<dependency>
147+
<groupId>mysql</groupId>
148+
<artifactId>mysql-connector-java</artifactId>
149+
<scope>test</scope>
150+
</dependency>
141151
<dependency>
142152
<groupId>org.postgresql</groupId>
143153
<artifactId>postgresql</artifactId>
144154
<scope>test</scope>
145155
</dependency>
156+
<dependency>
157+
<groupId>com.microsoft.sqlserver</groupId>
158+
<artifactId>mssql-jdbc</artifactId>
159+
<scope>test</scope>
160+
</dependency>
146161
<dependency>
147162
<groupId>org.springframework.boot</groupId>
148163
<artifactId>spring-boot-autoconfigure</artifactId>

spring-content-rest/src/test/java/internal/org/springframework/content/rest/hsql/AbstractRestIT.java

-444
This file was deleted.

spring-content-rest/src/test/java/internal/org/springframework/content/rest/hsql/JpaRestIT.java

-56
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
package internal.org.springframework.content.rest.it;
2+
3+
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.BeforeEach;
4+
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.Context;
5+
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.Describe;
6+
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.It;
7+
import static com.jayway.restassured.RestAssured.given;
8+
import static com.jayway.restassured.RestAssured.when;
9+
10+
import java.io.ByteArrayInputStream;
11+
12+
import javax.sql.DataSource;
13+
14+
import org.apache.http.HttpStatus;
15+
import org.hamcrest.Matchers;
16+
import org.junit.Test;
17+
import org.junit.runner.RunWith;
18+
import org.springframework.beans.factory.annotation.Autowired;
19+
import org.springframework.beans.factory.annotation.Value;
20+
import org.springframework.boot.test.context.SpringBootTest;
21+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
22+
import org.springframework.boot.web.server.LocalServerPort;
23+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.core.io.ClassPathResource;
27+
import org.springframework.core.io.Resource;
28+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
29+
import org.springframework.jdbc.datasource.DriverManagerDataSource;
30+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
31+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
32+
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
33+
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
34+
import org.springframework.orm.jpa.JpaTransactionManager;
35+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
36+
import org.springframework.orm.jpa.vendor.Database;
37+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
38+
import org.springframework.transaction.PlatformTransactionManager;
39+
import org.springframework.transaction.TransactionStatus;
40+
import org.springframework.transaction.annotation.EnableTransactionManagement;
41+
42+
import com.github.paulcwarren.ginkgo4j.Ginkgo4jConfiguration;
43+
import com.github.paulcwarren.ginkgo4j.Ginkgo4jSpringRunner;
44+
import com.jayway.restassured.RestAssured;
45+
46+
import internal.org.springframework.content.rest.support.TestEntity2;
47+
import internal.org.springframework.content.rest.support.TestEntity2Repository;
48+
import internal.org.springframework.content.rest.support.TestEntityChild;
49+
import net.bytebuddy.utility.RandomString;
50+
51+
public abstract class AbstractRestIT {
52+
53+
@Autowired
54+
private TestEntity2Repository claimRepo;
55+
56+
@Autowired
57+
private TestEntityChildContentRepository claimFormStore;
58+
59+
@LocalServerPort
60+
int port;
61+
62+
private TestEntity2 existingClaim;
63+
64+
{
65+
Describe("JpaRest", () -> {
66+
67+
Context("Spring Content REST", () -> {
68+
BeforeEach(() -> {
69+
RestAssured.port = port;
70+
71+
// delete any existing claim forms
72+
Iterable<TestEntity2> existingClaims = claimRepo.findAll();
73+
for (TestEntity2 existingClaim : existingClaims) {
74+
if (existingClaim.getChild() != null) {
75+
claimFormStore.unsetContent(existingClaim.getChild());
76+
}
77+
}
78+
79+
// and claims
80+
for (TestEntity2 existingClaim : existingClaims) {
81+
claimRepo.delete(existingClaim);
82+
}
83+
});
84+
Context("given a claim", () -> {
85+
BeforeEach(() -> {
86+
existingClaim = new TestEntity2();
87+
claimRepo.save(existingClaim);
88+
});
89+
It("should be POSTable with new content with 201 Created", () -> {
90+
// assert content does not exist
91+
when()
92+
.get("/files/" + existingClaim.getId() + "/child")
93+
.then()
94+
.assertThat()
95+
.statusCode(HttpStatus.SC_NOT_FOUND);
96+
97+
String newContent = "This is some new content";
98+
99+
// POST the new content
100+
given()
101+
.contentType("text/plain")
102+
.content(newContent.getBytes())
103+
.when()
104+
.post("/files/" + existingClaim.getId() + "/child")
105+
.then()
106+
.statusCode(HttpStatus.SC_CREATED);
107+
108+
// assert that it now exists
109+
given()
110+
.header("accept", "text/plain")
111+
.get("/files/" + existingClaim.getId() + "/child")
112+
.then()
113+
.statusCode(HttpStatus.SC_OK)
114+
.assertThat()
115+
.contentType(Matchers.startsWith("text/plain"))
116+
.body(Matchers.equalTo(newContent));
117+
});
118+
Context("given that claim has existing content", () -> {
119+
BeforeEach(() -> {
120+
existingClaim.setChild(new TestEntityChild());
121+
existingClaim.getChild().setMimeType("text/plain");
122+
claimFormStore.setContent(existingClaim.getChild(), new ByteArrayInputStream("This is plain text content!".getBytes()));
123+
claimRepo.save(existingClaim);
124+
});
125+
It("should return the content with 200 OK", () -> {
126+
given()
127+
.header("accept", "text/plain")
128+
.get("/files/" + existingClaim.getId() + "/child")
129+
.then()
130+
.statusCode(HttpStatus.SC_OK)
131+
.assertThat()
132+
.contentType(Matchers.startsWith("text/plain"))
133+
.body(Matchers.equalTo("This is plain text content!"));
134+
});
135+
It("should be POSTable with new content with 201 Created", () -> {
136+
String newContent = "This is new content";
137+
138+
given()
139+
.contentType("text/plain")
140+
.content(newContent.getBytes())
141+
.when()
142+
.post("/files/" + existingClaim.getId() + "/child")
143+
.then()
144+
.statusCode(HttpStatus.SC_OK);
145+
146+
given()
147+
.header("accept", "text/plain")
148+
.get("/files/" + existingClaim.getId() + "/child")
149+
.then()
150+
.statusCode(HttpStatus.SC_OK)
151+
.assertThat()
152+
.contentType(Matchers.startsWith("text/plain"))
153+
.body(Matchers.equalTo(newContent));
154+
});
155+
It("should be DELETEable with 204 No Content", () -> {
156+
given()
157+
.delete("/files/" + existingClaim.getId() + "/child")
158+
.then()
159+
.assertThat()
160+
.statusCode(HttpStatus.SC_NO_CONTENT);
161+
162+
// and make sure that it is really gone
163+
when()
164+
.get("/files/" + existingClaim.getId() + "/child")
165+
.then()
166+
.assertThat()
167+
.statusCode(HttpStatus.SC_NOT_FOUND);
168+
});
169+
});
170+
});
171+
});
172+
});
173+
}
174+
175+
protected String getId() {
176+
RandomString random = new RandomString(5);
177+
return "/store-tests/" + random.nextString();
178+
}
179+
180+
public static String getContextName(Class<?> configClass) {
181+
return configClass.getSimpleName().replaceAll("Config", "");
182+
}
183+
184+
@Test
185+
public void noop() {}
186+
}

spring-content-rest/src/test/java/internal/org/springframework/content/rest/hsql/TestEntityChildContentRepository.java spring-content-rest/src/test/java/internal/org/springframework/content/rest/it/TestEntityChildContentRepository.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package internal.org.springframework.content.rest.hsql;
1+
package internal.org.springframework.content.rest.it;
22

33
import org.springframework.content.commons.renditions.Renderable;
44
import org.springframework.content.commons.repository.ContentStore;

spring-content-rest/src/test/java/internal/org/springframework/content/rest/hsql/Application.java spring-content-rest/src/test/java/internal/org/springframework/content/rest/it/hsql/Application.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package internal.org.springframework.content.rest.hsql;
1+
package internal.org.springframework.content.rest.it.hsql;
22

33
import javax.sql.DataSource;
44

@@ -44,7 +44,7 @@ public static void main(String[] args) {
4444
@Import({RestConfiguration.class})
4545
@EnableJpaRepositories(basePackages="internal.org.springframework.content.rest.support")
4646
@EnableTransactionManagement
47-
@EnableJpaStores(basePackages="internal.org.springframework.content.rest.hsql")
47+
@EnableJpaStores(basePackages="internal.org.springframework.content.rest.it")
4848
public static class AppConfig {
4949
@Value("/org/springframework/content/jpa/schema-drop-hsqldb.sql")
5050
private ClassPathResource dropReopsitoryTables;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package internal.org.springframework.content.rest.it.hsql;
2+
3+
import org.junit.runner.RunWith;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
6+
7+
import com.github.paulcwarren.ginkgo4j.Ginkgo4jConfiguration;
8+
import com.github.paulcwarren.ginkgo4j.Ginkgo4jSpringRunner;
9+
10+
import internal.org.springframework.content.rest.it.AbstractRestIT;
11+
12+
@RunWith(Ginkgo4jSpringRunner.class)
13+
@Ginkgo4jConfiguration(threads=1)
14+
@SpringBootTest(classes = Application.class, webEnvironment=WebEnvironment.RANDOM_PORT)
15+
public class HSQLRestIT extends AbstractRestIT {
16+
}

spring-content-rest/src/test/java/internal/org/springframework/content/rest/jpa/Application.java spring-content-rest/src/test/java/internal/org/springframework/content/rest/it/pg/Application.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package internal.org.springframework.content.rest.jpa;
1+
package internal.org.springframework.content.rest.it.pg;
22

33
import javax.sql.DataSource;
44

@@ -42,7 +42,7 @@ public static void main(String[] args) {
4242
@Import(RestConfiguration.class)
4343
@EnableJpaRepositories(basePackages="internal.org.springframework.content.rest.support")
4444
@EnableTransactionManagement
45-
@EnableJpaStores(basePackages="internal.org.springframework.content.rest.hsql")
45+
@EnableJpaStores(basePackages="internal.org.springframework.content.rest.it")
4646
public static class AppConfig {
4747

4848
@Value("/org/springframework/content/jpa/schema-drop-postgresql.sql")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package internal.org.springframework.content.rest.it.pg;
2+
3+
import org.junit.runner.RunWith;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
6+
7+
import com.github.paulcwarren.ginkgo4j.Ginkgo4jConfiguration;
8+
import com.github.paulcwarren.ginkgo4j.Ginkgo4jSpringRunner;
9+
10+
import internal.org.springframework.content.rest.it.AbstractRestIT;
11+
12+
@RunWith(Ginkgo4jSpringRunner.class)
13+
@Ginkgo4jConfiguration(threads=1)
14+
@SpringBootTest(classes = Application.class, webEnvironment=WebEnvironment.RANDOM_PORT)
15+
public class PostgresRestIT extends AbstractRestIT {
16+
}

0 commit comments

Comments
 (0)