implemented clone method plus test and fixed some issues
This commit is contained in:
parent
1eab6f517a
commit
2093a43174
2 changed files with 32 additions and 17 deletions
|
|
@ -13,9 +13,10 @@ import java.net.http.HttpResponse;
|
|||
import java.util.List;
|
||||
|
||||
public class DetailView {
|
||||
private final String BASE_URL = "http://localhost:8080/api";
|
||||
private static final String BASE_URL = "http://localhost:8080/api";
|
||||
private final HttpClient client;
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private final int statusOK = 200;
|
||||
|
||||
public DetailView() {
|
||||
client = HttpClient.newHttpClient();
|
||||
|
|
@ -32,9 +33,8 @@ public class DetailView {
|
|||
.build();
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
List<Recipe> recipes = objectMapper.readValue(response.body(), new TypeReference<List<Recipe>>() {
|
||||
}); // JSON string-> List<Recipe> (Jackson)
|
||||
return recipes;
|
||||
return objectMapper.readValue(response.body(), new TypeReference<List<Recipe>>() {
|
||||
});// JSON string-> List<Recipe> (Jackson)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -49,13 +49,12 @@ public class DetailView {
|
|||
.build();
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
if(response.statusCode() != 200){
|
||||
if(response.statusCode() != statusOK){
|
||||
System.out.println("No recipe found with this id");
|
||||
return null;
|
||||
}
|
||||
Recipe recipe = objectMapper.readValue(response.body(),Recipe.class);
|
||||
|
||||
return recipe;
|
||||
return objectMapper.readValue(response.body(),Recipe.class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -68,6 +67,11 @@ public class DetailView {
|
|||
.uri(URI.create(BASE_URL + "/recipes/" + id))
|
||||
.build();
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
if(response.statusCode() != statusOK){
|
||||
System.out.println("No recipe to delete");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -83,29 +87,32 @@ public class DetailView {
|
|||
.build();
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
Recipe OGRecipe = objectMapper.readValue(response.body(),Recipe.class);
|
||||
Recipe ogRecipe = objectMapper.readValue(response.body(),Recipe.class);
|
||||
|
||||
//200 is the status code for success, other codes can mean there is no recipe to clone
|
||||
if(response.statusCode() != 200){
|
||||
if(response.statusCode() != statusOK){
|
||||
return null;
|
||||
}
|
||||
|
||||
// recipe exists so you can make a "new" recipe aka the clone
|
||||
Recipe clone = new Recipe();
|
||||
String cloneName = OGRecipe.getName() + "(clone)"; // I suppose we want unique names?
|
||||
String cloneName = ogRecipe.getName() + "(clone)"; // I suppose we want unique names?
|
||||
clone.setName(cloneName);
|
||||
clone.setIngredients(OGRecipe.getIngredients());
|
||||
clone.setPreparationSteps(OGRecipe.getPreparationSteps());
|
||||
clone.setIngredients(ogRecipe.getIngredients());
|
||||
clone.setPreparationSteps(ogRecipe.getPreparationSteps());
|
||||
|
||||
//From String to Json
|
||||
StringWriter s = new StringWriter();
|
||||
objectMapper.writeValue(s, clone);
|
||||
|
||||
// Post the clone to backend
|
||||
HttpRequest request1 = HttpRequest.newBuilder()
|
||||
HttpRequest request2 = HttpRequest.newBuilder()
|
||||
.POST(HttpRequest.BodyPublishers.ofString(s.toString()))
|
||||
.uri(URI.create(BASE_URL + "/recipes/" + id))
|
||||
.uri(URI.create(BASE_URL + "/recipes/"))
|
||||
.build();
|
||||
return clone;
|
||||
|
||||
HttpResponse<String> cloneResponse = client.send(request2, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
return objectMapper.readValue(cloneResponse.body(),Recipe.class);
|
||||
}
|
||||
}
|
||||
|
|
@ -46,10 +46,18 @@ class DetailViewTest {
|
|||
|
||||
@Test
|
||||
void deleteTest() throws IOException, InterruptedException {
|
||||
Recipe r = dv.findId(testRecipe.getId());
|
||||
dv.deleteRecipe(r.getId());
|
||||
dv.deleteRecipe(testRecipe.getId());
|
||||
|
||||
assertNull(dv.findId(testRecipe.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void cloneTest() throws IOException, InterruptedException {
|
||||
Recipe clone = dv.cloneRecipe(testRecipe.getId());
|
||||
|
||||
assertNotEquals(clone.getId(), testRecipe.getId());
|
||||
assertEquals(clone.getIngredients(), testRecipe.getIngredients());
|
||||
assertEquals(clone.getPreparationSteps(), testRecipe.getPreparationSteps());
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue