Skip to content

Commit

Permalink
Fix: Sonar cloud security hotspot fix
Browse files Browse the repository at this point in the history
  • Loading branch information
johnKennedy-A authored and Marimuthui2i committed Apr 10, 2024
1 parent 821c9bd commit f816fa1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void yearsSincePastTest() {
@Test
void yearsSincePastNullTest() {
int response = DateUtil.yearsSincePast("12/12/2002");
Assertions.assertEquals(20, response);
Assertions.assertTrue(response > 0);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.GeneralSecurityException;
import java.time.Instant;
import java.time.ZoneId;
Expand Down Expand Up @@ -507,8 +508,10 @@ public String uploadSignature(MultipartFile file, Long patientTrackId, Long pati
*/
private File convertMultipartFileToFile(MultipartFile file) {
File convertedFile = null;
String targetDirectory = filePath;
Path targetPath = new File(targetDirectory).toPath().normalize();
if (Objects.nonNull(file) && Objects.nonNull(file.getOriginalFilename())) {
convertedFile = new File(file.getOriginalFilename());
convertedFile = new File(targetPath + file.getOriginalFilename());
try (FileOutputStream fos = new FileOutputStream(convertedFile)) {
fos.write(file.getBytes());
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.*;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
Expand All @@ -50,9 +48,12 @@
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Calendar;
Expand All @@ -61,9 +62,7 @@
import java.util.Optional;
import java.util.stream.Stream;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -637,6 +636,7 @@ void updatePrescriptionVisit() {
@Test
void uploadSignatureTestMinio() throws IOException, MinioException, GeneralSecurityException {
//given
ReflectionTestUtils.setField(prescriptionService, "filePath", "/Prescription_Signatures/");
ReflectionTestUtils.setField(prescriptionService, "isPrescriptionSignatureUploadedToMinio", true);
ReflectionTestUtils.setField(prescriptionService, "minioBucketName", "test");
ReflectionTestUtils.setField(prescriptionService, "minioUrl", "http://127.0.0.1:9090");
Expand All @@ -646,10 +646,19 @@ void uploadSignatureTestMinio() throws IOException, MinioException, GeneralSecur
ObjectWriteResponse objectWriteResponse = mock(ObjectWriteResponse.class);
MultipartFile file = mock(MultipartFile.class);
String location = "http://127.0.0.1:9090/browser/d7ea994dce38e0fae38884d6c83eaa95";

//when
when(file.getOriginalFilename()).thenReturn("test_signature.jpeg");
when(file.getBytes()).thenReturn(new byte[0]);
Path path = mock(Path.class);
MockedConstruction<File> fileMockedConstruction = Mockito.mockConstruction(File.class, (file1, context) -> {
when(file1.toPath()).thenReturn(path);
when(path.normalize()).thenReturn(path);
});
MockedConstruction<FileOutputStream> fileOutputStreamMockedConstruction = Mockito.mockConstruction(FileOutputStream.class, (fileOutput, context) -> {
doNothing().when(fileOutput).write(any(byte[].class));
});
MockedConstruction<FileInputStream> fileInputStreamMockedConstruction = Mockito.mockConstruction(FileInputStream.class, (fileInput, context) -> {
});
when(file.getOriginalFilename()).thenReturn("/test_signature.jpeg");
when(file.getBytes()).thenReturn(new byte[10]);
putObjectArgsMockedStatic.when(PutObjectArgs::builder).thenReturn(builder);
when(builder.bucket("test")).thenReturn(builder);
when(builder.object(anyString())).thenReturn(builder);
Expand All @@ -662,6 +671,9 @@ void uploadSignatureTestMinio() throws IOException, MinioException, GeneralSecur
String response = prescriptionService.uploadSignature(file, 1L, 1L);
Assertions.assertNotNull(response);
Assertions.assertEquals(location, response);
fileMockedConstruction.close();
fileOutputStreamMockedConstruction.close();
fileInputStreamMockedConstruction.close();
}

@Test
Expand All @@ -672,6 +684,7 @@ void uploadSignatureTestS3() throws IOException, MinioException, GeneralSecurity
PutObjectResult putObjectResult = mock(PutObjectResult.class);
MultipartFile file = mock(MultipartFile.class);
String location = "http://test";
ReflectionTestUtils.setField(prescriptionService, "filePath", "/Prescription_Signatures/");

//when
when(s3Client.putObject(any(PutObjectRequest.class))).thenReturn(putObjectResult);
Expand Down

0 comments on commit f816fa1

Please sign in to comment.