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

Merge branch 'unitTest/Product' into 'main'

test and update for product util

See merge request idatt2106-v23-03/backend!20
parents 520b01d0 f77b1149
No related branches found
No related tags found
No related merge requests found
package ntnu.idatt2016.v233.SmartMat.model.product;
import lombok.Builder;
/**
* Product is a record class representing a product in the system.
* All other info about the product is fetched from api call on fronted.
......@@ -8,5 +10,6 @@ package ntnu.idatt2016.v233.SmartMat.model.product;
* @version 1.0
* @since 04.04.2023
*/
@Builder
public record Product (int ean, String name, String description){
}
......@@ -34,12 +34,30 @@ 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(words.get(i - 1) + unit);
}
}
volume = words.stream().filter(word -> Arrays.stream(VOLUME_UNITS).anyMatch(word::contains))
.filter(ProductUtil::hasNumbers)
.findAny()
.orElse("");
if (!volume.equals("")){
return Optional.of(volume);
}
}
}
return Optional.empty();
}
/**
* Checks if a string contains any numbers
* @param s The string to check
* @return True if the string contains any numbers, false otherwise
*/
private static boolean hasNumbers(String s) {
return s.matches(".*\\d.*");
}
}
package ntnu.idatt2016.v233.SmartMat.util;
public class tempUtil {
}
package ntnu.idatt2016.v233.SmartMat.util;
import ntnu.idatt2016.v233.SmartMat.model.product.Product;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.Console;
import static org.junit.jupiter.api.Assertions.*;
class ProductUtilTest {
Product product;
@BeforeEach
void setUp() {
this.product = Product.builder()
.ean(123456789)
.name("Pepsi Original 24x500 ml")
.description("Pepsi Original 24x500 ml")
.build();
}
@Test
void getVolumeFromProduct() {
assertEquals("24x500ml", ProductUtil.getVolumeFromProduct(product).get());
this.product = Product.builder()
.ean(123456789)
.name("test")
.description("Pepsi Original 24x500 ml")
.build();
assertEquals("24x500ml", ProductUtil.getVolumeFromProduct(product).get());
this.product = Product.builder()
.ean(123456789)
.name("Pepsi Original 24x500ml")
.description("test")
.build();
assertEquals("24x500ml", ProductUtil.getVolumeFromProduct(product).get());
this.product = Product.builder()
.ean(123456789)
.name("test")
.description("Pepsi Original 24x500ml")
.build();
assertEquals("24x500ml", 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