Skip to content

Commit fc0bcaf

Browse files
greenhalosSandra Thieme
authored andcommitted
Add API for searching addresses via query
1 parent 52dfb39 commit fc0bcaf

File tree

14 files changed

+270
-28
lines changed

14 files changed

+270
-28
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip
1+
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.2/apache-maven-3.5.2-bin.zip
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package net.contargo.iris.address.api;
2+
3+
import com.wordnik.swagger.annotations.Api;
4+
import com.wordnik.swagger.annotations.ApiOperation;
5+
6+
import net.contargo.iris.address.dto.AddressDto;
7+
import net.contargo.iris.address.dto.AddressDtoService;
8+
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
11+
import org.springframework.stereotype.Controller;
12+
13+
import org.springframework.web.bind.annotation.RequestMapping;
14+
import org.springframework.web.bind.annotation.RequestParam;
15+
16+
import java.util.List;
17+
18+
import static org.springframework.web.bind.annotation.RequestMethod.GET;
19+
20+
21+
/**
22+
* @author Ben Antony - antony@synyx.de
23+
* @author Sandra Thieme - thieme@synyx.de
24+
*/
25+
@Controller
26+
@Api(description = "API for querying addresses.", value = "")
27+
public class SimpleAddressApiController {
28+
29+
private final AddressDtoService nominatimAddressService;
30+
31+
@Autowired
32+
public SimpleAddressApiController(AddressDtoService nominatimAddressService) {
33+
34+
this.nominatimAddressService = nominatimAddressService;
35+
}
36+
37+
@ApiOperation(
38+
value = "Returns a list of matching addresses.",
39+
notes = "Can be static addresses or nomniatim resolved addresses."
40+
)
41+
@RequestMapping(value = "/addresses/simpleaddress", method = GET)
42+
public List<AddressDto> getAddresses(@RequestParam("query") String query) {
43+
44+
return nominatimAddressService.getAddressesByQuery(query);
45+
}
46+
}

src/main/java/net/contargo/iris/address/dto/AddressDtoService.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public interface AddressDtoService {
4646

4747
/**
4848
* Resolves an address (described by the given parameters) to a {@link java.util.List} of
49-
* {@link net.contargo.iris.address.Address} objects with the attributes name, latitude and longitude. Uses multiple
50-
* fallback strategies to find addresses if not all parameters are provided
49+
* {@link net.contargo.iris.address.Address} objects with the attributes name, latitude and longitude. Uses
50+
* multiple fallback strategies to find addresses if not all parameters are provided
5151
*
5252
* @param addressDetails The parameters describing the addresses we are looking for
5353
*
@@ -58,8 +58,8 @@ public interface AddressDtoService {
5858

5959
/**
6060
* Resolves an address (described by the given parameters) to a {@link java.util.List} of
61-
* {@link net.contargo.iris.address.Address} objects with the attributes name, latitude and longitude. Uses multiple
62-
* fallback strategies to find addresses if not all parameters are provided
61+
* {@link net.contargo.iris.address.Address} objects with the attributes name, latitude and longitude. Uses
62+
* multiple fallback strategies to find addresses if not all parameters are provided
6363
*
6464
* @param addressDetails The parameters describing the addresses we are looking for
6565
*
@@ -86,4 +86,14 @@ public interface AddressDtoService {
8686
* @return the {@link AddressDto} with the given hashKey
8787
*/
8888
AddressDto getAddressesByHashKey(String hashKey);
89+
90+
91+
/**
92+
* Returns a list of addresses matching the query.
93+
*
94+
* @param query the address query
95+
*
96+
* @return a list of matching addresses
97+
*/
98+
List<AddressDto> getAddressesByQuery(String query);
8999
}

src/main/java/net/contargo/iris/address/dto/AddressDtoServiceImpl.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import java.util.ArrayList;
99
import java.util.List;
1010
import java.util.Map;
11-
import java.util.stream.Collectors;
1211

1312
import static java.util.Collections.singletonList;
13+
import static java.util.stream.Collectors.toList;
1414

1515

