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 @@ -55,12 +55,12 @@ public class MatrixImage {
* The {@link CarverMatrix} to which this image belongs.
*/
@ManyToOne
@JoinColumn(name = "matrix_id", nullable = false)
@JoinColumn(name = "matrix_id", nullable = true)
@OnDelete(action = OnDeleteAction.CASCADE)
private CarverMatrix carverMatrix;

@ManyToOne
@JoinColumn(name = "item_id", nullable = false)
@JoinColumn(name = "item_id", nullable = true)
@OnDelete(action = OnDeleteAction.CASCADE)
private CarverItem carverItem;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.fmc.starterApp.repositories;

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.fmc.starterApp.models.entity.MatrixImage;

@Repository
public interface MatrixImageRepository extends JpaRepository<MatrixImage, Long> {
Optional<MatrixImage> findByImageId(Long imageId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ public CarverMatrix getMatrixById(Long matrixId) {
*/
public CarverMatrix createCarverMatrix(CarverMatrix matrix, Long userId) {
User2 user = user2Repository.findById(userId).orElseThrow(() -> new IllegalArgumentException("User not found with ID: " + userId));
if(matrix==null) throw new IllegalArgumentException("CarverMatrix must not be null");

matrix.setUser(user);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.fmc.starterApp.models.entity;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
Expand Down Expand Up @@ -118,8 +120,9 @@ void testAllArgsConstructor() {
eff.put("score", 5);
Map<String, Integer> recog = new HashMap<>();
recog.put("score", 6);
ArrayList<String> images = new ArrayList<String>();

CarverItem item = new CarverItem(10L, matrix, "AllArgsItem", crit, acc, recov, vul, eff, recog, targets, now);
CarverItem item = new CarverItem(10L, matrix, "AllArgsItem", crit, acc, recov, vul, eff, recog, targets, images, now);

assertEquals(10L, item.getItemId());
assertEquals(matrix, item.getCarverMatrix());
Expand Down Expand Up @@ -167,7 +170,7 @@ void testAllArgsConstructorNonNullEnforcement() {

// Passing null for itemName should trigger a NullPointerException.
NullPointerException ex = assertThrows(NullPointerException.class, () ->
new CarverItem(1L, matrix, null, new HashMap<>(), new HashMap<>(), new HashMap<>(), new HashMap<>(), new HashMap<>(), new HashMap<>(), targets, now));
new CarverItem(1L, matrix, null, new HashMap<>(), new HashMap<>(), new HashMap<>(), new HashMap<>(), new HashMap<>(), new HashMap<>(), targets, new ArrayList<String>(),now));
assertTrue(ex.getMessage().contains("itemName"));
}

Expand Down Expand Up @@ -233,8 +236,9 @@ void testToStringMethod() {
eff.put("score", 2);
Map<String, Integer> recog = new HashMap<>();
recog.put("score", 2);
ArrayList<String> images = new ArrayList<String>();

CarverItem item = new CarverItem(20L, matrix, "ToStringItem", crit, acc, recov, vul, eff, recog, targets, now);
CarverItem item = new CarverItem(20L, matrix, "ToStringItem", crit, acc, recov, vul, eff, recog, targets, images, now);
String str = item.toString();
assertNotNull(str);
assertTrue(str.contains("ToStringItem"), "toString() should include the itemName");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -73,6 +74,7 @@ void testAllArgsConstructor() {
String[] hosts = { "[email protected]", "[email protected]" };
String[] participants = { "[email protected]" };
List<CarverItem> items = new ArrayList<>(); // Start with an empty list.
ArrayList<Map<String, Object>> images = new ArrayList<>();

// Create a dummy User2.
User2 user = new User2();
Expand All @@ -81,7 +83,7 @@ void testAllArgsConstructor() {
user.setEmail("[email protected]");

CarverMatrix matrix = new CarverMatrix(2L, user, "Matrix AllArgs", "Detailed description for matrix.", now, hosts, participants, items,
1.0, 1.2, 0.8, 0.5, 1.1, 0.9, true, false, true);
1.0, 1.2, 0.8, 0.5, 1.1, 0.9, true, false, true,images);

assertEquals(2L, matrix.getMatrixId());
assertEquals(user, matrix.getUser());
Expand Down Expand Up @@ -127,6 +129,7 @@ void testAllArgsConstructorNonNullEnforcement() {
String[] hosts = { "[email protected]" };
String[] participants = { "[email protected]" };
List<CarverItem> items = new ArrayList<>();
ArrayList<Map<String, Object>> images = new ArrayList<>();

// We no longer enforce a non-null check on user, so passing null for user should work.
CarverMatrix matrixWithNullUser = new CarverMatrix(
Expand All @@ -139,7 +142,7 @@ void testAllArgsConstructorNonNullEnforcement() {
participants,
items,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
false, false, false
false, false, false, images
);
// Assert that matrix is created with a null user.
assertNull(matrixWithNullUser.getUser(), "User should be allowed to be null");
Expand All @@ -151,7 +154,7 @@ void testAllArgsConstructorNonNullEnforcement() {
dummyUser.setEmail("[email protected]");
NullPointerException ex = assertThrows(NullPointerException.class, () ->
new CarverMatrix(1L, dummyUser, null, "Valid Description", now, hosts, participants, items,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, false, false, false)
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, false, false, false,images)
);
assertTrue(ex.getMessage().contains("name"));
}
Expand Down Expand Up @@ -217,11 +220,12 @@ void testEqualsAndHashCodeBehavior() {
user.setUserId(50L);
user.setUsername("TestUser");
user.setEmail("[email protected]");
ArrayList<Map<String, Object>> images = new ArrayList<>();

CarverMatrix matrix1 = new CarverMatrix(1L, user, "Matrix", "Description", now, hosts, participants, items,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, true, false, true);
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, true, false, true, images);
CarverMatrix matrix2 = new CarverMatrix(1L, user, "Matrix", "Description", now, hosts, participants, items,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, true, false, true);
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, true, false, true, images);

// Expect that matrix1 and matrix2 are not equal because they are distinct objects.
assertNotEquals(matrix1, matrix2, "Different instances with identical fields should not be equal");
Expand All @@ -238,13 +242,15 @@ void testToStringMethod() {
String[] hosts = { "[email protected]" };
String[] participants = { "[email protected]" };
List<CarverItem> items = new ArrayList<>();
ArrayList<Map<String, Object>> images = new ArrayList<>();

User2 user = new User2();
user.setUserId(60L);
user.setUsername("ToStringUser");
user.setEmail("[email protected]");

CarverMatrix matrix = new CarverMatrix(2L, user, "ToStringMatrix", "Test Description", now, hosts, participants, items,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, false, true, false);
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, false, true, false, images);
String str = matrix.toString();
assertNotNull(str, "toString() should not return null");
// Check that key fields appear in the output.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.fmc.starterApp.models.entity;

import static org.junit.jupiter.api.Assertions.*;

import java.time.LocalDateTime;
import java.util.Set;

import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;

/**
Expand Down Expand Up @@ -64,7 +63,7 @@ void testAllArgsConstructor() {
matrix.setName("Matrix2");

LocalDateTime now = LocalDateTime.now();
MatrixImage image = new MatrixImage(10L, matrix, "http://example.com/image.png", now);
MatrixImage image = new MatrixImage(10L, matrix, null, "http://example.com/image.png", now);

assertEquals(10L, image.getImageId());
assertEquals(matrix, image.getCarverMatrix());
Expand Down Expand Up @@ -98,7 +97,7 @@ void testAllArgsConstructorNonNullEnforcement() {

// Passing null for imageUrl should trigger a NullPointerException.
NullPointerException ex = assertThrows(NullPointerException.class, () ->
new MatrixImage(1L, matrix, null, now));
new MatrixImage(1L, matrix, null, null, now));
assertTrue(ex.getMessage().contains("imageUrl"));
}

Expand Down Expand Up @@ -159,7 +158,7 @@ void testToStringMethod() {
matrix.setMatrixId(5L);
matrix.setName("MatrixTest");
LocalDateTime now = LocalDateTime.now();
MatrixImage image = new MatrixImage(20L, matrix, "http://example.com/image.png", now);
MatrixImage image = new MatrixImage(20L, matrix, null, "http://example.com/image.png", now);
String str = image.toString();
assertNotNull(str, "toString() should not return null");
assertTrue(str.contains("http://example.com/image.png"), "toString() should include the imageUrl");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ void testBulkOperationsForMatrixImages() {
assertThat(images.size()).isGreaterThanOrEqualTo(2);

matrixImageRepository.deleteAll(List.of(image1, image2));
Optional<MatrixImage> fetched1 = matrixImageRepository.findById(image1.getImageId());
Optional<MatrixImage> fetched2 = matrixImageRepository.findById(image2.getImageId());
Optional<MatrixImage> fetched1 = matrixImageRepository.findByImageId(image1.getImageId());
Optional<MatrixImage> fetched2 = matrixImageRepository.findByImageId(image2.getImageId());
assertThat(fetched1).isNotPresent();
assertThat(fetched2).isNotPresent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,25 +250,29 @@ void testCreateCarverMatrix_BasicFunctionality() {
*/
@Test
void testCreateCarverMatrix_NullMatrix() {
User2 user = new User2(null, "create-001", "Create", "User", "Create User", "createuser", "[email protected]", null);
user = user2Repository.save(user);
Long userId = user.getUserId();

IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> carverMatrixService.createCarverMatrix(null, 1L),
() -> carverMatrixService.createCarverMatrix(null, userId),
"Expected createCarverMatrix to throw IllegalArgumentException for null matrix");
assertThat(ex.getMessage()).contains("CarverMatrix must not be null");
}

/**
* **createCarverMatrix - Null UserId Input Test**
* Verify that passing a null userId to createCarverMatrix throws an IllegalArgumentException.
*/
@Test
void testCreateCarverMatrix_NullUserId() {
CarverMatrix matrix = new CarverMatrix();
matrix.setName("Matrix with Null User");
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> carverMatrixService.createCarverMatrix(matrix, null),
"Expected createCarverMatrix to throw IllegalArgumentException for null userId");
assertThat(ex.getMessage()).contains("UserId must not be null");
}
// /**
// * **createCarverMatrix - Null UserId Input Test**
// * Verify that passing a null userId to createCarverMatrix throws an IllegalArgumentException.
// */
// @Test
// void testCreateCarverMatrix_NullUserId() {
// CarverMatrix matrix = new CarverMatrix();
// matrix.setName("Matrix with Null User");
// IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
// () -> carverMatrixService.createCarverMatrix(matrix, null),
// "Expected createCarverMatrix to throw IllegalArgumentException for null userId");
// assertThat(ex.getMessage()).contains("UserId must not be null");
// }

// =========================================================================
// ✅ 3. createCarverMatrix's Transactional and Integration Tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void testUploadImage_ValidInput() throws IOException {
MediaType.IMAGE_JPEG_VALUE, "image content".getBytes());

// Act: call uploadImage.
String url = imageService.uploadImage(file, matrix.getMatrixId());
String url = imageService.uploadImage(file, matrix.getMatrixId(),null);

// Assert: verify that the URL is non-null.
assertNotNull(url, "Expected a non-null URL for the uploaded image");
Expand All @@ -106,7 +106,7 @@ void testUploadImage_ValidInput() throws IOException {
@Test
void testUploadImage_NullFile() {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> imageService.uploadImage(null, 1L),
() -> imageService.uploadImage(null, 1L,null),
"Expected uploadImage to throw IllegalArgumentException for null file");
assertThat(ex.getMessage()).contains("MultipartFile must not be null");
}
Expand All @@ -120,7 +120,7 @@ void testUploadImage_NullMatrixId() {
MockMultipartFile file = new MockMultipartFile("file", "test.jpg",
MediaType.IMAGE_JPEG_VALUE, "content".getBytes());
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> imageService.uploadImage(file, null),
() -> imageService.uploadImage(file,null, null),
"Expected uploadImage to throw IllegalArgumentException for null matrixId");
assertThat(ex.getMessage()).contains("MatrixId must not be null");
}
Expand All @@ -135,7 +135,7 @@ void testUploadImage_InvalidFileName() {
MockMultipartFile file = new MockMultipartFile("file", "",
MediaType.IMAGE_JPEG_VALUE, "content".getBytes());
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> imageService.uploadImage(file, 1L),
() -> imageService.uploadImage(file, 1L, null),
"Expected uploadImage to throw IllegalArgumentException for invalid file name");
assertThat(ex.getMessage()).contains("Invalid file name");
}
Expand Down Expand Up @@ -163,7 +163,7 @@ void testUploadImage_FilenameCleaning() throws IOException {
MediaType.IMAGE_JPEG_VALUE, "dummy content".getBytes());

// Act: call uploadImage.
String url = imageService.uploadImage(file, matrix.getMatrixId());
String url = imageService.uploadImage(file, matrix.getMatrixId(), null);

// Assert: the URL should contain the cleaned filename (invalid characters replaced by underscores).
assertNotNull(url);
Expand Down Expand Up @@ -192,7 +192,7 @@ void testUploadImage_S3UploadFailure() throws IOException {

// Act & Assert: expect a RuntimeException.
RuntimeException ex = assertThrows(RuntimeException.class,
() -> imageService.uploadImage(file, savedMatrix.getMatrixId()),
() -> imageService.uploadImage(file, savedMatrix.getMatrixId(), null),
"Expected uploadImage to throw RuntimeException when S3 upload fails");
assertThat(ex.getMessage()).contains("Failed to upload file to S3");
}
Expand All @@ -218,7 +218,7 @@ void testUploadImage_EndToEndIntegration() throws IOException {
MediaType.IMAGE_JPEG_VALUE, "integration content".getBytes());

// Act: upload the image.
String url = imageService.uploadImage(file, matrixSaved.getMatrixId());
String url = imageService.uploadImage(file, matrixSaved.getMatrixId(), null);

// Assert: verify the returned URL is non-null and that a MatrixImage is saved.
assertNotNull(url);
Expand All @@ -238,7 +238,7 @@ void testUploadImage_EndToEndIntegration() throws IOException {
@Test
void testUploadImage_ThrowsExceptionForNullFile() {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> imageService.uploadImage(null, 1L),
() -> imageService.uploadImage(null, 1L, null),
"Expected uploadImage to throw IllegalArgumentException for null file");
assertThat(ex.getMessage()).contains("MultipartFile must not be null");
}
Expand All @@ -252,7 +252,7 @@ void testUploadImage_ThrowsExceptionForNullMatrixId() {
MockMultipartFile file = new MockMultipartFile("file", "test.jpg",
MediaType.IMAGE_JPEG_VALUE, "content".getBytes());
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> imageService.uploadImage(file, null),
() -> imageService.uploadImage(file, null, null),
"Expected uploadImage to throw IllegalArgumentException for null matrixId");
assertThat(ex.getMessage()).contains("MatrixId must not be null");
}
Expand All @@ -279,7 +279,7 @@ void testUploadImage_UnexpectedErrorHandling() throws IOException {

// Act & Assert.
RuntimeException ex = assertThrows(RuntimeException.class,
() -> imageService.uploadImage(file, matrixSaved.getMatrixId()),
() -> imageService.uploadImage(file, matrixSaved.getMatrixId(), null),
"Expected uploadImage to throw RuntimeException for unexpected errors");
assertThat(ex.getMessage()).contains("Failed to upload file to S3");
}
Expand Down