|
| 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 | +} |
0 commit comments