-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Update Spring Boot, Java version, and add tests #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/main/java/com/onurdesk/iris/dto/QrCodeGenerationRequestDto.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
src/test/java/com/onurdesk/iris/service/QrCodeServiceTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,197 @@ | ||||||
package com.onurdesk.iris.service; | ||||||
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper; | ||||||
import com.google.zxing.BarcodeFormat; | ||||||
import com.google.zxing.WriterException; | ||||||
import com.google.zxing.client.j2se.MatrixToImageWriter; | ||||||
import com.google.zxing.common.BitMatrix; | ||||||
import com.google.zxing.qrcode.QRCodeWriter; | ||||||
import com.onurdesk.iris.dto.QrCodeGenerationRequestDto; | ||||||
import org.junit.jupiter.api.BeforeEach; | ||||||
import org.junit.jupiter.api.Test; | ||||||
import org.junit.jupiter.api.extension.ExtendWith; | ||||||
import org.mockito.ArgumentCaptor; | ||||||
import org.mockito.InjectMocks; | ||||||
import org.mockito.Mock; | ||||||
import org.mockito.MockedStatic; | ||||||
import org.mockito.Mockito; | ||||||
import org.mockito.junit.jupiter.MockitoExtension; | ||||||
import org.springframework.http.HttpHeaders; | ||||||
import org.springframework.http.HttpStatus; | ||||||
import org.springframework.http.ResponseEntity; | ||||||
import org.springframework.mock.web.MockHttpServletResponse; | ||||||
import org.springframework.web.multipart.MultipartFile; | ||||||
|
||||||
import javax.imageio.ImageIO; | ||||||
import jakarta.servlet.ServletOutputStream; | ||||||
import java.awt.image.BufferedImage; | ||||||
import java.io.ByteArrayInputStream; | ||||||
import java.io.ByteArrayOutputStream; | ||||||
import java.io.IOException; | ||||||
import java.io.InputStream; | ||||||
|
||||||
import static org.junit.jupiter.api.Assertions.*; | ||||||
import static org.mockito.ArgumentMatchers.any; | ||||||
import static org.mockito.ArgumentMatchers.anyInt; | ||||||
import static org.mockito.ArgumentMatchers.anyString; | ||||||
import static org.mockito.Mockito.*; | ||||||
|
||||||
@ExtendWith(MockitoExtension.class) | ||||||
class QrCodeServiceTests { | ||||||
|
||||||
@InjectMocks | ||||||
private QrCodeService qrCodeService; | ||||||
|
||||||
private QrCodeGenerationRequestDto sampleDto; | ||||||
private ObjectMapper objectMapper = new ObjectMapper(); | ||||||
|
||||||
@BeforeEach | ||||||
void setUp() { | ||||||
sampleDto = QrCodeGenerationRequestDto.builder() | ||||||
.title("Test QR") | ||||||
.message("This is a test payload") // Assuming 'payload' maps to 'message' in DTO based on schema | ||||||
.generatedByName("JUnit") // Assuming 'generatedBy' maps to 'generatedByName' | ||||||
.generatedForName("Test Target") // Adding a value for this field | ||||||
.build(); | ||||||
} | ||||||
|
||||||
@Test | ||||||
void testGenerateQrCode_success() throws IOException, WriterException { | ||||||
MockHttpServletResponse mockResponse = new MockHttpServletResponse(); | ||||||
|
||||||
qrCodeService.generate(sampleDto, mockResponse); | ||||||
|
||||||
assertEquals("attachment;filename=Test_QR.png", mockResponse.getHeader(HttpHeaders.CONTENT_DISPOSITION)); | ||||||
assertEquals("image/png", mockResponse.getContentType()); // MatrixToImageWriter sets this implicitly | ||||||
|
||||||
// Verify the output stream contains PNG data (basic check for non-empty) | ||||||
assertTrue(mockResponse.getContentAsByteArray().length > 0); | ||||||
|
||||||
// Further verification could involve trying to read the byte array as an image | ||||||
// and potentially decoding it, but that might be too much for a unit test. | ||||||
// For now, we trust that if MatrixToImageWriter.writeToStream ran without error | ||||||
// and produced bytes, it's likely correct. | ||||||
|
||||||
// We can also try to verify the content of the QR code if we mock the writer part. | ||||||
// Let's try to capture the string passed to the QRCodeWriter.encode | ||||||
// This requires QRCodeWriter to be a mock or using a static mock for MatrixToImageWriter | ||||||
// For simplicity, the current check on headers and non-empty output is a good start. | ||||||
} | ||||||
|
||||||
@Test | ||||||
void testGenerateQrCode_nullDto() { | ||||||
MockHttpServletResponse mockResponse = new MockHttpServletResponse(); | ||||||
assertThrows(NullPointerException.class, () -> { | ||||||
// The ObjectMapper().writeValueAsString(null) will throw NullPointerException | ||||||
qrCodeService.generate(null, mockResponse); | ||||||
}); | ||||||
} | ||||||
|
||||||
@Test | ||||||
void testGenerateQrCode_emptyTitleInDto() throws IOException, WriterException { | ||||||
MockHttpServletResponse mockResponse = new MockHttpServletResponse(); | ||||||
QrCodeGenerationRequestDto dtoWithEmptyTitle = QrCodeGenerationRequestDto.builder() | ||||||
.title("") // Empty title | ||||||
.message("Some payload") | ||||||
.generatedByName("JUnit") | ||||||
.generatedForName("Test Target") | ||||||
.build(); | ||||||
|
||||||
qrCodeService.generate(dtoWithEmptyTitle, mockResponse); | ||||||
|
||||||
// Expecting "attachment;filename=.png" or similar, depending on implementation logic for empty title | ||||||
assertEquals("attachment;filename=.png", mockResponse.getHeader(HttpHeaders.CONTENT_DISPOSITION)); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] Consider handling empty title values explicitly—either by validating the DTO or providing a default filename—to avoid generating a filename like ".png".
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
assertTrue(mockResponse.getContentAsByteArray().length > 0); | ||||||
} | ||||||
|
||||||
|
||||||
@Test | ||||||
void testReadQrCode_success() throws Exception { | ||||||
// 1. Prepare a valid QR code image as byte array | ||||||
String originalContent = objectMapper.writeValueAsString(sampleDto); | ||||||
QRCodeWriter qrCodeWriter = new QRCodeWriter(); | ||||||
BitMatrix bitMatrix = qrCodeWriter.encode(originalContent, BarcodeFormat.QR_CODE, 200, 200); | ||||||
ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream(); | ||||||
MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream); | ||||||
byte[] qrCodeBytes = pngOutputStream.toByteArray(); | ||||||
|
||||||
// 2. Mock MultipartFile | ||||||
MultipartFile multipartFile = mock(MultipartFile.class); | ||||||
when(multipartFile.getInputStream()).thenReturn(new ByteArrayInputStream(qrCodeBytes)); | ||||||
|
||||||
// 3. Call read method | ||||||
ResponseEntity<?> responseEntity = qrCodeService.read(multipartFile); | ||||||
|
||||||
// 4. Assertions | ||||||
assertNotNull(responseEntity); | ||||||
assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); | ||||||
assertTrue(responseEntity.getBody() instanceof QrCodeGenerationRequestDto); | ||||||
QrCodeGenerationRequestDto resultDto = (QrCodeGenerationRequestDto) responseEntity.getBody(); | ||||||
assertEquals(sampleDto.getTitle(), resultDto.getTitle()); | ||||||
assertEquals(sampleDto.getMessage(), resultDto.getMessage()); // Changed from getPayload to getMessage | ||||||
assertEquals(sampleDto.getGeneratedByName(), resultDto.getGeneratedByName()); // Changed from getGeneratedBy | ||||||
} | ||||||
|
||||||
@Test | ||||||
void testReadQrCode_invalidImageFormat() throws IOException { | ||||||
MultipartFile multipartFile = mock(MultipartFile.class); | ||||||
// Simulate a file that is not a valid image (e.g., random bytes) | ||||||
byte[] invalidImageBytes = "This is not an image".getBytes(); | ||||||
when(multipartFile.getInputStream()).thenReturn(new ByteArrayInputStream(invalidImageBytes)); | ||||||
|
||||||
// ImageIO.read is expected to return null for non-image formats it doesn't understand | ||||||
// which will then cause NullPointerException in BufferedImageLuminanceSource constructor | ||||||
assertThrows(NullPointerException.class, () -> { | ||||||
qrCodeService.read(multipartFile); | ||||||
}, "Should throw NullPointerException when ImageIO.read returns null for invalid image format that is not decodable by ImageIO"); | ||||||
} | ||||||
|
||||||
@Test | ||||||
void testReadQrCode_notAQrCode() throws IOException { | ||||||
// Create a valid PNG image, but not a QR code (e.g., a blank image) | ||||||
BufferedImage blankImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); | ||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||||||
ImageIO.write(blankImage, "png", baos); | ||||||
byte[] blankImageBytes = baos.toByteArray(); | ||||||
|
||||||
MultipartFile multipartFile = mock(MultipartFile.class); | ||||||
when(multipartFile.getInputStream()).thenReturn(new ByteArrayInputStream(blankImageBytes)); | ||||||
|
||||||
// Expect NotFoundException because MultiFormatReader won't find a QR code | ||||||
assertThrows(com.google.zxing.NotFoundException.class, () -> { | ||||||
qrCodeService.read(multipartFile); | ||||||
}); | ||||||
} | ||||||
|
||||||
@Test | ||||||
void testReadQrCode_ioExceptionOnInputStream() throws IOException { | ||||||
MultipartFile multipartFile = mock(MultipartFile.class); | ||||||
when(multipartFile.getInputStream()).thenThrow(new IOException("Failed to read input stream")); | ||||||
|
||||||
assertThrows(IOException.class, () -> { | ||||||
qrCodeService.read(multipartFile); | ||||||
}); | ||||||
} | ||||||
|
||||||
@Test | ||||||
void testReadQrCode_unexpectedContent() throws Exception { | ||||||
// 1. Prepare a QR code with content that is not a valid JSON for QrCodeGenerationRequestDto | ||||||
String nonJsonContent = "Just some plain text, not JSON"; | ||||||
QRCodeWriter qrCodeWriter = new QRCodeWriter(); | ||||||
BitMatrix bitMatrix = qrCodeWriter.encode(nonJsonContent, BarcodeFormat.QR_CODE, 200, 200); | ||||||
ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream(); | ||||||
MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream); | ||||||
byte[] qrCodeBytes = pngOutputStream.toByteArray(); | ||||||
|
||||||
// 2. Mock MultipartFile | ||||||
MultipartFile multipartFile = mock(MultipartFile.class); | ||||||
when(multipartFile.getInputStream()).thenReturn(new ByteArrayInputStream(qrCodeBytes)); | ||||||
|
||||||
// 3. Call read method and expect a Jackson mapping/parsing exception | ||||||
// The service tries to map result.getText() to QrCodeGenerationRequestDto.class | ||||||
// This will fail if the text is not a JSON representation of that DTO. | ||||||
assertThrows(com.fasterxml.jackson.core.JsonProcessingException.class, () -> { // Changed to broader JsonProcessingException | ||||||
qrCodeService.read(multipartFile); | ||||||
}); | ||||||
} | ||||||
} |
57 changes: 57 additions & 0 deletions
57
target/classes/META-INF/spring-configuration-metadata.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{ | ||
"groups": [ | ||
{ | ||
"name": "com.onurdesk.iris.swagger", | ||
"type": "com.onurdesk.iris.configuration.properties.OpenApiConfigurationProperties", | ||
"sourceType": "com.onurdesk.iris.configuration.properties.OpenApiConfigurationProperties" | ||
}, | ||
{ | ||
"name": "com.onurdesk.iris.swagger.properties", | ||
"type": "com.onurdesk.iris.configuration.properties.OpenApiConfigurationProperties$Properties", | ||
"sourceType": "com.onurdesk.iris.configuration.properties.OpenApiConfigurationProperties", | ||
"sourceMethod": "getProperties()" | ||
}, | ||
{ | ||
"name": "com.onurdesk.iris.swagger.properties.contact", | ||
"type": "com.onurdesk.iris.configuration.properties.OpenApiConfigurationProperties$Properties$Contact", | ||
"sourceType": "com.onurdesk.iris.configuration.properties.OpenApiConfigurationProperties$Properties", | ||
"sourceMethod": "getContact()" | ||
} | ||
], | ||
"properties": [ | ||
{ | ||
"name": "com.onurdesk.iris.swagger.properties.api-version", | ||
"type": "java.lang.String", | ||
"sourceType": "com.onurdesk.iris.configuration.properties.OpenApiConfigurationProperties$Properties" | ||
}, | ||
{ | ||
"name": "com.onurdesk.iris.swagger.properties.contact.email", | ||
"type": "java.lang.String", | ||
"sourceType": "com.onurdesk.iris.configuration.properties.OpenApiConfigurationProperties$Properties$Contact" | ||
}, | ||
{ | ||
"name": "com.onurdesk.iris.swagger.properties.contact.name", | ||
"type": "java.lang.String", | ||
"sourceType": "com.onurdesk.iris.configuration.properties.OpenApiConfigurationProperties$Properties$Contact" | ||
}, | ||
{ | ||
"name": "com.onurdesk.iris.swagger.properties.contact.url", | ||
"type": "java.lang.String", | ||
"sourceType": "com.onurdesk.iris.configuration.properties.OpenApiConfigurationProperties$Properties$Contact" | ||
}, | ||
{ | ||
"name": "com.onurdesk.iris.swagger.properties.description", | ||
"type": "java.lang.String", | ||
"sourceType": "com.onurdesk.iris.configuration.properties.OpenApiConfigurationProperties$Properties" | ||
}, | ||
{ | ||
"name": "com.onurdesk.iris.swagger.properties.title", | ||
"type": "java.lang.String", | ||
"sourceType": "com.onurdesk.iris.configuration.properties.OpenApiConfigurationProperties$Properties" | ||
} | ||
], | ||
"hints": [], | ||
"ignored": { | ||
"properties": [] | ||
} | ||
} |
Binary file modified
BIN
+0 Bytes
(100%)
target/classes/com/onurdesk/iris/SpringBootQrCodeGeneratorReaderApplication.class
Binary file not shown.
Binary file modified
BIN
+485 Bytes
(120%)
target/classes/com/onurdesk/iris/configuration/OpenApiConfiguration.class
Binary file not shown.
Binary file modified
BIN
+2.34 KB
(340%)
...esk/iris/configuration/properties/OpenApiConfigurationProperties$Properties$Contact.class
Binary file not shown.
Binary file modified
BIN
+3.12 KB
(360%)
...om/onurdesk/iris/configuration/properties/OpenApiConfigurationProperties$Properties.class
Binary file not shown.
Binary file modified
BIN
+1.8 KB
(280%)
...t/classes/com/onurdesk/iris/configuration/properties/OpenApiConfigurationProperties.class
Binary file not shown.
Binary file modified
BIN
-14 Bytes
(99%)
target/classes/com/onurdesk/iris/controller/QrCodeController.class
Binary file not shown.
Binary file added
BIN
+2.25 KB
.../com/onurdesk/iris/dto/QrCodeGenerationRequestDto$QrCodeGenerationRequestDtoBuilder.class
Binary file not shown.
Binary file modified
BIN
+1.06 KB
(170%)
target/classes/com/onurdesk/iris/dto/QrCodeGenerationRequestDto.class
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
target/classes/com/onurdesk/iris/exception/handler/GenericExceptionHandler.class
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
target/classes/com/onurdesk/iris/exception/handler/ValidationFailureExceptionHandler.class
Binary file not shown.
Binary file modified
BIN
+1.74 KB
(170%)
target/classes/com/onurdesk/iris/service/QrCodeService.class
Binary file not shown.
2 changes: 2 additions & 0 deletions
2
target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
META-INF/spring-configuration-metadata.json | ||
com/onurdesk/iris/dto/QrCodeGenerationRequestDto$QrCodeGenerationRequestDtoBuilder.class |
8 changes: 8 additions & 0 deletions
8
target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/app/src/main/java/com/onurdesk/iris/SpringBootQrCodeGeneratorReaderApplication.java | ||
/app/src/main/java/com/onurdesk/iris/configuration/OpenApiConfiguration.java | ||
/app/src/main/java/com/onurdesk/iris/configuration/properties/OpenApiConfigurationProperties.java | ||
/app/src/main/java/com/onurdesk/iris/controller/QrCodeController.java | ||
/app/src/main/java/com/onurdesk/iris/dto/QrCodeGenerationRequestDto.java | ||
/app/src/main/java/com/onurdesk/iris/exception/handler/GenericExceptionHandler.java | ||
/app/src/main/java/com/onurdesk/iris/exception/handler/ValidationFailureExceptionHandler.java | ||
/app/src/main/java/com/onurdesk/iris/service/QrCodeService.java |
1 change: 1 addition & 0 deletions
1
target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com/onurdesk/iris/service/QrCodeServiceTests.class |
2 changes: 2 additions & 0 deletions
2
target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/app/src/test/java/com/onurdesk/iris/SpringBootQrCodeGeneratorReaderApplicationTests.java | ||
/app/src/test/java/com/onurdesk/iris/service/QrCodeServiceTests.java |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The word 'genrate' is misspelled; consider correcting it to 'generate'.
Copilot uses AI. Check for mistakes.