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;
|
import java.util.List;
|
||||||
|
|
||||||
public class DetailView {
|
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 HttpClient client;
|
||||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
private final int statusOK = 200;
|
||||||
|
|
||||||
public DetailView() {
|
public DetailView() {
|
||||||
client = HttpClient.newHttpClient();
|
client = HttpClient.newHttpClient();
|
||||||
|
|
@ -32,9 +33,8 @@ public class DetailView {
|
||||||
.build();
|
.build();
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
List<Recipe> recipes = objectMapper.readValue(response.body(), new TypeReference<List<Recipe>>() {
|
return objectMapper.readValue(response.body(), new TypeReference<List<Recipe>>() {
|
||||||
}); // JSON string-> List<Recipe> (Jackson)
|
});// JSON string-> List<Recipe> (Jackson)
|
||||||
return recipes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -49,13 +49,12 @@ public class DetailView {
|
||||||
.build();
|
.build();
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
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");
|
System.out.println("No recipe found with this id");
|
||||||
return null;
|
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))
|
.uri(URI.create(BASE_URL + "/recipes/" + id))
|
||||||
.build();
|
.build();
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
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();
|
.build();
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
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
|
//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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// recipe exists so you can make a "new" recipe aka the clone
|
// recipe exists so you can make a "new" recipe aka the clone
|
||||||
Recipe clone = new Recipe();
|
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.setName(cloneName);
|
||||||
clone.setIngredients(OGRecipe.getIngredients());
|
clone.setIngredients(ogRecipe.getIngredients());
|
||||||
clone.setPreparationSteps(OGRecipe.getPreparationSteps());
|
clone.setPreparationSteps(ogRecipe.getPreparationSteps());
|
||||||
|
|
||||||
//From String to Json
|
//From String to Json
|
||||||
StringWriter s = new StringWriter();
|
StringWriter s = new StringWriter();
|
||||||
objectMapper.writeValue(s, clone);
|
objectMapper.writeValue(s, clone);
|
||||||
|
|
||||||
// Post the clone to backend
|
// Post the clone to backend
|
||||||
HttpRequest request1 = HttpRequest.newBuilder()
|
HttpRequest request2 = HttpRequest.newBuilder()
|
||||||
.POST(HttpRequest.BodyPublishers.ofString(s.toString()))
|
.POST(HttpRequest.BodyPublishers.ofString(s.toString()))
|
||||||
.uri(URI.create(BASE_URL + "/recipes/" + id))
|
.uri(URI.create(BASE_URL + "/recipes/"))
|
||||||
.build();
|
.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
|
@Test
|
||||||
void deleteTest() throws IOException, InterruptedException {
|
void deleteTest() throws IOException, InterruptedException {
|
||||||
Recipe r = dv.findId(testRecipe.getId());
|
dv.deleteRecipe(testRecipe.getId());
|
||||||
dv.deleteRecipe(r.getId());
|
|
||||||
|
|
||||||
assertNull(dv.findId(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