Skip to content
Snippets Groups Projects
Commit 3054ed43 authored by Jakob Karevold Grønhaug's avatar Jakob Karevold Grønhaug
Browse files

Vare-API

parent 55ca3e2e
Branches
No related tags found
1 merge request!7Vare-API
config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
\ No newline at end of file
package edu.ntnu.idatt210602.matsvinnbackend;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import lombok.Generated;
@SpringBootApplication
@EnableAutoConfiguration
public class MatsvinnBackendApplication {
@Generated
public static void main(String[] args) {
SpringApplication.run(MatsvinnBackendApplication.class, args);
}
......
package edu.ntnu.idatt210602.matsvinnbackend.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.server.ResponseStatusException;
import edu.ntnu.idatt210602.matsvinnbackend.model.Item;
@CrossOrigin
@RestController
@RequestMapping(path = "/item")
public class ItemController {
private static String KASSALAPP_API_URL = "http://localhost:2730";
private RestTemplate restTemplate;
public ItemController() {
this.restTemplate = new RestTemplate();
}
@GetMapping("")
public List<Item> search(@RequestParam("search") String search) {
ArrayList<Item> result = restTemplate.getForObject(KASSALAPP_API_URL + "/?search=" + search, ArrayList.class);
return result;
}
@GetMapping("/{id}")
public Item getItemById(@PathVariable("id") String id) {
try {
Item result = restTemplate.getForObject(KASSALAPP_API_URL + "/id/" + id, Item.class);
return result;
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
}
}
package edu.ntnu.idatt210602.matsvinnbackend.model;
import java.io.Serializable;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.util.List;
import lombok.Generated;
import org.springframework.beans.factory.annotation.Value;
import com.google.gson.Gson;
import jakarta.persistence.Entity;
import lombok.Getter;
import lombok.Setter;
@Generated
public class Item implements Serializable {
@Value("${kassalapp.api-url}")
private static String KASSALAPP_API_URL;
Item() {}
Item(String name, String ean, String image_url, String store, List<String> allergens, List<Nutrition> nutrition) {
public Item() {}
@Generated
public Item(String name, String ean, String image_url, String store, List<String> allergens, List<Nutrition> nutrition) {
this.allergens = allergens;
this.ean = ean;
this.image_url = image_url;
......
package edu.ntnu.idatt210602.matsvinnbackend.model;
import lombok.Generated;
@Generated
public record Nutrition(String code, String display_name, double amount, String unit) {}
package edu.ntnu.idatt210602.matsvinnbackend.controller;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.ArrayList;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.server.ResponseStatusException;
import edu.ntnu.idatt210602.matsvinnbackend.model.Item;
@SpringBootTest
public class ItemControllerTests {
@Mock
RestTemplate restTemplate;
@InjectMocks
ItemController itemController;
@Test
void getByIdPositive() {
Mockito.when(restTemplate.getForObject(Mockito.anyString(), Mockito.any())).thenReturn(new Item() {
{
setName("Kroneis");
}
});
assertEquals("Kroneis", itemController.getItemById("1234").getName());
}
@Test
void getByIdNegative() {
Mockito.when(restTemplate.getForObject(Mockito.anyString(), Mockito.any())).thenThrow(new RuntimeException());
assertThrows(ResponseStatusException.class, () -> {
itemController.getItemById("1234");
});
}
@Test
void searchWithResults() {
Mockito.when(restTemplate.getForObject(Mockito.anyString(), Mockito.any())).thenReturn(new ArrayList<Item>() {
{
add(new Item());
add(new Item());
}
});
assertEquals(2, itemController.search("Gulost").size());
}
@Test
void searchWithoutResults() {
Mockito.when(restTemplate.getForObject(Mockito.anyString(), Mockito.any())).thenReturn(new ArrayList<Item>());
assertEquals(0, itemController.search("Søkeord som ikke gir noen treff").size());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment