Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.http.ResponseEntity;

import es.codeurjcstudents.pcmod.dto.ComponentDTO;
import es.codeurjcstudents.pcmod.model.Component;
import es.codeurjcstudents.pcmod.service.ComponentsService;

@RestController
Expand All @@ -36,6 +37,14 @@ public List<ComponentDTO> findTop3MoreRecent() {
return componentService.findTop3MoreRecent().stream().map(componentService::toDTO).toList();
}

@GetMapping("/{id}")
public ResponseEntity<ComponentDTO> getComponent (@PathVariable long id) {

Component component = componentService.getComponent(id);

return ResponseEntity.ok(componentService.toDTO(component));
}

@GetMapping("/{id}/image")
public ResponseEntity<Resource> getComponentImage(@PathVariable long id) throws SQLException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.springframework.core.io.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.stereotype.Service;

import es.codeurjcstudents.pcmod.dto.ComponentDTO;
Expand All @@ -22,11 +24,11 @@ public class ComponentsService {

private final ComponentsRepository componentRepository;

@Autowired
private ComponentMapper componentMapper;
private final ComponentMapper componentMapper;

public ComponentsService(ComponentsRepository componentRepository) {
public ComponentsService(ComponentsRepository componentRepository, ComponentMapper componentMapper) {
this.componentRepository = componentRepository;
this.componentMapper = componentMapper;
}

public Page<Component> findAll(Pageable pageable) {
Expand All @@ -41,6 +43,11 @@ public List<Component> findTop3MoreRecent() {
return componentRepository.findTop3ByOrderByIdDesc();
}

public Component getComponent(long id) {
return componentRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Componente no encontrado."));
}

public Resource getImageFile(long id) throws SQLException {

Optional<Image> imageOptional = componentRepository.findImageById(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@ public void getComponents() {
.body("content[0].stock", equalTo(5));
}

@Test
public void getComponent() {
given().header("Content-Type", "application/json")
.when().get("/components/1")
.then().statusCode(200).contentType(ContentType.JSON)
.body("id", notNullValue())
.body("name", equalTo("AMD Ryzen 7 7800X3D"))
.body("type", equalTo("CPU"))
.body("brand", equalTo("AMD"))
.body("price", equalTo(350.0f))
.body("stock", equalTo(5));
}

@Test
public void getComponentNotFound() {
given().header("Content-Type", "application/json")
.when().get("/components/12")
.then().statusCode(404).contentType(ContentType.JSON)
.body("error", equalTo("Not Found"))
.body("message", equalTo("Componente no encontrado."));
}

@Test
public void getComponentImage() throws IOException {
byte[] expectedImage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,120 @@ public void loadErrorTest() {
assertThat(errorMessage).isEqualTo("Hubo un problema al cargar más componentes. Por favor, pruebe de nuevo.");

}

@Test
public void componentDetailRenderTest() {

driver.get("http://localhost:5173/components/1");

wait.until(ExpectedConditions.presenceOfElementLocated(By.id("component-stock")));
String componentStockMessage = driver.findElement(By.id("component-stock")).getText();
assertThat(componentStockMessage).isEqualTo("¡Últimas unidades!");
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("component-name")));
String loadedComponentName = driver.findElement(By.id("component-name")).getText();
assertThat(loadedComponentName).isEqualTo("AMD Ryzen 7 7800X3D");
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("component-description")));
String loadedComponentDescription = driver.findElement(By.id("component-description")).getText();
assertThat(loadedComponentDescription).contains("8 núcleos y 16 hilos ideales para gaming");
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("component-brand-type")));
String loadedComponentBrandType = driver.findElement(By.id("component-brand-type")).getText();
assertThat(loadedComponentBrandType).isEqualTo("AMD | CPU");
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("component-price")));
String loadedComponentPrice = driver.findElement(By.id("component-price")).getText();
assertThat(loadedComponentPrice).isEqualTo("350.00 €");

wait.until(ExpectedConditions.presenceOfElementLocated(By.id("component-reviews-title")));
String loadedComponentReviewsTitle = driver.findElement(By.id("component-reviews-title")).getText();
assertThat(loadedComponentReviewsTitle).isEqualTo("Reseñas del producto:");
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("component-average-rating")));
String loadedComponentAverageRating = driver.findElement(By.id("component-average-rating")).getText();
assertThat(loadedComponentAverageRating).isEqualTo("Puntuación media: N/A");
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("component-no-reviews")));
String loadedComponentNoReviews = driver.findElement(By.id("component-no-reviews")).getText();
assertThat(loadedComponentNoReviews).isEqualTo("No hay reseñas de este producto todavía.");

}

@Test
public void componentDetailRenderNoLastUnitsTest() {

driver.get("http://localhost:5173/components/3");

assertThat(driver.findElements(By.id("component-stock"))).isEmpty();

}

@Test
public void componentDetailRenderLastUnitsTest() {

driver.get("http://localhost:5173/components/1");

wait.until(ExpectedConditions.presenceOfElementLocated(By.id("component-stock")));
String componentStockMessage = driver.findElement(By.id("component-stock")).getText();
assertThat(componentStockMessage).isEqualTo("¡Últimas unidades!");

}

@Test
public void componentDetailOptionsAsNonLoggedInTest() {

driver.get("http://localhost:5173/components/1");

wait.until(ExpectedConditions.presenceOfElementLocated(By.id("add-to-cart-button")));

assertThat(driver.findElements(By.id("add-to-cart-button"))).isNotEmpty();
assertThat(driver.findElements(By.id("edit-component-button"))).isEmpty();
assertThat(driver.findElements(By.id("delete-component-button"))).isEmpty();

}

@Test
public void componentDetailOptionsAsLoggedInNoAdminTest() {

driver.get("http://localhost:5173/login");

wait.until(ExpectedConditions.presenceOfElementLocated(By.id("email")));
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("password")));
driver.findElement(By.id("email")).sendKeys("user@example.com");
driver.findElement(By.id("password")).sendKeys("userpass");

scrollToAndClick(By.name("login-button"));

wait.until(ExpectedConditions.urlToBe("http://localhost:5173/"));

driver.get("http://localhost:5173/components/1");

wait.until(ExpectedConditions.presenceOfElementLocated(By.id("add-to-cart-button")));

assertThat(driver.findElements(By.id("add-to-cart-button"))).isNotEmpty();
assertThat(driver.findElements(By.id("edit-component-button"))).isEmpty();
assertThat(driver.findElements(By.id("delete-component-button"))).isEmpty();

}

@Test
public void componentDetailOptionsAsAdminTest() {

driver.get("http://localhost:5173/login");

wait.until(ExpectedConditions.presenceOfElementLocated(By.id("email")));
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("password")));
driver.findElement(By.id("email")).sendKeys("admin@example.com");
driver.findElement(By.id("password")).sendKeys("adminpass");

scrollToAndClick(By.name("login-button"));

wait.until(ExpectedConditions.urlToBe("http://localhost:5173/"));

driver.get("http://localhost:5173/components/1");

wait.until(ExpectedConditions.presenceOfElementLocated(By.id("add-to-cart-button")));
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("edit-component-button")));
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("delete-component-button")));

assertThat(driver.findElements(By.id("add-to-cart-button"))).isNotEmpty();
assertThat(driver.findElements(By.id("edit-component-button"))).isNotEmpty();
assertThat(driver.findElements(By.id("delete-component-button"))).isNotEmpty();

}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package es.codeurjcstudents.pcmod.integration;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.IOException;
import java.math.BigDecimal;
Expand All @@ -18,7 +20,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;

import org.springframework.web.server.ResponseStatusException;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
Expand Down Expand Up @@ -88,6 +90,27 @@ void loadComponents() {
assertEquals("NVIDIA GeForce RTX 5060 WINDFORCE MAX OC", componentList.getContent().get(1).getName());
}

@Test
public void testGetComponent() {
Component component = componentsRepository.save(new Component("Intel i5 12400F", "Procesador de 6 núcleos y 12 hilos", ComponentType.CPU, "Intel", BigDecimal.valueOf(153.97), 14));

Component loadedComponent = componentsService.getComponent(component.getId());

assertDoesNotThrow(() -> componentsService.getComponent(component.getId()));
assertEquals(component.getId(), loadedComponent.getId());
assertEquals(component.getName(), loadedComponent.getName());
assertEquals(component.getDescription(), loadedComponent.getDescription());
assertEquals(component.getType(), loadedComponent.getType());
assertEquals(component.getBrand(), loadedComponent.getBrand());
assertEquals(component.getPrice(), loadedComponent.getPrice());
assertEquals(component.getStock(), loadedComponent.getStock());
}

@Test
public void testGetComponentNotFound() {
assertThrows(ResponseStatusException.class, () -> componentsService.getComponent(99L));
}

