Turned Starbucks client into test.

Allows us to benefit from default logging setup. Removed obsolete Tomcat version declaration. Polished StoreInitializer to use Lambdas instead of an enum.
This commit is contained in:
Oliver Gierke
2015-03-25 10:41:38 +01:00
parent e11e73b58a
commit 3306ea56f3
5 changed files with 127 additions and 35 deletions

View File

@@ -24,16 +24,13 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.separator.DefaultRecordSeparatorPolicy;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindException;
/**
* Component initializing a hand full of Starbucks stores and persisting them through a {@link StoreRepository}.
@@ -80,8 +77,16 @@ public class StoreInitializer {
tokenizer.setStrict(false);
DefaultLineMapper<Store> lineMapper = new DefaultLineMapper<Store>();
lineMapper.setFieldSetMapper(fields -> {
Point location = new Point(fields.readDouble("Longitude"), fields.readDouble("Latitude"));
Address address = new Address(fields.readString("Street Address"), fields.readString("City"), fields
.readString("Zip"), location);
return new Store(fields.readString("Name"), address);
});
lineMapper.setLineTokenizer(tokenizer);
lineMapper.setFieldSetMapper(StoreFieldSetMapper.INSTANCE);
itemReader.setLineMapper(lineMapper);
itemReader.setRecordSeparatorPolicy(new DefaultRecordSeparatorPolicy());
itemReader.setLinesToSkip(1);
@@ -102,23 +107,4 @@ public class StoreInitializer {
return stores;
}
private static enum StoreFieldSetMapper implements FieldSetMapper<Store> {
INSTANCE;
/*
* (non-Javadoc)
* @see org.springframework.batch.item.file.mapping.FieldSetMapper#mapFieldSet(org.springframework.batch.item.file.transform.FieldSet)
*/
@Override
public Store mapFieldSet(FieldSet fields) throws BindException {
Point location = new Point(fields.readDouble("Longitude"), fields.readDouble("Latitude"));
Address address = new Address(fields.readString("Street Address"), fields.readString("City"),
fields.readString("Zip"), location);
return new Store(fields.readString("Name"), address);
}
}
}

View File

@@ -20,39 +20,65 @@ import java.util.HashMap;
import java.util.Map;
import java.util.stream.StreamSupport;
import lombok.extern.slf4j.Slf4j;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.hateoas.MediaTypes;
import org.springframework.hateoas.PagedResources;
import org.springframework.hateoas.PagedResources.PageMetadata;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.client.Traverson;
import org.springframework.hateoas.client.Traverson.TraversalBuilder;
import org.springframework.hateoas.mvc.TypeReferences.PagedResourcesType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* A test case to discover the search resource and execute a predefined search with it.
*
* @author Oliver Gierke
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration
@Slf4j
public class StarbucksClient {
public static void main(String[] args) {
@Configuration
static class Config {}
@Test
@Ignore
public void discoverStoreSearch() {
Traverson traverson = new Traverson(URI.create("http://localhost:8080"), MediaTypes.HAL_JSON);
// Set up path traversal
TraversalBuilder builder = traverson. //
follow("stores", "search", "by-location");
// Log discovered
log.info("");
log.info("Discovered link: {}", builder.asTemplatedLink());
log.info("");
Map<String, Object> parameters = new HashMap<>();
parameters.put("location", "40.740337,-73.995146");
parameters.put("distance", "0.5miles");
PagedResources<Resource<Store>> resources = traverson. //
follow("stores", "search", "by-location").//
PagedResources<Resource<Store>> resources = builder.//
withTemplateParameters(parameters).//
toObject(new PagedResourcesType<Resource<Store>>() {});
PageMetadata metadata = resources.getMetadata();
System.out.println(String.format("Got %s of %s stores: ", resources.getContent().size(),
metadata.getTotalElements()));
log.info("Got {} of {} stores: ", resources.getContent().size(), metadata.getTotalElements());
StreamSupport.stream(resources.spliterator(), false).//
map(Resource::getContent).//
forEach(store -> String.format("- %s - %s", store.name, store.address));
forEach(store -> log.info("{} - {}", store.name, store.address));
}
static class Store {