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

Completed zoo API #1

Open
wants to merge 1 commit into
base: main
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
Expand Down Expand Up @@ -34,4 +36,9 @@ public Animal addAnimal(@RequestBody Animal animal) {
public List<Animal> getAnimals() {
return animalService.getAnimals();
}

@PutMapping("/animals/{id}/treat")
public Animal treatAnimal(@PathVariable int id) {
return animalService.treatAnimal(id);
}
}
22 changes: 17 additions & 5 deletions src/main/java/com/galvanize/cryptozoology/model/Animal.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import com.fasterxml.jackson.annotation.JsonProperty;

@Entity
public class Animal {

@Id
@GeneratedValue
private int id;
private Integer id;
private String name;
private String type;
private String type;
@JsonProperty("isHappy")
private boolean isHappy;

public Animal(int id, String name, String type) {
public Animal(Integer id, String name, String type) {
this.id = id;
this.name = name;
this.type = type;
Expand All @@ -39,13 +43,21 @@ public void setType(String type) {
this.type = type;
}

public int getId() {
public Integer getId() {
return id;
}

public void setId(int id) {
public void setId(Integer id) {
this.id = id;
}

public boolean isHappy() {
return isHappy;
}

public void setHappy(boolean isHappy) {
this.isHappy = isHappy;
}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
import com.galvanize.cryptozoology.model.Animal;

@Repository
public interface ZooRepository extends JpaRepository<Animal, String>{
public interface ZooRepository extends JpaRepository<Animal, Integer>{

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ public List<Animal> getAnimals() {
return zooRepository.findAll();
}

public Animal treatAnimal(int id) {
Animal existingAnimal = zooRepository.findById(Integer.valueOf(id)).orElseThrow();
existingAnimal.setHappy(true);
return zooRepository.save(existingAnimal);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand Down Expand Up @@ -38,21 +39,48 @@ public class AnimalControllerIntegrationTest {
public void test_addAnimalIntegration_ReturnsCreatedAnimal() throws Exception {
mockMvc.perform(
post("/api/zoo/animals")
.content(animalContentString())
.contentType(MediaType.APPLICATION_JSON)
)
.andExpect(status().isCreated()).andExpect(jsonPath("id").exists())
.andExpect(jsonPath("name").value("Bird")).andExpect(jsonPath("type").value("flying"));
.content(animalContentString())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated())
.andExpect(jsonPath("id").exists())
.andExpect(jsonPath("name").value("Bird"))
.andExpect(jsonPath("type").value("flying"));
}

@Test
public void test_getAnimalsIntegration_ReturnsListOfAnimals() throws Exception {
zooRepository.save(animalContent());
zooRepository.save(animalContent());
mockMvc.perform(
get("/api/zoo/animals")).andExpect(status().isOk()).andExpect(jsonPath("$").isNotEmpty());
get("/api/zoo/animals"))
.andExpect(status().isOk())
.andExpect(jsonPath("$").isNotEmpty());

}

@Test
public void test_TreatUnHappyAnimal_ReturnsHappyAnimal() throws Exception {
Animal newAnimal = zooRepository.save(animalContent());
mockMvc.perform(
put(String.format("/api/zoo/animals/%d/treat",newAnimal.getId())))
.andExpect(status().isOk())
.andExpect(jsonPath("id"). value(newAnimal.getId()))
.andExpect(jsonPath("isHappy").value(true))
.andExpect(jsonPath("type").value("flying"))
.andExpect(jsonPath("name").value("Bird"));
}

@Test
public void test_TreatHappyAnimal_ReturnsHappyAnimal() throws Exception {
Animal newAnimal = zooRepository.save(happyAnimalContent());
mockMvc.perform(
put(String.format("/api/zoo/animals/%d/treat",newAnimal.getId())))
.andExpect(status().isOk())
.andExpect(jsonPath("id"). value(newAnimal.getId()))
.andExpect(jsonPath("isHappy").value(true))
.andExpect(jsonPath("type").value("walking"))
.andExpect(jsonPath("name").value("Dog"));
}

private Animal animalContent() {
Animal animal = new Animal();
Expand All @@ -61,6 +89,15 @@ private Animal animalContent() {
return animal;

}

private Animal happyAnimalContent() {
Animal animal = new Animal();
animal.setName("Dog");
animal.setType("walking");
animal.setHappy(true);
return animal;

}

private String animalContentString() throws IOException {
return new ObjectMapper().writeValueAsString(animalContent());
Expand Down