Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding new endpoints and updating tests #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 42 additions & 14 deletions src/main/java/cx/catapult/animals/service/BaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,54 @@
import cx.catapult.animals.domain.Animal;

import java.util.*;
import java.util.stream.Collectors;

public abstract class BaseService<T extends Animal> implements Service<T> {

private HashMap<String, T> items = new HashMap<>();
private HashMap<String, T> items = new HashMap<>();

@Override
public Collection<T> all() {
return items.values();
}
@Override
public Collection<T> all() {
return items.values();
}

@Override
public T create(T animal) {
String id = UUID.randomUUID().toString();
animal.setId(id);
items.put(id, animal);
return animal;
}

@Override
public T get(String id) {
return items.get(id);
}

@Override
public Collection<T> filteredAll(String searchString) {
return items.entrySet().stream()
.filter(entry -> {
T rowData = entry.getValue();
return rowData.getName().toLowerCase().contains(searchString.toLowerCase()) || rowData.getDescription().toLowerCase().contains(searchString.toLowerCase());
}).map(Map.Entry::getValue)
.collect(Collectors.toList());
}

@Override
public T create(T animal) {
String id = UUID.randomUUID().toString();
animal.setId(id);
items.put(id, animal);
return animal;
@Override
public T update(String id, T animal) {
if (items.containsKey(id)) {
items.put(id, animal);
}
return animal;
}

@Override
public T get(String id) {
return items.get(id);
@Override
public T delete(String id) {
T currentData = items.get(id);
if (items.containsKey(id)) {
items.remove(id);
}
return currentData;
}
}
6 changes: 6 additions & 0 deletions src/main/java/cx/catapult/animals/service/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ public interface Service<T extends Animal> {

public T get(String id);

T update(String id, T animal);

T delete(String id);

public Collection<T> filteredAll(String searchString);

}
65 changes: 43 additions & 22 deletions src/main/java/cx/catapult/animals/web/CatsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,51 @@

import java.util.Collection;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping(path = "/api/1/cats", produces = MediaType.APPLICATION_JSON_VALUE)
public class CatsController {

@Autowired
private CatsService service;

@GetMapping(value = "", produces = "application/json")
public @ResponseBody
Collection<Cat> all() {
return service.all();
}

@GetMapping(value = "/{id}")
public @ResponseBody
Cat get(@PathVariable String id) {
return service.get(id);
}

@PostMapping(value = "", consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public @ResponseBody
Cat
create(@RequestBody Cat cat) {
return service.create(cat);
}
@Autowired
private CatsService service;

@GetMapping(value = "", produces = "application/json")
public @ResponseBody
Collection<Cat> all() {
return service.all();
}

@GetMapping(value = "/{id}")
public @ResponseBody
Cat get(@PathVariable String id) {
return service.get(id);
}

@PostMapping(value = "", consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public @ResponseBody
Cat
create(@RequestBody Cat cat) {
return service.create(cat);
}

@GetMapping(value = "", produces = "application/json", params = "searchString")
public @ResponseBody
Collection<Cat> filter(@RequestParam("searchString") String searchString) {
return searchString.isEmpty() ? service.all() : service.filteredAll(searchString);
}

@PutMapping(value = "/{id}", consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody
Cat update(@PathVariable String id, @RequestBody Cat cat) {
return service.update(id, cat);
}

@DeleteMapping(value = "/{id}")
@ResponseStatus(HttpStatus.OK)
public @ResponseBody
Cat delete(@PathVariable String id) {
return service.delete(id);
}
}
94 changes: 64 additions & 30 deletions src/test/java/cx/catapult/animals/service/CatsServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,68 @@

public class CatsServiceTest {

CatsService service = new CatsService();
Cat cat = new Cat("Tom", "Bob cat");

@Test
public void createShouldWork() throws Exception {
Cat thisCat = new Cat();
thisCat.setName("Jerry");
thisCat.setDescription("Mouse Cat");
Cat actual = service.create(thisCat);
assertThat(actual).isEqualTo(thisCat);
assertThat(actual.getName()).isEqualTo(thisCat.getName());
assertThat(actual.getDescription()).isEqualTo(thisCat.getDescription());
assertThat(actual.getGroup()).isEqualTo(thisCat.getGroup());
}

@Test
public void allShouldWork() throws Exception {
service.create(cat);
assertThat(service.all().size()).isEqualTo(1);
}

@Test
public void getShouldWork() throws Exception {
service.create(cat);
Cat actual = service.get(cat.getId());
assertThat(actual).isEqualTo(cat);
assertThat(actual.getName()).isEqualTo(cat.getName());
assertThat(actual.getDescription()).isEqualTo(cat.getDescription());
assertThat(actual.getGroup()).isEqualTo(cat.getGroup());
}
CatsService service = new CatsService();
Cat cat = new Cat("Tom", "Bob cat");

@Test
public void createShouldWork() throws Exception {
Cat thisCat = new Cat();
thisCat.setName("Jerry");
thisCat.setDescription("Mouse Cat");
Cat actual = service.create(thisCat);
assertThat(actual).isEqualTo(thisCat);
assertThat(actual.getName()).isEqualTo(thisCat.getName());
assertThat(actual.getDescription()).isEqualTo(thisCat.getDescription());
assertThat(actual.getGroup()).isEqualTo(thisCat.getGroup());
}

@Test
public void allShouldWork() throws Exception {
service.create(cat);
assertThat(service.all().size()).isEqualTo(1);
}

@Test
public void getShouldWork() throws Exception {
service.create(cat);
Cat actual = service.get(cat.getId());
assertThat(actual).isEqualTo(cat);
assertThat(actual.getName()).isEqualTo(cat.getName());
assertThat(actual.getDescription()).isEqualTo(cat.getDescription());
assertThat(actual.getGroup()).isEqualTo(cat.getGroup());
}

@Test
public void updateShouldWork() throws Exception {
service.create(cat);
String catId = cat.getId();
Cat actual = service.get(catId);
actual.setName("Robo");
actual.setDescription("my new cat");
Cat updatedActual = service.update(catId, actual);
assertThat(updatedActual.getName()).isEqualTo("Robo");
assertThat(updatedActual.getDescription()).isEqualTo("my new cat");
assertThat(updatedActual.getGroup()).isEqualTo(cat.getGroup());
}

@Test
public void deleteShouldWork() throws Exception {
service.create(cat);
String catId = cat.getId();
service.delete(catId);
Cat actual = service.get(catId);
assertThat(actual).isEqualTo(null);
}

@Test
public void filterShouldWork() throws Exception {
Cat catOne = new Cat("Jerry", "my fat cat");
Cat catTwo = new Cat("Julu", "cute little cat, like old jerry");
service.create(catOne);
service.create(catTwo);
assertThat(service.filteredAll("jerry").size()).isEqualTo(2);
assertThat(service.filteredAll("cute").size()).isEqualTo(1);
assertThat(service.filteredAll("").size()).isEqualTo(2);
assertThat(service.filteredAll("nice").size()).isEqualTo(0);
}
}
93 changes: 49 additions & 44 deletions src/test/java/cx/catapult/animals/web/CatsControllerIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,53 @@
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

public class CatsControllerIT {
@LocalServerPort
private int port;

private URL base;

private Cat cat = new Cat("Tom", "Bob cat");

@Autowired
private TestRestTemplate template;

@BeforeEach
public void setUp() throws Exception {
this.base = new URL("http://localhost:" + port + "/api/1/cats");
}

@Test
public void createShouldWork() throws Exception {
ResponseEntity<Cat> response = template.postForEntity(base.toString(), cat, Cat.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED);
assertThat(response.getBody().getId()).isNotEmpty();
assertThat(response.getBody().getName()).isEqualTo(cat.getName());
assertThat(response.getBody().getDescription()).isEqualTo(cat.getDescription());
assertThat(response.getBody().getGroup()).isEqualTo(cat.getGroup());
}

@Test
public void allShouldWork() throws Exception {
Collection items = template.getForObject(base.toString(), Collection.class);
assertThat(items.size()).isGreaterThanOrEqualTo(7);
}

@Test
public void getShouldWork() throws Exception {
Cat created = create("Test 1");
ResponseEntity<String> response = template.getForEntity(base.toString() + "/" + created.getId(), String.class);
assertThat(response.getBody()).isNotEmpty();
}

Cat create(String name) {
Cat created = template.postForObject(base.toString(), new Cat(name, name), Cat.class);
assertThat(created.getId()).isNotEmpty();
assertThat(created.getName()).isEqualTo(name);
return created;
}
@LocalServerPort
private int port;

private URL base;

private Cat cat = new Cat("Tom", "Bob cat");

@Autowired
private TestRestTemplate template;

@BeforeEach
public void setUp() throws Exception {
this.base = new URL("http://localhost:" + port + "/api/1/cats");
}

@Test
public void createShouldWork() throws Exception {
ResponseEntity<Cat> response = template.postForEntity(base.toString(), cat, Cat.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED);
assertThat(response.getBody().getId()).isNotEmpty();
assertThat(response.getBody().getName()).isEqualTo(cat.getName());
assertThat(response.getBody().getDescription()).isEqualTo(cat.getDescription());
assertThat(response.getBody().getGroup()).isEqualTo(cat.getGroup());
}

@Test
public void allShouldWork() throws Exception {
Collection items = template.getForObject(base.toString(), Collection.class);
assertThat(items.size()).isGreaterThanOrEqualTo(7);
}

@Test
public void getShouldWork() throws Exception {
Cat created = create("Test 1");
ResponseEntity<String> response = template.getForEntity(base.toString() + "/" + created.getId(), String.class);
assertThat(response.getBody()).isNotEmpty();
}

@Test
public void updateShouldWork() throws Exception {
template.put(base.toString(), cat);
}

Cat create(String name) {
Cat created = template.postForObject(base.toString(), new Cat(name, name), Cat.class);
assertThat(created.getId()).isNotEmpty();
assertThat(created.getName()).isEqualTo(name);
return created;
}
}
Loading