|
64 | 64 | import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
65 | 65 | import org.springframework.core.annotation.AnnotationConfigurationException;
|
66 | 66 | import org.springframework.core.annotation.Order;
|
| 67 | +import org.springframework.data.domain.Page; |
| 68 | +import org.springframework.data.domain.PageImpl; |
| 69 | +import org.springframework.data.domain.Slice; |
| 70 | +import org.springframework.data.domain.SliceImpl; |
| 71 | +import org.springframework.data.geo.Distance; |
| 72 | +import org.springframework.data.geo.GeoPage; |
| 73 | +import org.springframework.data.geo.GeoResult; |
| 74 | +import org.springframework.data.geo.GeoResults; |
67 | 75 | import org.springframework.http.HttpStatus;
|
68 | 76 | import org.springframework.http.HttpStatusCode;
|
69 | 77 | import org.springframework.http.MediaType;
|
@@ -756,6 +764,28 @@ public void findByIdWhenUnauthorizedResultThenDenies() {
|
756 | 764 | assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(flight::getAltitude);
|
757 | 765 | }
|
758 | 766 |
|
| 767 | + @Test |
| 768 | + @WithMockUser(authorities = "airplane:read") |
| 769 | + public void findGeoResultByIdWhenAuthorizedResultThenAuthorizes() { |
| 770 | + this.spring.register(AuthorizeResultConfig.class).autowire(); |
| 771 | + FlightRepository flights = this.spring.getContext().getBean(FlightRepository.class); |
| 772 | + GeoResult<Flight> geoResultFlight = flights.findGeoResultFlightById("1"); |
| 773 | + Flight flight = geoResultFlight.getContent(); |
| 774 | + assertThatNoException().isThrownBy(flight::getAltitude); |
| 775 | + assertThatNoException().isThrownBy(flight::getSeats); |
| 776 | + } |
| 777 | + |
| 778 | + @Test |
| 779 | + @WithMockUser(authorities = "seating:read") |
| 780 | + public void findGeoResultByIdWhenUnauthorizedResultThenDenies() { |
| 781 | + this.spring.register(AuthorizeResultConfig.class).autowire(); |
| 782 | + FlightRepository flights = this.spring.getContext().getBean(FlightRepository.class); |
| 783 | + GeoResult<Flight> geoResultFlight = flights.findGeoResultFlightById("1"); |
| 784 | + Flight flight = geoResultFlight.getContent(); |
| 785 | + assertThatNoException().isThrownBy(flight::getSeats); |
| 786 | + assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(flight::getAltitude); |
| 787 | + } |
| 788 | + |
759 | 789 | @Test
|
760 | 790 | @WithMockUser(authorities = "airplane:read")
|
761 | 791 | public void findByIdWhenAuthorizedResponseEntityThenAuthorizes() {
|
@@ -827,6 +857,46 @@ public void findAllWhenPostFilterThenFilters() {
|
827 | 857 | .doesNotContain("Kevin Mitnick"));
|
828 | 858 | }
|
829 | 859 |
|
| 860 | + @Test |
| 861 | + @WithMockUser(authorities = "airplane:read") |
| 862 | + public void findPageWhenPostFilterThenFilters() { |
| 863 | + this.spring.register(AuthorizeResultConfig.class).autowire(); |
| 864 | + FlightRepository flights = this.spring.getContext().getBean(FlightRepository.class); |
| 865 | + flights.findPage() |
| 866 | + .forEach((flight) -> assertThat(flight.getPassengers()).extracting(Passenger::getName) |
| 867 | + .doesNotContain("Kevin Mitnick")); |
| 868 | + } |
| 869 | + |
| 870 | + @Test |
| 871 | + @WithMockUser(authorities = "airplane:read") |
| 872 | + public void findSliceWhenPostFilterThenFilters() { |
| 873 | + this.spring.register(AuthorizeResultConfig.class).autowire(); |
| 874 | + FlightRepository flights = this.spring.getContext().getBean(FlightRepository.class); |
| 875 | + flights.findSlice() |
| 876 | + .forEach((flight) -> assertThat(flight.getPassengers()).extracting(Passenger::getName) |
| 877 | + .doesNotContain("Kevin Mitnick")); |
| 878 | + } |
| 879 | + |
| 880 | + @Test |
| 881 | + @WithMockUser(authorities = "airplane:read") |
| 882 | + public void findGeoPageWhenPostFilterThenFilters() { |
| 883 | + this.spring.register(AuthorizeResultConfig.class).autowire(); |
| 884 | + FlightRepository flights = this.spring.getContext().getBean(FlightRepository.class); |
| 885 | + flights.findGeoPage() |
| 886 | + .forEach((flight) -> assertThat(flight.getContent().getPassengers()).extracting(Passenger::getName) |
| 887 | + .doesNotContain("Kevin Mitnick")); |
| 888 | + } |
| 889 | + |
| 890 | + @Test |
| 891 | + @WithMockUser(authorities = "airplane:read") |
| 892 | + public void findGeoResultsWhenPostFilterThenFilters() { |
| 893 | + this.spring.register(AuthorizeResultConfig.class).autowire(); |
| 894 | + FlightRepository flights = this.spring.getContext().getBean(FlightRepository.class); |
| 895 | + flights.findGeoResults() |
| 896 | + .forEach((flight) -> assertThat(flight.getContent().getPassengers()).extracting(Passenger::getName) |
| 897 | + .doesNotContain("Kevin Mitnick")); |
| 898 | + } |
| 899 | + |
830 | 900 | @Test
|
831 | 901 | @WithMockUser(authorities = "airplane:read")
|
832 | 902 | public void findAllWhenPreFilterThenFilters() {
|
@@ -1802,10 +1872,39 @@ Iterator<Flight> findAll() {
|
1802 | 1872 | return this.flights.values().iterator();
|
1803 | 1873 | }
|
1804 | 1874 |
|
| 1875 | + Page<Flight> findPage() { |
| 1876 | + return new PageImpl<>(new ArrayList<>(this.flights.values())); |
| 1877 | + } |
| 1878 | + |
| 1879 | + Slice<Flight> findSlice() { |
| 1880 | + return new SliceImpl<>(new ArrayList<>(this.flights.values())); |
| 1881 | + } |
| 1882 | + |
| 1883 | + GeoPage<Flight> findGeoPage() { |
| 1884 | + List<GeoResult<Flight>> results = new ArrayList<>(); |
| 1885 | + for (Flight flight : this.flights.values()) { |
| 1886 | + results.add(new GeoResult<>(flight, new Distance(flight.altitude))); |
| 1887 | + } |
| 1888 | + return new GeoPage<>(new GeoResults<>(results)); |
| 1889 | + } |
| 1890 | + |
| 1891 | + GeoResults<Flight> findGeoResults() { |
| 1892 | + List<GeoResult<Flight>> results = new ArrayList<>(); |
| 1893 | + for (Flight flight : this.flights.values()) { |
| 1894 | + results.add(new GeoResult<>(flight, new Distance(flight.altitude))); |
| 1895 | + } |
| 1896 | + return new GeoResults<>(results); |
| 1897 | + } |
| 1898 | + |
1805 | 1899 | Flight findById(String id) {
|
1806 | 1900 | return this.flights.get(id);
|
1807 | 1901 | }
|
1808 | 1902 |
|
| 1903 | + GeoResult<Flight> findGeoResultFlightById(String id) { |
| 1904 | + Flight flight = this.flights.get(id); |
| 1905 | + return new GeoResult<>(flight, new Distance(flight.altitude)); |
| 1906 | + } |
| 1907 | + |
1809 | 1908 | Flight save(Flight flight) {
|
1810 | 1909 | this.flights.put(flight.getId(), flight);
|
1811 | 1910 | return flight;
|
|
0 commit comments