Skip to content

Commit

Permalink
It is finally working including all tests and azure function
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Philip authored and Jan Philip committed Oct 19, 2021
1 parent 21e57bb commit 0d201f9
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.hswhameln.isbnvalidator;

import de.hswhameln.isbnvalidator.beans.ISBNValidationResult;
import de.hswhameln.isbnvalidator.dto.ValidationRequest;
import de.hswhameln.isbnvalidator.dto.ValidationResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package de.hswhameln.isbnvalidator.dto;

public class ValidationResponse {
private String isbn;


private boolean valid;
private String message;

public ValidationResponse() {

}

public ValidationResponse(String isbn, boolean valid, String message) {
this.isbn = isbn;
this.valid = valid;
this.message = message;
}

public String getIsbn() {
return isbn;
}

public void setIsbn(String isbn) {
this.isbn = isbn;
}

public boolean isValid() {
return valid;
}

public void setValid(boolean valid) {
this.valid = valid;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
package de.hswhameln.isbnvalidator.utils;

import de.hswhameln.isbnvalidator.dto.ValidationResponse;
import de.hswhameln.isbnvalidator.exceptions.ISBNValidatorNotAccessibleException;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ISBNAPI {
private static final Logger logger = LoggerFactory.getLogger(ISBNAPI.class);

public static boolean validateISBN(String isbn) {
logger.info("Start Rest-call to validateISBN for isbn: " + isbn);
RestConsumer isbnValidator = RestConsumer.getInstance();
JSONObject response = isbnValidator.getRequest("/ValidateISBN", isbn);
try {
return response.getBoolean("valid");
} catch (JSONException e) {
throw new ISBNValidatorNotAccessibleException();
}
ValidationResponse response = isbnValidator.getRequest("/ValidateISBN", isbn);
return response.isValid();
}

/*
public static int getCheckDigit(String isbn) {
logger.info("Start Rest-call getCheckDigit for isbn: " + isbn);
RestConsumer isbnValidator = RestConsumer.getInstance();
JSONObject response = isbnValidator.getRequest("/checkDigit", isbn);
ValidationRe response = isbnValidator.getRequest("/checkDigit", isbn);
try {
return response.getInt("checkDigit");
} catch (JSONException e) {
throw new ISBNValidatorNotAccessibleException();
}
}
*/

}
Original file line number Diff line number Diff line change
@@ -1,34 +1,48 @@
package de.hswhameln.isbnvalidator.utils;

import de.hswhameln.isbnvalidator.dto.ValidationResponse;
import de.hswhameln.isbnvalidator.exceptions.ISBNValidatorNotAccessibleException;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;


import java.util.HashMap;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RestConsumer {
private static RestConsumer instance;

public static RestConsumer getInstance() {
if(instance == null) {
instance = new RestConsumer("https://validateisbn.azurewebsites.net/api/");
instance = new RestConsumer("https://isbnchecker.azurewebsites.net/api");
}
return instance;
}

private final Logger logger = LoggerFactory.getLogger(RestConsumer.class);
private String url;

private RestConsumer(String url) {
this.url = url;
}

public JSONObject postRequest(String urlPath, Map<String, String> params) {
this.logger.info(String.format("Execute Post-Request with params <%s>", params.toString()));
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity(url + urlPath, params, String.class);

if (response.getStatusCode().equals(HttpStatus.OK)) {
try {
return new JSONObject(response.getBody());
Expand All @@ -42,17 +56,17 @@ public JSONObject postRequest(String urlPath, Map<String, String> params) {
}
}

public JSONObject getRequest(String urlPath, String isbn) {
Map<String, String> params = new HashMap<>();
params.put("isbn", isbn);
public ValidationResponse getRequest(String urlPath, String isbn) {
this.logger.info(String.format("Execute Get-Request with param <%s>", isbn));
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.getForEntity(url + urlPath, String.class, params);
ResponseEntity<ValidationResponse> response = restTemplate.getForEntity(url + urlPath + "?isbn=" + isbn, ValidationResponse.class);


this.logger.info("Received a response for the Get-Request with status: " + response.getStatusCode());
this.logger.info("Response includes Body: " + response.getBody());
if (response.getStatusCode().equals(HttpStatus.OK)) {
try {
return new JSONObject(response.getBody());
} catch (JSONException e) {
throw new ISBNValidatorNotAccessibleException();
}
return response.getBody();
} else if (response.getStatusCode().equals(HttpStatus.CONFLICT)) {
throw new IllegalArgumentException();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import de.hswhameln.isbnvalidator.beans.Book;
import de.hswhameln.isbnvalidator.exceptions.BookAlreadyExistsException;
import de.hswhameln.isbnvalidator.exceptions.BookNotFoundException;
import de.hswhameln.isbnvalidator.exceptions.ISBNNotValidException;
import de.hswhameln.isbnvalidator.repositories.BookRepository;
import de.hswhameln.isbnvalidator.services.BookService;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -65,18 +66,17 @@ void createBookTest() {
void findBookTest() { assertTrue(service.findBook("978-3-442-22232-2").isPresent());
}

@Disabled @Test
@Test
@DirtiesContext
void createFalseBookTest() {
// Auf Jano warten
service.createBook(new Book("Star Wars: Das Licht der Jedi", "Charles Soule","Blanvalent","978-0-593-15771-9"));
System.out.println(service.findBook("978-0-593-15771-9").get().getTitle());
assertThrows(ISBNNotValidException.class, () -> service.createBook(new Book("Star Wars: Das Licht der Jedi", "Charles Soule","Blanvalent","978-0-593-15771-9")));
}

@Test
@DirtiesContext
void createAndDeleteTest() {
Book b = new Book("SuperSache", "Markus Büning", "Panini", "978-3-779-50599-0");
Book b = new Book("SuperSache", "Markus Büning", "Panini", "978-3-16-148410-0");
service.createBook(b);
service.deleteBook(b);
}
Expand Down

0 comments on commit 0d201f9

Please sign in to comment.