|
| 1 | +/* |
| 2 | + * Copyright 2020 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 | + * https://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 |
| 13 | + * or implied. See the License for the specific language governing |
| 14 | + * permissions and limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.geode.boot.autoconfigure.data; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.io.IOException; |
| 22 | +import java.time.Duration; |
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.Set; |
| 25 | +import java.util.stream.StreamSupport; |
| 26 | + |
| 27 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 28 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 29 | + |
| 30 | +import org.junit.AfterClass; |
| 31 | +import org.junit.BeforeClass; |
| 32 | +import org.junit.Test; |
| 33 | + |
| 34 | +import org.apache.geode.cache.Region; |
| 35 | + |
| 36 | +import org.springframework.boot.ApplicationRunner; |
| 37 | +import org.springframework.boot.WebApplicationType; |
| 38 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 39 | +import org.springframework.boot.builder.SpringApplicationBuilder; |
| 40 | +import org.springframework.context.annotation.Bean; |
| 41 | +import org.springframework.context.annotation.Profile; |
| 42 | +import org.springframework.data.gemfire.GemfireTemplate; |
| 43 | +import org.springframework.data.gemfire.config.annotation.EnableEntityDefinedRegions; |
| 44 | +import org.springframework.data.gemfire.tests.integration.ForkingClientServerIntegrationTestsSupport; |
| 45 | +import org.springframework.data.gemfire.tests.process.ProcessWrapper; |
| 46 | +import org.springframework.data.gemfire.tests.util.FileSystemUtils; |
| 47 | +import org.springframework.data.gemfire.tests.util.FileUtils; |
| 48 | +import org.springframework.geode.boot.autoconfigure.DataImportExportAutoConfiguration; |
| 49 | +import org.springframework.geode.config.annotation.EnableClusterAware; |
| 50 | + |
| 51 | +import example.app.golf.model.Golfer; |
| 52 | + |
| 53 | +/** |
| 54 | + * Integration Tests for {@link DataImportExportAutoConfiguration}, which specifically tests the export of |
| 55 | + * {@link Region} values (data) to JSON on Spring Boot application (JVM) shutdown. |
| 56 | + * |
| 57 | + * @author John Blum |
| 58 | + * @see org.junit.Test |
| 59 | + * @see org.apache.geode.cache.Region |
| 60 | + * @see org.springframework.boot.SpringApplication |
| 61 | + * @see org.springframework.boot.autoconfigure.SpringBootApplication |
| 62 | + * @see org.springframework.context.annotation.Profile |
| 63 | + * @see org.springframework.geode.boot.autoconfigure.DataImportExportAutoConfiguration |
| 64 | + * @since 1.3.0 |
| 65 | + */ |
| 66 | +public class CacheDataExportAutoConfigurationIntegrationTests extends ForkingClientServerIntegrationTestsSupport { |
| 67 | + |
| 68 | + private static final File GEODE_WORKING_DIRECTORY = |
| 69 | + new File(String.format("cache-data-export-%d", System.currentTimeMillis())); |
| 70 | + |
| 71 | + private static ProcessWrapper process; |
| 72 | + |
| 73 | + private static final String DATA_GOLFERS_JSON = "data-golfers.json"; |
| 74 | + |
| 75 | + @BeforeClass |
| 76 | + public static void runGeodeProcess() throws IOException { |
| 77 | + |
| 78 | + System.setProperty(DIRECTORY_DELETE_ON_EXIT_PROPERTY, Boolean.FALSE.toString()); |
| 79 | + |
| 80 | + process = run(GEODE_WORKING_DIRECTORY, TestGeodeConfiguration.class, |
| 81 | + "-Dspring.profiles.active=EXPORT", "-Dspring.boot.data.gemfire.cache.data.export.enabled=true"); |
| 82 | + |
| 83 | + assertThat(process).isNotNull(); |
| 84 | + |
| 85 | + waitOn(() -> !process.isRunning(), Duration.ofSeconds(20).toMillis(), Duration.ofSeconds(2).toMillis()); |
| 86 | + } |
| 87 | + |
| 88 | + @AfterClass |
| 89 | + public static void cleanup() { |
| 90 | + System.clearProperty(DIRECTORY_DELETE_ON_EXIT_PROPERTY); |
| 91 | + FileSystemUtils.deleteRecursive(GEODE_WORKING_DIRECTORY); |
| 92 | + stop(process); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void exportedJsonIsCorrect() throws Exception { |
| 97 | + |
| 98 | + File dataGolferJson = new File(GEODE_WORKING_DIRECTORY, DATA_GOLFERS_JSON); |
| 99 | + |
| 100 | + assertThat(dataGolferJson).isFile(); |
| 101 | + |
| 102 | + String actualJson = FileUtils.read(dataGolferJson); |
| 103 | + |
| 104 | + /* |
| 105 | + String expectedJson = "[" |
| 106 | + + "{\"@type\":\"example.app.golf.model.Golfer\",\"handicap\":9,\"id\":1,\"name\":\"John Blum\"}," |
| 107 | + + "{\"@type\":\"example.app.golf.model.Golfer\",\"handicap\":10,\"id\":2,\"name\":\"Moe Haroon\"}" |
| 108 | + + "]"; |
| 109 | +
|
| 110 | + assertThat(actualJson).isEqualTo(expectedJson); |
| 111 | + */ |
| 112 | + |
| 113 | + Set<Golfer> expectedGolfers = mapFromJsonToGolfers(actualJson); |
| 114 | + |
| 115 | + assertThat(expectedGolfers).isNotNull(); |
| 116 | + assertThat(expectedGolfers).hasSize(2); |
| 117 | + assertContains(expectedGolfers, Golfer.newGolfer(1L, "John Blum").withHandicap(9)); |
| 118 | + assertContains(expectedGolfers, Golfer.newGolfer(2L, "Moe Haroon").withHandicap(10)); |
| 119 | + } |
| 120 | + |
| 121 | + private void assertContains(Iterable<Golfer> golfers, Golfer golfer) { |
| 122 | + |
| 123 | + assertThat(StreamSupport.stream(golfers.spliterator(), false) |
| 124 | + .anyMatch(it -> it.getId().equals(golfer.getId()) |
| 125 | + && it.getName().equals(golfer.getName()) |
| 126 | + && it.getHandicap().equals(golfer.getHandicap()))) |
| 127 | + .isTrue(); |
| 128 | + } |
| 129 | + |
| 130 | + private Set<Golfer> mapFromJsonToGolfers(String json) throws Exception { |
| 131 | + return new HashSet<>(newObjectMapper().readerForListOf(Golfer.class).readValue(json)); |
| 132 | + } |
| 133 | + |
| 134 | + private ObjectMapper newObjectMapper() { |
| 135 | + |
| 136 | + return new ObjectMapper() |
| 137 | + .configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false) |
| 138 | + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 139 | + } |
| 140 | + |
| 141 | + @Profile("EXPORT") |
| 142 | + @SpringBootApplication |
| 143 | + @EnableClusterAware |
| 144 | + @EnableEntityDefinedRegions(basePackageClasses = Golfer.class) |
| 145 | + @SuppressWarnings("unused") |
| 146 | + static class TestGeodeConfiguration { |
| 147 | + |
| 148 | + public static void main(String[] args) { |
| 149 | + |
| 150 | + new SpringApplicationBuilder(TestGeodeConfiguration.class) |
| 151 | + .web(WebApplicationType.NONE) |
| 152 | + .build() |
| 153 | + .run(args); |
| 154 | + } |
| 155 | + |
| 156 | + private static void log(String message, Object... args) { |
| 157 | + System.out.printf(String.format("%s%n", message), args); |
| 158 | + System.out.flush(); |
| 159 | + } |
| 160 | + |
| 161 | + @Bean |
| 162 | + ApplicationRunner runner(GemfireTemplate golfersTemplate) { |
| 163 | + |
| 164 | + return args -> { |
| 165 | + |
| 166 | + save(golfersTemplate, Golfer.newGolfer(1L, "John Blum").withHandicap(9)); |
| 167 | + save(golfersTemplate, Golfer.newGolfer(2L, "Moe Haroon").withHandicap(10)); |
| 168 | + |
| 169 | + log("FORE!"); |
| 170 | + }; |
| 171 | + } |
| 172 | + |
| 173 | + private Golfer save(GemfireTemplate golfersTemplate, Golfer golfer) { |
| 174 | + golfersTemplate.put(golfer.getId(), golfer); |
| 175 | + return golfer; |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments