|
| 1 | +/* |
| 2 | + * Copyright 2017-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 or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.jdbc.repository; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import junit.framework.AssertionFailedError; |
| 21 | +import lombok.Data; |
| 22 | +import lombok.RequiredArgsConstructor; |
| 23 | + |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.HashSet; |
| 26 | +import java.util.Set; |
| 27 | + |
| 28 | +import org.junit.ClassRule; |
| 29 | +import org.junit.Rule; |
| 30 | +import org.junit.Test; |
| 31 | +import org.springframework.beans.factory.annotation.Autowired; |
| 32 | +import org.springframework.context.annotation.Bean; |
| 33 | +import org.springframework.context.annotation.Configuration; |
| 34 | +import org.springframework.context.annotation.Import; |
| 35 | +import org.springframework.data.annotation.Id; |
| 36 | +import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory; |
| 37 | +import org.springframework.data.jdbc.testing.DatabaseProfileValueSource; |
| 38 | +import org.springframework.data.jdbc.testing.TestConfiguration; |
| 39 | +import org.springframework.data.repository.CrudRepository; |
| 40 | +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; |
| 41 | +import org.springframework.test.annotation.IfProfileValue; |
| 42 | +import org.springframework.test.annotation.ProfileValueSourceConfiguration; |
| 43 | +import org.springframework.test.context.ContextConfiguration; |
| 44 | +import org.springframework.test.context.junit4.rules.SpringClassRule; |
| 45 | +import org.springframework.test.context.junit4.rules.SpringMethodRule; |
| 46 | +import org.springframework.transaction.annotation.Transactional; |
| 47 | + |
| 48 | +/** |
| 49 | + * Very simple use cases for creation and usage of JdbcRepositories. |
| 50 | + * |
| 51 | + * @author Jens Schauder |
| 52 | + * @author Thomas Lang |
| 53 | + */ |
| 54 | +@ContextConfiguration |
| 55 | +@ProfileValueSourceConfiguration(DatabaseProfileValueSource.class) |
| 56 | +@Transactional |
| 57 | +public class JdbcRepositoryWithCollectionsAndIDOnlyEntityIntegrationTests { |
| 58 | + |
| 59 | + @Configuration |
| 60 | + @Import(TestConfiguration.class) |
| 61 | + static class Config { |
| 62 | + |
| 63 | + @Autowired JdbcRepositoryFactory factory; |
| 64 | + |
| 65 | + @Bean |
| 66 | + Class<?> testClass() { |
| 67 | + return JdbcRepositoryWithCollectionsAndIDOnlyEntityIntegrationTests.class; |
| 68 | + } |
| 69 | + |
| 70 | + @Bean |
| 71 | + DummyEntityRepository dummyEntityRepository() { |
| 72 | + return factory.getRepository(DummyEntityRepository.class); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @ClassRule public static final SpringClassRule classRule = new SpringClassRule(); |
| 77 | + @Rule public SpringMethodRule methodRule = new SpringMethodRule(); |
| 78 | + |
| 79 | + @Autowired NamedParameterJdbcTemplate template; |
| 80 | + @Autowired DummyEntityRepository repository; |
| 81 | + |
| 82 | + @Test // DATAJDBC-557 |
| 83 | + public void saveAndLoadEmptySet() { |
| 84 | + |
| 85 | + DummyEntity entity = repository.save(createDummyEntity()); |
| 86 | + |
| 87 | + assertThat(entity.id).isNotNull(); |
| 88 | + |
| 89 | + DummyEntity reloaded = repository.findById(entity.id).orElseThrow(AssertionFailedError::new); |
| 90 | + |
| 91 | + assertThat(reloaded.content) // |
| 92 | + .isNotNull() // |
| 93 | + .isEmpty(); |
| 94 | + } |
| 95 | + |
| 96 | + @Test // DATAJDBC-557 |
| 97 | + public void saveAndLoadNonEmptySet() { |
| 98 | + |
| 99 | + Element element1 = new Element(); |
| 100 | + Element element2 = new Element(); |
| 101 | + |
| 102 | + DummyEntity entity = createDummyEntity(); |
| 103 | + entity.content.add(element1); |
| 104 | + entity.content.add(element2); |
| 105 | + |
| 106 | + entity = repository.save(entity); |
| 107 | + |
| 108 | + assertThat(entity.id).isNotNull(); |
| 109 | + assertThat(entity.content).allMatch(element -> element.id != null); |
| 110 | + |
| 111 | + DummyEntity reloaded = repository.findById(entity.id).orElseThrow(AssertionFailedError::new); |
| 112 | + |
| 113 | + assertThat(reloaded.content) // |
| 114 | + .isNotNull() // |
| 115 | + .extracting(e -> e.id) // |
| 116 | + .containsExactlyInAnyOrder(element1.id, element2.id); |
| 117 | + } |
| 118 | + |
| 119 | + @Test // DATAJDBC-557 |
| 120 | + public void findAllLoadsCollection() { |
| 121 | + |
| 122 | + Element element1 = new Element(); |
| 123 | + Element element2 = new Element(); |
| 124 | + |
| 125 | + DummyEntity entity = createDummyEntity(); |
| 126 | + entity.content.add(element1); |
| 127 | + entity.content.add(element2); |
| 128 | + |
| 129 | + entity = repository.save(entity); |
| 130 | + |
| 131 | + assertThat(entity.id).isNotNull(); |
| 132 | + assertThat(entity.content).allMatch(element -> element.id != null); |
| 133 | + |
| 134 | + Iterable<DummyEntity> reloaded = repository.findAll(); |
| 135 | + |
| 136 | + assertThat(reloaded) // |
| 137 | + .extracting(e -> e.id, e -> e.content.size()) // |
| 138 | + .containsExactly(tuple(entity.id, entity.content.size())); |
| 139 | + } |
| 140 | + |
| 141 | + @Test // DATAJDBC-557 |
| 142 | + @IfProfileValue(name = "current.database.is.not.mssql", value = "true") // DATAJDBC-278 |
| 143 | + public void updateSet() { |
| 144 | + |
| 145 | + Element element1 = createElement("one"); |
| 146 | + Element element2 = createElement("two"); |
| 147 | + Element element3 = createElement("three"); |
| 148 | + |
| 149 | + DummyEntity entity = createDummyEntity(); |
| 150 | + entity.content.add(element1); |
| 151 | + entity.content.add(element2); |
| 152 | + |
| 153 | + entity = repository.save(entity); |
| 154 | + |
| 155 | + entity.content.remove(element1); |
| 156 | + element2.content = "two changed"; |
| 157 | + entity.content.add(element3); |
| 158 | + |
| 159 | + entity = repository.save(entity); |
| 160 | + |
| 161 | + assertThat(entity.id).isNotNull(); |
| 162 | + assertThat(entity.content).allMatch(element -> element.id != null); |
| 163 | + |
| 164 | + DummyEntity reloaded = repository.findById(entity.id).orElseThrow(AssertionFailedError::new); |
| 165 | + |
| 166 | + // the elements got properly updated and reloaded |
| 167 | + assertThat(reloaded.content) // |
| 168 | + .isNotNull() // |
| 169 | + .extracting(e -> e.id, e -> e.content) // |
| 170 | + .containsExactlyInAnyOrder( // |
| 171 | + tuple(element2.id, "two changed"), // |
| 172 | + tuple(element3.id, "three") // |
| 173 | + ); |
| 174 | + |
| 175 | + Long count = template.queryForObject("select count(1) from Element", new HashMap<>(), Long.class); |
| 176 | + assertThat(count).isEqualTo(2); |
| 177 | + } |
| 178 | + |
| 179 | + @Test // DATAJDBC-557 |
| 180 | + public void deletingWithSet() { |
| 181 | + |
| 182 | + Element element1 = createElement("one"); |
| 183 | + Element element2 = createElement("two"); |
| 184 | + |
| 185 | + DummyEntity entity = createDummyEntity(); |
| 186 | + entity.content.add(element1); |
| 187 | + entity.content.add(element2); |
| 188 | + |
| 189 | + entity = repository.save(entity); |
| 190 | + |
| 191 | + repository.deleteById(entity.id); |
| 192 | + |
| 193 | + assertThat(repository.findById(entity.id)).isEmpty(); |
| 194 | + |
| 195 | + Long count = template.queryForObject("select count(1) from Element", new HashMap<>(), Long.class); |
| 196 | + assertThat(count).isEqualTo(0); |
| 197 | + } |
| 198 | + |
| 199 | + private Element createElement(String content) { |
| 200 | + |
| 201 | + Element element = new Element(); |
| 202 | + element.content = content; |
| 203 | + return element; |
| 204 | + } |
| 205 | + |
| 206 | + private static DummyEntity createDummyEntity() { |
| 207 | + |
| 208 | + DummyEntity entity = new DummyEntity(); |
| 209 | + return entity; |
| 210 | + } |
| 211 | + |
| 212 | + interface DummyEntityRepository extends CrudRepository<DummyEntity, Long> {} |
| 213 | + |
| 214 | + @Data |
| 215 | + static class DummyEntity { |
| 216 | + |
| 217 | + @Id private Long id; |
| 218 | + Set<Element> content = new HashSet<>(); |
| 219 | + |
| 220 | + } |
| 221 | + |
| 222 | + @RequiredArgsConstructor |
| 223 | + static class Element { |
| 224 | + |
| 225 | + @Id private Long id; |
| 226 | + String content; |
| 227 | + } |
| 228 | + |
| 229 | +} |
0 commit comments