@Test
public void testGetComponentImage() throws IOException, SQLException {
Component component = componentsRepository.findAll().get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -14,14 +15,18 @@
import java.util.List;
import java.util.Optional;
import java.io.IOException;
import java.math.BigDecimal;

import javax.sql.rowset.serial.SerialBlob;

import org.springframework.core.io.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.web.server.ResponseStatusException;

import es.codeurjcstudents.pcmod.dto.ComponentMapper;
import es.codeurjcstudents.pcmod.enums.ComponentType;
import es.codeurjcstudents.pcmod.model.Component;
import es.codeurjcstudents.pcmod.model.Image;
import es.codeurjcstudents.pcmod.repository.ComponentsRepository;
Expand All @@ -33,7 +38,8 @@ public class ComponentsUnitTests {
@Test
public void testComponentsLoad() {
ComponentsRepository componentsRepository = mock(ComponentsRepository.class);
ComponentsService componentsService = new ComponentsService(componentsRepository);
ComponentMapper componentMapper = mock(ComponentMapper.class);
ComponentsService componentsService = new ComponentsService(componentsRepository, componentMapper);
Pageable pageable = mock(Pageable.class);

List<Component> componentList = Arrays.asList(new Component(), new Component());
Expand All @@ -46,10 +52,39 @@ public void testComponentsLoad() {
verify(componentsRepository).findAll(pageable);
}

@Test
public void testGetComponent() {
ComponentsRepository componentsRepository = mock(ComponentsRepository.class);
ComponentMapper componentMapper = mock(ComponentMapper.class);
ComponentsService componentsService = new ComponentsService(componentsRepository, componentMapper);

Component componentToReturn = new Component("Intel i5 12400F", "Procesador de 6 núcleos y 12 hilos", ComponentType.CPU, "Intel", BigDecimal.valueOf(153.97), 14);

when(componentsRepository.findById(1L)).thenReturn(Optional.of(componentToReturn));

Component component = componentsService.getComponent(1L);

assertEquals(componentToReturn, component);
verify(componentsRepository).findById(1L);
}

@Test
public void testGetComponentNotFound() {
ComponentsRepository componentsRepository = mock(ComponentsRepository.class);
ComponentMapper componentMapper = mock(ComponentMapper.class);
ComponentsService componentsService = new ComponentsService(componentsRepository, componentMapper);

when(componentsRepository.findById(12L)).thenReturn(Optional.empty());

assertThrows(ResponseStatusException.class, () -> componentsService.getComponent(12L));
verify(componentsRepository).findById(12L);
}

@Test
public void testGetComponentImage() throws SQLException, IOException {
ComponentsRepository componentsRepository = mock(ComponentsRepository.class);
ComponentsService componentsService = new ComponentsService(componentsRepository);
ComponentMapper componentMapper = mock(ComponentMapper.class);
ComponentsService componentsService = new ComponentsService(componentsRepository, componentMapper);

byte[] originalBytes = new byte[] { 1, 2, 3 };
Image existingImage = new Image();
Expand All @@ -65,7 +100,8 @@ public void testGetComponentImage() throws SQLException, IOException {
@Test
public void testGetNullComponentImage() throws SQLException {
ComponentsRepository componentsRepository = mock(ComponentsRepository.class);
ComponentsService componentsService = new ComponentsService(componentsRepository);
ComponentMapper componentMapper = mock(ComponentMapper.class);
ComponentsService componentsService = new ComponentsService(componentsRepository, componentMapper);

when(componentsRepository.findImageById(1L)).thenReturn(Optional.empty());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Arrays;
import java.util.List;

import es.codeurjcstudents.pcmod.dto.ComponentMapper;
import es.codeurjcstudents.pcmod.enums.ComponentType;
import es.codeurjcstudents.pcmod.model.Component;
import es.codeurjcstudents.pcmod.repository.ComponentsRepository;
Expand All @@ -23,7 +24,8 @@ public class HomePageUnitTests {
public void testRecentComponentsLoad() {

ComponentsRepository componentsRepository = mock(ComponentsRepository.class);
ComponentsService componentsService = new ComponentsService(componentsRepository);
ComponentMapper componentMapper = mock(ComponentMapper.class);
ComponentsService componentsService = new ComponentsService(componentsRepository, componentMapper);

Component component1 = new Component("AMD Ryzen 7 7800X3D",
"8 núcleos y 16 hilos ideales para gaming\r\n" + //
Expand Down
Loading
Loading