Skip to content
Snippets Groups Projects
Commit e93191a2 authored by Birk Øvstetun Narvhus's avatar Birk Øvstetun Narvhus
Browse files

change returnvalue of product volum util to list

parent 1430b0c4
No related branches found
No related tags found
No related merge requests found
......@@ -5,10 +5,7 @@ import ntnu.idatt2016.v233.SmartMat.dto.request.ProductRequest;
import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
import ntnu.idatt2016.v233.SmartMat.service.product.ProductService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
/**
* The product controller is responsible for handling requests related to products.
......@@ -47,4 +44,18 @@ public class ProductController {
}
/**
* Returns a product with the given ean.
* @param ean The ean of the product to be returned.
* @return The product with the given ean.
*/
@GetMapping("ean/{ean}")
public ResponseEntity<Product> getProduct(@PathVariable long ean) {
return productService.getProductById(ean)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
}
......@@ -76,7 +76,7 @@ public class ProductService {
* @param id The id of the product to get the volume from
* @return The volume of the product, if it exists
*/
public Optional<String> getProductVolume(long id) {
public Optional<List<String>> getProductVolume(long id) {
if(productRepository.findById(id).isEmpty())
return Optional.empty();
......
......@@ -5,6 +5,7 @@ import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
/**
* Utility class for products
......@@ -26,7 +27,7 @@ public class ProductUtil {
* @param product The product to get the volume from
* @return The volume of the product, if it exists
*/
public static Optional<String> getVolumeFromProduct(Product product) {
public static Optional<List<String>> getVolumeFromProduct(Product product) {
for (String desc : Arrays.asList(product.getName(), product.getDescription())) {
List<String> words = List.of(desc.split(" "));
if (words.size() > 1) {
......@@ -34,16 +35,25 @@ public class ProductUtil {
for (String unit : VOLUME_UNITS) {
int i = words.indexOf(unit);
if (i != -1) {
return Optional.of(words.get(i - 1) + unit);
return Optional.of(List.of(words.get(i - 1), unit));
}
}
volume = words.stream().filter(word -> Arrays.stream(VOLUME_UNITS).anyMatch(word::contains))
volume = words.stream().map(word -> Arrays.stream(VOLUME_UNITS).map(unit -> {
int index = word.indexOf(unit);
if (index == -1) {
if (!Pattern.matches("[a-zA-Z]+", word) && ProductUtil.hasNumbers(word)) {
return word;
}
return "";
}
return word.substring(0, index) + " " + word.substring(index);
}).findAny().orElse(""))
.filter(ProductUtil::hasNumbers)
.findAny()
.orElse("");
if (!volume.equals("")){
return Optional.of(volume);
return Optional.of(List.of(volume.split(" ")));
}
}
}
......
......@@ -133,9 +133,9 @@ public class ProductServiceTest {
ProductService productService = new ProductService(mockRepository);
// Verify that the service returns the correct volume
Optional<String> returnedVolume = productService.getProductVolume(productId);
Optional<List<String>> returnedVolume = productService.getProductVolume(productId);
assertTrue(returnedVolume.isPresent());
assertEquals("500ml", returnedVolume.get());
assertEquals(List.of("500", "ml"), returnedVolume.get());
}
@Test
......@@ -150,7 +150,7 @@ public class ProductServiceTest {
ProductService productService = new ProductService(mockRepository);
// Verify that the service returns an empty optional
Optional<String> returnedVolume = productService.getProductVolume(productId);
Optional<List<String>> returnedVolume = productService.getProductVolume(productId);
assertTrue(returnedVolume.isEmpty());
}
......
......@@ -4,6 +4,8 @@ import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class ProductUtilTest {
......@@ -20,7 +22,7 @@ class ProductUtilTest {
@Test
void getVolumeFromProduct() {
assertEquals("24x500ml", ProductUtil.getVolumeFromProduct(product).get());
assertEquals(List.of("24x500", "ml"), ProductUtil.getVolumeFromProduct(product).get());
this.product = Product.builder()
.ean(123456789)
......@@ -28,7 +30,7 @@ class ProductUtilTest {
.description("Pepsi Original 24x500 ml")
.build();
assertEquals("24x500ml", ProductUtil.getVolumeFromProduct(product).get());
assertEquals(List.of("24x500", "ml"), ProductUtil.getVolumeFromProduct(product).get());
this.product = Product.builder()
.ean(123456789)
......@@ -37,7 +39,7 @@ class ProductUtilTest {
.build();
assertEquals("24x500ml", ProductUtil.getVolumeFromProduct(product).get());
assertEquals(List.of("24x500", "ml"), ProductUtil.getVolumeFromProduct(product).get());
this.product = Product.builder()
.ean(123456789)
......@@ -46,6 +48,6 @@ class ProductUtilTest {
.build();
assertEquals("24x500ml", ProductUtil.getVolumeFromProduct(product).get());
assertEquals(List.of("24x500", "ml"), ProductUtil.getVolumeFromProduct(product).get());
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment