|
6 | 6 | package org.tailormap.api.geotools.featuresources; |
7 | 7 |
|
8 | 8 | import static org.hamcrest.MatcherAssert.assertThat; |
9 | | -import static org.hamcrest.Matchers.containsStringIgnoringCase; |
| 9 | +import static org.hamcrest.Matchers.is; |
| 10 | +import static org.hamcrest.Matchers.lessThanOrEqualTo; |
10 | 11 | import static org.junit.jupiter.api.Assertions.assertEquals; |
11 | | -import static org.junit.jupiter.api.Assertions.assertThrows; |
12 | 12 | import static org.junit.jupiter.api.Assertions.fail; |
| 13 | +import static org.junit.jupiter.api.Assumptions.assumeFalse; |
| 14 | +import static org.junit.jupiter.params.provider.Arguments.arguments; |
13 | 15 |
|
| 16 | +import java.io.IOException; |
14 | 17 | import java.sql.Connection; |
15 | 18 | import java.sql.ResultSet; |
| 19 | +import java.sql.SQLException; |
16 | 20 | import java.sql.Statement; |
17 | 21 | import java.util.Objects; |
18 | 22 | import java.util.stream.Stream; |
19 | 23 | import org.geotools.jdbc.JDBCDataStore; |
| 24 | +import org.junit.jupiter.api.AfterEach; |
| 25 | +import org.junit.jupiter.api.BeforeEach; |
| 26 | +import org.junit.jupiter.api.DisplayName; |
| 27 | +import org.junit.jupiter.api.MethodOrderer; |
| 28 | +import org.junit.jupiter.api.Order; |
20 | 29 | import org.junit.jupiter.api.Test; |
21 | | -import org.junit.jupiter.params.ParameterizedTest; |
| 30 | +import org.junit.jupiter.api.TestMethodOrder; |
| 31 | +import org.junit.jupiter.params.Parameter; |
| 32 | +import org.junit.jupiter.params.ParameterizedClass; |
22 | 33 | import org.junit.jupiter.params.provider.Arguments; |
23 | 34 | import org.junit.jupiter.params.provider.MethodSource; |
24 | | -import org.junit.jupiter.params.provider.NullAndEmptySource; |
25 | 35 | import org.springframework.beans.factory.annotation.Autowired; |
26 | 36 | import org.tailormap.api.annotation.PostgresIntegrationTest; |
27 | 37 | import org.tailormap.api.persistence.TMFeatureType; |
28 | 38 | import org.tailormap.api.persistence.json.AttachmentAttributeType; |
29 | 39 | import org.tailormap.api.repository.FeatureSourceRepository; |
30 | 40 | import org.tailormap.api.repository.FeatureTypeRepository; |
31 | 41 |
|
| 42 | +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
32 | 43 | @PostgresIntegrationTest |
| 44 | +@ParameterizedClass |
| 45 | +@DisplayName("AttachmentsHelper integration test") |
| 46 | +@MethodSource("titlesAndNamesForFeatureSourcesAndFeatureTypes") |
33 | 47 | class AttachmentsHelperIntegrationTest { |
| 48 | + |
34 | 49 | @Autowired |
35 | 50 | private FeatureSourceRepository featureSourceRepository; |
36 | 51 |
|
37 | 52 | @Autowired |
38 | 53 | private FeatureTypeRepository featureTypeRepository; |
39 | 54 |
|
40 | | - private static Stream<Arguments> titlesAndNamesForFeatureSourcesAndFeatureTypes() { |
| 55 | + @Parameter(0) |
| 56 | + private String featureSourceTitle; |
| 57 | + |
| 58 | + @Parameter(1) |
| 59 | + private String featureTypeName; |
| 60 | + |
| 61 | + private TMFeatureType featureType; |
| 62 | + private JDBCDataStore ds; |
| 63 | + |
| 64 | + static Stream<Arguments> titlesAndNamesForFeatureSourcesAndFeatureTypes() { |
41 | 65 | return Stream.of( |
42 | | - Arguments.of("PostGIS", "bord"), |
43 | | - Arguments.of("MS SQL Server", "bord"), |
44 | | - Arguments.of("Oracle", "BORD"), |
45 | | - Arguments.of("PostGIS OSM", "osm_polygon")); |
| 66 | + arguments("PostGIS", "bord"), |
| 67 | + arguments("PostGIS", "pk_variation_bigint"), |
| 68 | + arguments("PostGIS", "pk_variation_decimal"), |
| 69 | + arguments("PostGIS", "pk_variation_integer"), |
| 70 | + arguments("PostGIS", "pk_variation_numeric"), |
| 71 | + arguments("PostGIS", "pk_variation_serial"), |
| 72 | + arguments("PostGIS", "pk_variation_uuid"), |
| 73 | + arguments("MS SQL Server", "bord"), |
| 74 | + arguments("MS SQL Server", "pk_variation_bigint"), |
| 75 | + arguments("MS SQL Server", "pk_variation_decimal"), |
| 76 | + arguments("MS SQL Server", "pk_variation_integer"), |
| 77 | + arguments("MS SQL Server", "pk_variation_numeric"), |
| 78 | + arguments("MS SQL Server", "pk_variation_serial"), |
| 79 | + arguments("MS SQL Server", "pk_variation_uuid"), |
| 80 | + arguments("Oracle", "BORD"), |
| 81 | + arguments("Oracle", "PK_VARIATION_BIGINT"), |
| 82 | + arguments("Oracle", "PK_VARIATION_DECIMAL"), |
| 83 | + arguments("Oracle", "PK_VARIATION_INTEGER"), |
| 84 | + arguments("Oracle", "PK_VARIATION_NUMERIC"), |
| 85 | + arguments("Oracle", "PK_VARIATION_SERIAL"), |
| 86 | + arguments("Oracle", "PK_VARIATION_UUID"), |
| 87 | + arguments("PostGIS OSM", "osm_polygon")); |
46 | 88 | } |
47 | 89 |
|
48 | | - @ParameterizedTest |
49 | | - @MethodSource("titlesAndNamesForFeatureSourcesAndFeatureTypes") |
50 | | - void testGetCreateAttachmentsForFeatureTypeStatements(String fsTitle, String ftName) { |
51 | | - TMFeatureType featureType = featureTypeRepository |
| 90 | + @BeforeEach |
| 91 | + void setUp() throws IOException { |
| 92 | + featureType = featureTypeRepository |
52 | 93 | .getTMFeatureTypeByNameAndFeatureSource( |
53 | | - ftName, featureSourceRepository.getByTitle(fsTitle).orElseThrow()) |
| 94 | + featureTypeName, |
| 95 | + featureSourceRepository.getByTitle(featureSourceTitle).orElseThrow()) |
54 | 96 | .orElseThrow(); |
55 | 97 |
|
56 | 98 | featureType |
57 | 99 | .getSettings() |
58 | 100 | .addAttachmentAttributesItem(new AttachmentAttributeType() |
59 | | - .attributeName("bord_photos") |
| 101 | + .attributeName(featureTypeName + "_photos") |
60 | 102 | .maxAttachmentSize(4_000_000L) |
61 | 103 | .mimeType("image/jpeg")); |
62 | 104 |
|
63 | | - JDBCDataStore ds = null; |
| 105 | + ds = (JDBCDataStore) new JDBCFeatureSourceHelper().createDataStore(featureType.getFeatureSource()); |
| 106 | + if (Objects.equals(featureTypeName, "osm_polygon")) |
| 107 | + featureTypeName = ds.getDatabaseSchema() + "." + featureTypeName; |
| 108 | + } |
| 109 | + |
| 110 | + @AfterEach |
| 111 | + void tearDown() throws SQLException { |
| 112 | + featureType.getSettings().getAttachmentAttributes().clear(); |
| 113 | + try (Connection conn = ds.getDataSource().getConnection(); |
| 114 | + Statement stmt = conn.createStatement()) { |
| 115 | + boolean success = stmt.execute("drop table " + featureTypeName + "_attachments"); |
| 116 | + assumeFalse(success, "Maybe failed to cleanup attachments table, we did not get an update count result."); |
| 117 | + assertThat(stmt.getUpdateCount(), is(lessThanOrEqualTo(1))); |
| 118 | + } finally { |
| 119 | + if (ds != null) { |
| 120 | + ds.dispose(); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @Order(1) |
| 126 | + @Test |
| 127 | + @DisplayName("Create attachments table for feature type.") |
| 128 | + void createAttachmentsTableForFeatureTypeStatements() { |
64 | 129 | try { |
65 | 130 | AttachmentsHelper.createAttachmentTableForFeatureType(featureType); |
66 | | - ds = (JDBCDataStore) new JDBCFeatureSourceHelper().createDataStore(featureType.getFeatureSource()); |
67 | 131 |
|
68 | | - if (Objects.equals(ftName, "osm_polygon")) ftName = ds.getDatabaseSchema() + "." + ftName; |
69 | 132 | try (Connection conn = ds.getDataSource().getConnection(); |
70 | 133 | Statement stmt = conn.createStatement(); |
71 | | - ResultSet rs = stmt.executeQuery("select count(*) from " + ftName + "_attachments")) { |
| 134 | + ResultSet rs = stmt.executeQuery("select count(*) from " + featureTypeName + "_attachments")) { |
72 | 135 | if (rs.next()) { |
73 | 136 | int count = rs.getInt(1); |
74 | 137 | assertEquals(0, count, "Attachments table exists but is not empty."); |
75 | 138 | } else { |
76 | | - fail("Attachments table does not exist."); |
| 139 | + fail("Attachments table '%s_attachments' does not exist.".formatted(featureTypeName)); |
77 | 140 | } |
78 | | - // cleanup created attachments table |
79 | | - stmt.execute("drop table " + ftName + "_attachments"); |
80 | 141 | } |
81 | 142 | } catch (Exception e) { |
82 | 143 | fail(e.getMessage()); |
83 | | - } finally { |
84 | | - if (ds != null) { |
85 | | - ds.dispose(); |
86 | | - } |
87 | 144 | } |
88 | 145 | } |
89 | | - |
90 | | - @ParameterizedTest |
91 | | - @NullAndEmptySource |
92 | | - void testInvalidAttachmentAttributes(String invalidInput) throws Exception { |
93 | | - TMFeatureType featureType = featureTypeRepository |
94 | | - .getTMFeatureTypeByNameAndFeatureSource( |
95 | | - "bak", featureSourceRepository.getByTitle("PostGIS").orElseThrow()) |
96 | | - .orElseThrow(); |
97 | | - |
98 | | - featureType |
99 | | - .getSettings() |
100 | | - .addAttachmentAttributesItem(new AttachmentAttributeType() |
101 | | - .attributeName(invalidInput) // Invalid attribute name |
102 | | - .maxAttachmentSize(4_000_000L) |
103 | | - .mimeType("image/jpeg")); |
104 | | - |
105 | | - Exception exception = assertThrows(IllegalArgumentException.class, () -> { |
106 | | - AttachmentsHelper.createAttachmentTableForFeatureType(featureType); |
107 | | - }); |
108 | | - |
109 | | - assertThat( |
110 | | - exception.getMessage(), |
111 | | - containsStringIgnoringCase( |
112 | | - "FeatureType bak has an attachment attribute with invalid (null or empty) attribute name")); |
113 | | - } |
114 | | - |
115 | | - @Test |
116 | | - void testNullFeatureType() { |
117 | | - Exception exception = assertThrows(IllegalArgumentException.class, () -> { |
118 | | - AttachmentsHelper.createAttachmentTableForFeatureType(null); |
119 | | - }); |
120 | | - |
121 | | - assertThat( |
122 | | - exception.getMessage(), |
123 | | - containsStringIgnoringCase( |
124 | | - "FeatureType null is invalid or has no attachment attributes defined in its settings")); |
125 | | - } |
126 | 146 | } |
0 commit comments