1616
/**
@@ -64,7 +64,7 @@ public List<AddressListDto> getAddressesByDetails(Map<String, String> addressDet
6464
return addressServiceWrapper.getAddressesByDetails(addressDetails)
6565
.stream()
6666
.map(AddressListDto::new)
67-
.collect(Collectors.toList());
67+
.collect(toList());
6868
}
6969

7070

@@ -85,10 +85,7 @@ public List<AddressDto> getAddressesByDetailsPlain(Map<String, String> addressDe
8585
@Override
8686
public List<AddressDto> getAddressesWherePlaceIsIn(Long placeId) {
8787

88-
return addressService.getAddressesWherePlaceIsIn(placeId)
89-
.stream()
90-
.map(AddressDto::new)
91-
.collect(Collectors.toList());
88+
return addressService.getAddressesWherePlaceIsIn(placeId).stream().map(AddressDto::new).collect(toList());
9289
}
9390

9491

@@ -97,4 +94,11 @@ public AddressDto getAddressesByHashKey(String hashKey) {
9794

9895
return new AddressDto(addressServiceWrapper.getByHashKey(hashKey));
9996
}
97+
98+
99+
@Override
100+
public List<AddressDto> getAddressesByQuery(String query) {
101+
102+
return addressServiceWrapper.getAddressesByQuery(query).stream().map(AddressDto::new).collect(toList());
103+
}
100104
}

src/main/java/net/contargo/iris/address/nominatim/service/AddressService.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
* @author Sven Mueller - mueller@synyx.de
1515
* @author Aljona Murygina - murygina@synyx.de
1616
* @author Arnold Franke - franke@synyx.de
17+
* @author Sandra Thieme - thieme@synyx.de
18+
* @author Ben Antony - antony@synyx.de
1719
*/
1820
public interface AddressService {
1921

2022
/**
2123
* Resolves an address (described by the given parameters) to a {@link java.util.List} of
22-
* {@link net.contargo.iris.address.Address} objects with the attributes name, latitude and longitude. Uses multiple
23-
* fallback strategies to find addresses if not all parameters are provided
24+
* {@link net.contargo.iris.address.Address} objects with the attributes name, latitude and longitude. Uses
25+
* multiple fallback strategies to find addresses if not all parameters are provided
2426
*
2527
* @param addressDetails @return
2628
*/
@@ -51,4 +53,14 @@ public interface AddressService {
5153
* @return The address for the given geolocation.
5254
*/
5355
Address getAddressByGeolocation(GeoLocation location);
56+
57+
58+
/**
59+
* Returns a list of addresses matching the query.
60+
*
61+
* @param query the address query
62+
*
63+
* @return a list of matching addresses
64+
*/
65+
List<Address> getAddressesByQuery(String query);
5466
}

src/main/java/net/contargo/iris/address/nominatim/service/NominatimAddressService.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ public Address getAddressByGeolocation(GeoLocation geoLocation) {
120120
}
121121

122122

123+
@Override
124+
public List<Address> getAddressesByQuery(String query) {
125+
126+
String url = nominatimUrlBuilder.buildSearchUrl(query);
127+
128+
return nominatimResponder.getAddresses(url);
129+
}
130+
131+
123132
private List<Address> searchSuburbsViaNominatimsDetailPage(Long osmPlaceId, SuburbType suburbType,
124133
Set<String> suburbGlobalDisplayNames) {
125134

@@ -190,7 +199,7 @@ public List<Address> getAddressesByDetails(Map<String, String> addressDetails) {
190199
}
191200

192201

193-
private String[][] createResolvingStrategies(String postalCode, String city, String country, String name,
202+
private static String[][] createResolvingStrategies(String postalCode, String city, String country, String name,
194203
String internStreet) {
195204

196205
return new String[][] {

src/main/java/net/contargo/iris/address/nominatim/service/NominatimUrlBuilder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,10 @@ String buildUrl(GeoLocation geoLocation) {
174174

175175
return url;
176176
}
177+
178+
179+
public String buildSearchUrl(String query) {
180+
181+
return baseUrl + "search/" + query + "?format=json&addressdetails=1";
182+
}
177183
}

src/main/java/net/contargo/iris/address/service/AddressServiceWrapper.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ private List<AddressList> resolveByNominatim(Map<String, String> addressDetails)
175175
}
176176

177177

178-
private List<AddressList> getSimpleAddressList(List<Address> addresses) {
178+
private static List<AddressList> getSimpleAddressList(List<Address> addresses) {
179179

180180
List<AddressList> addressListList = new ArrayList<>();
181181

@@ -189,4 +189,10 @@ private List<AddressList> getSimpleAddressList(List<Address> addresses) {
189189

190190
return addressListList;
191191
}
192+
193+
194+
public List<Address> getAddressesByQuery(String query) {
195+
196+
return addressService.getAddressesByQuery(query);
197+
}
192198
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package net.contargo.iris.address.api;
2+
3+
import net.contargo.iris.address.Address;
4+
import net.contargo.iris.address.dto.AddressDto;
5+
import net.contargo.iris.address.dto.AddressDtoService;
6+
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
import org.junit.runner.RunWith;
11+
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
14+
import org.springframework.test.context.ContextConfiguration;
15+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
16+
import org.springframework.test.context.web.WebAppConfiguration;
17+
import org.springframework.test.web.servlet.MockMvc;
18+
19+
import org.springframework.web.context.WebApplicationContext;
20+
21+
import static org.hamcrest.Matchers.hasSize;
22+
import static org.hamcrest.Matchers.is;
23+
24+
import static org.mockito.Mockito.when;
25+
26+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
27+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
28+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
29+
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
30+
31+
import static java.util.Collections.singletonList;
32+
33+
34+
/**
35+
* @author Ben Antony - antony@synyx.de
36+
* @author Sandra Thieme - thieme@synyx.de
37+
*/
38+
@RunWith(SpringJUnit4ClassRunner.class)
39+
@ContextConfiguration(locations = { "classpath:public-api-context.xml" })
40+
@WebAppConfiguration
41+
public class SimpleAddressApiControllerUnitTest {
42+
43+
@Autowired
44+
private WebApplicationContext webApplicationContext;
45+
@Autowired
46+
private AddressDtoService addressDtoServiceMock;
47+
48+
private MockMvc mockMvc;
49+
50+
@Before
51+
public void setUp() {
52+
53+
mockMvc = webAppContextSetup(webApplicationContext).build();
54+
}
55+
56+
57+
@Test
58+
public void getAddresses() throws Exception {
59+
60+
Address address = new Address();
61+
address.setDisplayName("Gartenstr. 67, Karlsruhe (Südweststadt)");
62+
63+
when(addressDtoServiceMock.getAddressesByQuery("Gartenstraße 67, Karlsruhe")).thenReturn(singletonList(
64+
new AddressDto(address)));
65+
66+
mockMvc.perform(get("/addresses/simpleaddress").param("query", "Gartenstraße 67, Karlsruhe"))
67+
.andExpect(status().isOk())
68+
.andExpect(jsonPath("$.addressDtoList", hasSize(1)))
69+
.andExpect(jsonPath("$.addressDtoList[0].displayName", is("Gartenstr. 67, Karlsruhe (Südweststadt)")));
70+
}
71+
}

src/test/java/net/contargo/iris/address/dto/AddressDtoServiceImplUnitTest.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import static org.hamcrest.MatcherAssert.assertThat;
2525

26+
import static org.hamcrest.Matchers.hasSize;
2627
import static org.hamcrest.Matchers.is;
2728
import static org.hamcrest.Matchers.nullValue;
2829

@@ -45,16 +46,17 @@ public class AddressDtoServiceImplUnitTest {
4546
private static final int OSM_ID = 1;
4647
private static final GeoLocation GEOLOCATION = new GeoLocation(BigDecimal.ONE, BigDecimal.ONE);
4748
private static final Long PLACE_ID = 1L;
49+
4850
private final AddressService addressServiceMock = mock(AddressService.class);
4951
private final AddressServiceWrapper addressServiceWrapperMock = mock(AddressServiceWrapper.class);
5052
private final AddressDtoServiceImpl sut = new AddressDtoServiceImpl(addressServiceMock, addressServiceWrapperMock);
51-
Address address = new Address(BigDecimal.ONE, BigDecimal.ONE);
52-
private Map<String, String> addressDetails;
53+
54+
private final Address address = new Address(BigDecimal.ONE, BigDecimal.ONE);
55+
private final Map<String, String> addressDetails = new HashMap<>();
5356

5457
@Before
55-
public void setUp() throws Exception {
58+
public void setUp() {
5659

57-
addressDetails = new HashMap<>();
5860
addressDetails.put(CITY.getKey(), "city");
5961
addressDetails.put(STREET.getKey(), "street");
6062
addressDetails.put(POSTAL_CODE.getKey(), "postalcode");
@@ -175,4 +177,19 @@ public void getAddressesByHashKey() {
175177
AddressDto addressDto = sut.getAddressesByHashKey(hashKey);
176178
assertThat(addressDto.getDisplayName(), is(displayName));
177179
}
180+
181+
182+
@Test
183+
public void getAddressesByQuery() {
184+
185+
address.setDisplayName("Gartenstr. 67, Karlsruhe (Südweststadt)");
186+
187+
when(addressServiceWrapperMock.getAddressesByQuery("Gartenstraße 67, Karlsruhe")).thenReturn(singletonList(
188+
address));
189+
190+
List<AddressDto> addresses = sut.getAddressesByQuery("Gartenstraße 67, Karlsruhe");
191+
192+
assertThat(addresses, hasSize(1));
193+
assertThat(addresses.get(0).getDisplayName(), is("Gartenstr. 67, Karlsruhe (Südweststadt)"));
194+
}
178195
}

0 commit comments

Comments
 (0)