|
| 1 | +package internal.org.springframework.content.rest.links; |
| 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 | + |
| 7 | +import java.io.ByteArrayInputStream; |
| 8 | +import java.io.File; |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.InputStream; |
| 11 | + |
| 12 | +import org.apache.commons.io.IOUtils; |
| 13 | +import org.junit.Test; |
| 14 | +import org.junit.runner.RunWith; |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; |
| 16 | +import org.springframework.content.commons.renditions.RenditionProvider; |
| 17 | +import org.springframework.content.fs.config.EnableFilesystemStores; |
| 18 | +import org.springframework.content.fs.io.FileSystemResourceLoader; |
| 19 | +import org.springframework.content.rest.config.HypermediaConfiguration; |
| 20 | +import org.springframework.content.rest.config.RestConfiguration; |
| 21 | +import org.springframework.context.annotation.Bean; |
| 22 | +import org.springframework.context.annotation.Configuration; |
| 23 | +import org.springframework.context.annotation.Profile; |
| 24 | +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
| 25 | +import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration; |
| 26 | +import org.springframework.test.context.ActiveProfiles; |
| 27 | +import org.springframework.test.context.ContextConfiguration; |
| 28 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 29 | +import org.springframework.test.web.servlet.MockMvc; |
| 30 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 31 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 32 | +import org.springframework.transaction.annotation.Transactional; |
| 33 | +import org.springframework.web.context.WebApplicationContext; |
| 34 | +import org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration; |
| 35 | + |
| 36 | +import com.github.paulcwarren.ginkgo4j.Ginkgo4jConfiguration; |
| 37 | +import com.github.paulcwarren.ginkgo4j.Ginkgo4jSpringRunner; |
| 38 | + |
| 39 | +import internal.org.springframework.content.rest.support.TestEntity3; |
| 40 | +import internal.org.springframework.content.rest.support.TestEntity3ContentRepository; |
| 41 | +import internal.org.springframework.content.rest.support.TestEntity3Repository; |
| 42 | +import internal.org.springframework.content.rest.support.config.JpaInfrastructureConfig; |
| 43 | + |
| 44 | +@RunWith(Ginkgo4jSpringRunner.class) |
| 45 | +@Ginkgo4jConfiguration(threads = 1) |
| 46 | +@WebAppConfiguration |
| 47 | +@ContextConfiguration(classes = { |
| 48 | + ContextPathContentLinksIntegrationTest.ContextPathConfig.class, |
| 49 | + DelegatingWebMvcConfiguration.class, |
| 50 | + RepositoryRestMvcConfiguration.class, |
| 51 | + RestConfiguration.class, |
| 52 | + HypermediaConfiguration.class }) |
| 53 | +@Transactional |
| 54 | +@ActiveProfiles("store") |
| 55 | +public class ContextPathContentLinksIntegrationTest { |
| 56 | + |
| 57 | + @Autowired |
| 58 | + TestEntity3Repository repository3; |
| 59 | + @Autowired |
| 60 | + TestEntity3ContentRepository contentRepository3; |
| 61 | + |
| 62 | + @Autowired |
| 63 | + private WebApplicationContext context; |
| 64 | + |
| 65 | + private MockMvc mvc; |
| 66 | + |
| 67 | + private TestEntity3 testEntity3; |
| 68 | + |
| 69 | + private EntityContentLinkTests entityContentLinkTests; |
| 70 | + |
| 71 | + { |
| 72 | + Describe("given the spring content baseUri property is set to contentApi", () -> { |
| 73 | + BeforeEach(() -> { |
| 74 | + mvc = MockMvcBuilders.webAppContextSetup(context).build(); |
| 75 | + }); |
| 76 | + |
| 77 | + Context("given an Entity and a Store with a default store path", () -> { |
| 78 | + BeforeEach(() -> { |
| 79 | + testEntity3 = repository3.save(new TestEntity3()); |
| 80 | + |
| 81 | + entityContentLinkTests.setMvc(mvc); |
| 82 | + entityContentLinkTests.setRepository(repository3); |
| 83 | + entityContentLinkTests.setStore(contentRepository3); |
| 84 | + entityContentLinkTests.setTestEntity(testEntity3); |
| 85 | + entityContentLinkTests.setUrl("/contextPath/testEntity3s/" + testEntity3.getId()); |
| 86 | + entityContentLinkTests.setContextPath("/contextPath"); |
| 87 | + entityContentLinkTests.setLinkRel("testEntity3s"); |
| 88 | + entityContentLinkTests.setExpectedLinkRegex("http://localhost/contextPath/testEntity3s/" + testEntity3.getId()); |
| 89 | + }); |
| 90 | + entityContentLinkTests = new EntityContentLinkTests(); |
| 91 | + }); |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + @Configuration |
| 96 | + @EnableJpaRepositories(basePackages = "internal.org.springframework.content.rest.support") |
| 97 | + @EnableTransactionManagement |
| 98 | + @EnableFilesystemStores(basePackages = "internal.org.springframework.content.rest.support") |
| 99 | + @Profile("store") |
| 100 | + public static class ContextPathConfig extends JpaInfrastructureConfig { |
| 101 | + |
| 102 | + @Bean |
| 103 | + FileSystemResourceLoader fileSystemResourceLoader() { |
| 104 | + return new FileSystemResourceLoader(filesystemRoot().getAbsolutePath()); |
| 105 | + } |
| 106 | + |
| 107 | + @Bean |
| 108 | + public File filesystemRoot() { |
| 109 | + File baseDir = new File(System.getProperty("java.io.tmpdir")); |
| 110 | + File filesystemRoot = new File(baseDir, "spring-content-controller-tests"); |
| 111 | + filesystemRoot.mkdirs(); |
| 112 | + return filesystemRoot; |
| 113 | + } |
| 114 | + |
| 115 | + @Bean |
| 116 | + public RenditionProvider textToHtml() { |
| 117 | + return new RenditionProvider() { |
| 118 | + |
| 119 | + @Override |
| 120 | + public String consumes() { |
| 121 | + return "text/plain"; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public String[] produces() { |
| 126 | + return new String[] { "text/html" }; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public InputStream convert(InputStream fromInputSource, String toMimeType) { |
| 131 | + String input = null; |
| 132 | + try { |
| 133 | + input = IOUtils.toString(fromInputSource); |
| 134 | + } |
| 135 | + catch (IOException e) { |
| 136 | + } |
| 137 | + return new ByteArrayInputStream( |
| 138 | + String.format("<html><body>%s</body></html>", input).getBytes()); |
| 139 | + } |
| 140 | + }; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void noop() {} |
| 146 | +} |
0 commit comments