package com.lbslocal.api.view;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.lbslocal.api.objects.external.Address;
import com.lbslocal.api.objects.external.AddressOptions;
import com.lbslocal.api.objects.external.City;
import com.lbslocal.api.objects.external.FindAddress;
import com.lbslocal.api.objects.external.FindCity;
import com.lbslocal.api.objects.external.GetAddress;
import com.lbslocal.api.objects.external.GetXY;
import com.lbslocal.api.objects.external.ResultRange;
import com.lbslocal.test.helpers.HttpClient;
import com.lbslocal.test.helpers.Response;
import com.lbslocal.test.helpers.TestConstants;
import com.lbslocal.test.helpers.XmlParser;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class AddressFinderTest {
private AddressOptions options;
private XStream xstream;
public AddressFinderTest() throws IOException {
xstream = new XStream(new DomDriver("UTF-8"));
xstream.processAnnotations(GetAddress.class);
xstream.processAnnotations(FindAddress.class);
xstream.processAnnotations(FindCity.class);
xstream.processAnnotations(GetXY.class);
}
@Before
public void setUp() {
givenADefaultSearchOptions();
}
@Test
public void shouldFindAnAddress() throws Exception, InterruptedException {
Address address = createAnAddress();
FindAddress findAddressResource = new FindAddress();
findAddressResource.setAddress(address);
findAddressResource.setToken(TestConstants.getToken());
findAddressResource.setOptions(this.options);
String xmlToBeSent = TestConstants.XML_HEADER + xstream.toXML(findAddressResource);
Response response =
HttpClient.doPost(
TestConstants.getAddressFinderUrl(),
xmlToBeSent);
String body = response.getBody();
Assert.assertEquals("200", response.getStatusCode());
Assert.assertEquals("text/xml;charset=UTF-8", response.getContentType());
Assert.assertTrue(body.startsWith("<?xml version='1.0' encoding='UTF-8'?>"));
XmlParser.parse(body);
}
@Test
public void shouldFindACity() throws Exception, InterruptedException {
City city = createACity();
FindCity findCityResource = new FindCity();
findCityResource.setCity(city);
findCityResource.setOptions(this.options);
findCityResource.setToken(TestConstants.getToken());
String xmlToBeSent = TestConstants.XML_HEADER + xstream.toXML(findCityResource);
Response response =
HttpClient.doPost(
TestConstants.getAddressFinderUrl(),
xmlToBeSent);
String body = response.getBody();
Assert.assertEquals("200", response.getStatusCode());
Assert.assertEquals("text/xml;charset=UTF-8", response.getContentType());
Assert.assertTrue(body.startsWith("<?xml version='1.0' encoding='UTF-8'?>"));
XmlParser.parse(body);
}
@Test
public void shouldGetAnAddress() throws IOException, Exception {
com.lbslocal.api.objects.external.Point point =
new com.lbslocal.api.objects.external.Point(-46.6449081, -23.5708346);
GetAddress address = new GetAddress();
address.setPoint(point);
address.setToken(TestConstants.getToken());
address.setOptions(this.options);
address.setTolerance(5);
String xmlToBeSent = TestConstants.XML_HEADER + xstream.toXML(address);
Response response =
HttpClient.doPost(
TestConstants.getAddressFinderUrl(),
xmlToBeSent);
String body = response.getBody();
Assert.assertEquals("200", response.getStatusCode());
Assert.assertTrue(body.startsWith("<?xml version='1.0' encoding='UTF-8'?>"));
XmlParser.parse(body);
}
@Test
public void shouldGetXY() throws Exception {
GetXY getXY = new GetXY();
getXY.setAddress(createAnAddress());
getXY.setToken(TestConstants.getToken());
String xmlToBeSent = TestConstants.XML_HEADER + xstream.toXML(getXY);
Response response =
HttpClient.doPost(
TestConstants.getAddressFinderUrl(),
xmlToBeSent);
String body = response.getBody();
Assert.assertEquals("200", response.getStatusCode());
Assert.assertTrue(body.startsWith("<?xml version='1.0' encoding='UTF-8'?>"));
XmlParser.parse(body);
}
private Address createAnAddress(City city) {
Address address = new Address();
address.setHouseNumber("129");
address.setStreet("Funchal");
address.setCity(city);
return address;
}
private Address createAnAddress() {
Address address = createAnAddress(createACity());
return address;
}
private City createACity() {
City city = new City();
city.setName("Sao Paulo");
city.setState("SP");
return city;
}
private AddressFinderTest givenADefaultSearchOptions() {
ResultRange resultRange = new ResultRange();
resultRange.setPageIndex(0);
resultRange.setRecordsPerPage(0);
this.options = new AddressOptions();
this.options.setUsePhonetic(true);
this.options.setSearchType("2");
this.options.setResultRange(resultRange);
return this;
}
}