|
63 | 63 | import org.springframework.context.event.EventListener;
|
64 | 64 | import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
65 | 65 | import org.springframework.core.annotation.AnnotationConfigurationException;
|
66 |
| -import org.springframework.core.annotation.Order; |
| 66 | +import org.springframework.data.domain.Page; |
| 67 | +import org.springframework.data.domain.PageImpl; |
| 68 | +import org.springframework.data.domain.Slice; |
| 69 | +import org.springframework.data.domain.SliceImpl; |
| 70 | +import org.springframework.data.geo.Distance; |
| 71 | +import org.springframework.data.geo.GeoPage; |
| 72 | +import org.springframework.data.geo.GeoResult; |
| 73 | +import org.springframework.data.geo.GeoResults; |
67 | 74 | import org.springframework.http.HttpStatus;
|
68 | 75 | import org.springframework.http.HttpStatusCode;
|
69 | 76 | import org.springframework.http.MediaType;
|
@@ -756,6 +763,28 @@ public void findByIdWhenUnauthorizedResultThenDenies() {
|
756 | 763 | assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(flight::getAltitude);
|
757 | 764 | }
|
758 | 765 |
|
| 766 | + @Test |
| 767 | + @WithMockUser(authorities = "airplane:read") |
| 768 | + public void findGeoResultByIdWhenAuthorizedResultThenAuthorizes() { |
| 769 | + this.spring.register(AuthorizeResultConfig.class).autowire(); |
| 770 | + FlightRepository flights = this.spring.getContext().getBean(FlightRepository.class); |
| 771 | + GeoResult<Flight> geoResultFlight = flights.findGeoResultFlightById("1"); |
| 772 | + Flight flight = geoResultFlight.getContent(); |
| 773 | + assertThatNoException().isThrownBy(flight::getAltitude); |
| 774 | + assertThatNoException().isThrownBy(flight::getSeats); |
| 775 | + } |
| 776 | + |
| 777 | + @Test |
| 778 | + @WithMockUser(authorities = "seating:read") |
| 779 | + public void findGeoResultByIdWhenUnauthorizedResultThenDenies() { |
| 780 | + this.spring.register(AuthorizeResultConfig.class).autowire(); |
| 781 | + FlightRepository flights = this.spring.getContext().getBean(FlightRepository.class); |
| 782 | + GeoResult<Flight> geoResultFlight = flights.findGeoResultFlightById("1"); |
| 783 | + Flight flight = geoResultFlight.getContent(); |
| 784 | + assertThatNoException().isThrownBy(flight::getSeats); |
| 785 | + assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(flight::getAltitude); |
| 786 | + } |
| 787 | + |
759 | 788 | @Test
|
760 | 789 | @WithMockUser(authorities = "airplane:read")
|
761 | 790 | public void findByIdWhenAuthorizedResponseEntityThenAuthorizes() {
|
@@ -827,6 +856,46 @@ public void findAllWhenPostFilterThenFilters() {
|
827 | 856 | .doesNotContain("Kevin Mitnick"));
|
828 | 857 | }
|
829 | 858 |
|
| 859 | + @Test |
| 860 | + @WithMockUser(authorities = "airplane:read") |
| 861 | + public void findPageWhenPostFilterThenFilters() { |
| 862 | + this.spring.register(AuthorizeResultConfig.class).autowire(); |
| 863 | + FlightRepository flights = this.spring.getContext().getBean(FlightRepository.class); |
| 864 | + flights.findPage() |
| 865 | + .forEach((flight) -> assertThat(flight.getPassengers()).extracting(Passenger::getName) |
| 866 | + .doesNotContain("Kevin Mitnick")); |
| 867 | + } |
| 868 | + |
| 869 | + @Test |
| 870 | + @WithMockUser(authorities = "airplane:read") |
| 871 | + public void findSliceWhenPostFilterThenFilters() { |
| 872 | + this.spring.register(AuthorizeResultConfig.class).autowire(); |
| 873 | + FlightRepository flights = this.spring.getContext().getBean(FlightRepository.class); |
| 874 | + flights.findSlice() |
| 875 | + .forEach((flight) -> assertThat(flight.getPassengers()).extracting(Passenger::getName) |
| 876 | + .doesNotContain("Kevin Mitnick")); |
| 877 | + } |
| 878 | + |
| 879 | + @Test |
| 880 | + @WithMockUser(authorities = "airplane:read") |
| 881 | + public void findGeoPageWhenPostFilterThenFilters() { |
| 882 | + this.spring.register(AuthorizeResultConfig.class).autowire(); |
| 883 | + FlightRepository flights = this.spring.getContext().getBean(FlightRepository.class); |
| 884 | + flights.findGeoPage() |
| 885 | + .forEach((flight) -> assertThat(flight.getContent().getPassengers()).extracting(Passenger::getName) |
| 886 | + .doesNotContain("Kevin Mitnick")); |
| 887 | + } |
| 888 | + |
| 889 | + @Test |
| 890 | + @WithMockUser(authorities = "airplane:read") |
| 891 | + public void findGeoResultsWhenPostFilterThenFilters() { |
| 892 | + this.spring.register(AuthorizeResultConfig.class).autowire(); |
| 893 | + FlightRepository flights = this.spring.getContext().getBean(FlightRepository.class); |
| 894 | + flights.findGeoResults() |
| 895 | + .forEach((flight) -> assertThat(flight.getContent().getPassengers()).extracting(Passenger::getName) |
| 896 | + .doesNotContain("Kevin Mitnick")); |
| 897 | + } |
| 898 | + |
830 | 899 | @Test
|
831 | 900 | @WithMockUser(authorities = "airplane:read")
|
832 | 901 | public void findAllWhenPreFilterThenFilters() {
|
@@ -1762,16 +1831,8 @@ static class AuthorizeResultConfig {
|
1762 | 1831 |
|
1763 | 1832 | @Bean
|
1764 | 1833 | @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
|
1765 |
| - @Order(1) |
1766 |
| - static TargetVisitor mock() { |
1767 |
| - return Mockito.mock(TargetVisitor.class); |
1768 |
| - } |
1769 |
| - |
1770 |
| - @Bean |
1771 |
| - @Role(BeanDefinition.ROLE_INFRASTRUCTURE) |
1772 |
| - @Order(0) |
1773 |
| - static TargetVisitor skipValueTypes() { |
1774 |
| - return TargetVisitor.defaultsSkipValueTypes(); |
| 1834 | + static TargetVisitor customTargetVisitor() { |
| 1835 | + return TargetVisitor.of(Mockito.mock(), TargetVisitor.defaultsSkipValueTypes()); |
1775 | 1836 | }
|
1776 | 1837 |
|
1777 | 1838 | @Bean
|
@@ -1802,10 +1863,39 @@ Iterator<Flight> findAll() {
|
1802 | 1863 | return this.flights.values().iterator();
|
1803 | 1864 | }
|
1804 | 1865 |
|
| 1866 | + Page<Flight> findPage() { |
| 1867 | + return new PageImpl<>(new ArrayList<>(this.flights.values())); |
| 1868 | + } |
| 1869 | + |
| 1870 | + Slice<Flight> findSlice() { |
| 1871 | + return new SliceImpl<>(new ArrayList<>(this.flights.values())); |
| 1872 | + } |
| 1873 | + |
| 1874 | + GeoPage<Flight> findGeoPage() { |
| 1875 | + List<GeoResult<Flight>> results = new ArrayList<>(); |
| 1876 | + for (Flight flight : this.flights.values()) { |
| 1877 | + results.add(new GeoResult<>(flight, new Distance(flight.altitude))); |
| 1878 | + } |
| 1879 | + return new GeoPage<>(new GeoResults<>(results)); |
| 1880 | + } |
| 1881 | + |
| 1882 | + GeoResults<Flight> findGeoResults() { |
| 1883 | + List<GeoResult<Flight>> results = new ArrayList<>(); |
| 1884 | + for (Flight flight : this.flights.values()) { |
| 1885 | + results.add(new GeoResult<>(flight, new Distance(flight.altitude))); |
| 1886 | + } |
| 1887 | + return new GeoResults<>(results); |
| 1888 | + } |
| 1889 | + |
1805 | 1890 | Flight findById(String id) {
|
1806 | 1891 | return this.flights.get(id);
|
1807 | 1892 | }
|
1808 | 1893 |
|
| 1894 | + GeoResult<Flight> findGeoResultFlightById(String id) { |
| 1895 | + Flight flight = this.flights.get(id); |
| 1896 | + return new GeoResult<>(flight, new Distance(flight.altitude)); |
| 1897 | + } |
| 1898 | + |
1809 | 1899 | Flight save(Flight flight) {
|
1810 | 1900 | this.flights.put(flight.getId(), flight);
|
1811 | 1901 | return flight;
|
|
0 commit comments