Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
9.0 kB
0
Indexable
Never
class YourClassNameTest {

    @Mock
    private YourDependency yourDependency;

    private YourClassName yourClass;

    @BeforeEach
    void setUp() {
        MockitoAnnotations.openMocks(this);
        yourClass = new YourClassName(yourDependency);
    }

    @Test
    void testIsExtraDataExistInS3_WhenMultipartFilesLengthIsNotOne() {
        // Arrange
        int featureId = 1;
        MultipartFile[] multipartFiles = new MultipartFile[0];
        ExtraDataType type = ExtraDataType.TYPE1;

        // Act
        boolean result = yourClass.isExtraDataExistInS3(featureId, multipartFiles, type);

        // Assert
        assertFalse(result);
        verify(yourDependency, never()).isExtraDataExistInS3(anyString());
    }

    @Test
    void testIsExtraDataExistInS3_WhenIOExceptionOccurs() throws IOException {
        // Arrange
        int featureId = 1;
        MultipartFile[] multipartFiles = {new MockMultipartFile("file.txt", new byte[0])};
        ExtraDataType type = ExtraDataType.TYPE1;

        when(yourDependency.isExtraDataExistInS3(anyString())).thenReturn(true);

        // Act
        boolean result = yourClass.isExtraDataExistInS3(featureId, multipartFiles, type);

        // Assert
        assertFalse(result);
        verify(yourDependency, never()).isExtraDataExistInS3(anyString());
    }

    @Test
    void testIsExtraDataExistInS3_WhenSuccess() throws IOException {
        // Arrange
        int featureId = 1;
        MultipartFile[] multipartFiles = {new MockMultipartFile("file.txt", new byte[0])};
        ExtraDataType type = ExtraDataType.TYPE1;

        when(yourDependency.isExtraDataExistInS3(anyString())).thenReturn(true);

        // Act
        boolean result = yourClass.isExtraDataExistInS3(featureId, multipartFiles, type);

        // Assert
        assertTrue(result);

        ArgumentCaptor<String> filePathCaptor = ArgumentCaptor.forClass(String.class);
        verify(yourDependency).isExtraDataExistInS3(filePathCaptor.capture());

        String expectedFilePath = getFilePath(featureId, "file.txt", type);
        assertEquals(expectedFilePath, filePathCaptor.getValue());
    }
}
////////////////
private MockMultipartFile createMockMultipartFile(String filename, String content) {
    byte[] contentBytes = content.getBytes();
    InputStream inputStream = new ByteArrayInputStream(contentBytes);
    return new MockMultipartFile("file", filename, null, inputStream);
}
//////////////////////
class YourClassNameTest {

    @Mock
    private ProceedingJoinPoint joinPoint;

    @Mock
    private FeatureExtraDataService featureExtraDataService;

    private YourClassName yourClass;

    @BeforeEach
    void setUp() {
        MockitoAnnotations.openMocks(this);
        yourClass = new YourClassName(featureExtraDataService);
    }

    @Test
    void testLogFeatureExtraDataUploaded_WhenExtraDataExists() throws Throwable {
        // Arrange
        int featureId = 1;
        MultipartFile[] multipartFiles = {createMockMultipartFile("test.txt", "Test file content")};
        ExtraDataType type = ExtraDataType.TYPE_A;
        String endpointUrl = "http://example.com/endpoint";

        when(featureExtraDataService.isExtraDataExistInS3(eq(featureId), eq(multipartFiles), eq(type))).thenReturn(true);
        when(joinPoint.proceed()).thenReturn(endpointUrl);

        // Act
        String result = yourClass.logFeatureExtraDataUploaded(joinPoint, featureId, multipartFiles, type);

        // Assert
        assertEquals(endpointUrl, result);

        ArgumentCaptor<Integer> featureIdCaptor = ArgumentCaptor.forClass(Integer.class);
        ArgumentCaptor<String> dataTypeCaptor = ArgumentCaptor.forClass(String.class);
        ArgumentCaptor<ChangeType> changeTypeCaptor = ArgumentCaptor.forClass(ChangeType.class);
        ArgumentCaptor<String> detailCaptor = ArgumentCaptor.forClass(String.class);

        verify(featureExtraDataService).isExtraDataExistInS3(featureIdCaptor.capture(), any(), eq(type));
        verify(joinPoint).proceed();
        verify(featureExtraDataService).saveChangeLog(eq(featureId), isNull(), dataTypeCaptor.capture(),
                changeTypeCaptor.capture(), detailCaptor.capture());

        assertEquals(featureId, featureIdCaptor.getValue());
        assertEquals(DataType.EXTRA_DATA.toString(), dataTypeCaptor.getValue());
        assertEquals(ChangeType.UPDATED, changeTypeCaptor.getValue());

        Map<String, String> expectedDetail = new HashMap<>();
        expectedDetail.put("extraDataName", "endpoint");
        assertEquals(expectedDetail.toString(), detailCaptor.getValue());
    }

    @Test
    void testLogFeatureExtraDataUploaded_WhenExtraDataDoesNotExist() throws Throwable {
        // Arrange
        int featureId = 1;
        MultipartFile[] multipartFiles = {createMockMultipartFile("test.txt", "Test file content")};
        ExtraDataType type = ExtraDataType.TYPE_A;
        String endpointUrl = "http://example.com/endpoint";

        when(featureExtraDataService.isExtraDataExistInS3(eq(featureId), eq(multipartFiles), eq(type))).thenReturn(false);
        when(joinPoint.proceed()).thenReturn(endpointUrl);

        // Act
        String result = yourClass.logFeatureExtraDataUploaded(joinPoint, featureId, multipartFiles, type);

        // Assert
        assertEquals(endpointUrl, result);

        ArgumentCaptor<Integer> featureIdCaptor = ArgumentCaptor.forClass(Integer.class);
        ArgumentCaptor<String> dataTypeCaptor = ArgumentCaptor.forClass(String.class);
        ArgumentCaptor<ChangeType> changeTypeCaptor = ArgumentCaptor.forClass(ChangeType.class);
        ArgumentCaptor<String> detailCaptor = ArgumentCaptor.forClass(String.class);

        verify(featureExtraDataService).isExtraDataExistInS3(featureIdCaptor.capture(), any(), eq(type));
        verify(joinPoint).proceed();
        verify(featureExtraDataService).saveChangeLog(eq(featureId), isNull(), dataTypeCaptor.capture(),
                changeTypeCaptor.capture(), detailCaptor.capture());

        assertEquals(featureId, featureIdCaptor.getValue());
        assertEquals(DataType.EXTRA_DATA.toString(), dataTypeCaptor.getValue());
        assertEquals(ChangeType.CREATED, changeTypeCaptor.getValue());

        Map<String, String> expectedDetail = new HashMap<>();
        expectedDetail.put("extraDataName", "endpoint");
        assertEquals(expectedDetail.toString(), detailCaptor.getValue());
    }

    private MockMultipartFile createMockMultipartFile(String filename, String content) {
        byte[] contentBytes = content.getBytes();
        return new MockMultipartFile("file", filename, null, contentBytes);
    }
}
//////////////////////////
class YourClassNameTest {

    @Mock
    private JoinPoint joinPoint;

    @Mock
    private FeatureExtraDataService featureExtraDataService;

    private YourClassName yourClass;

    @BeforeEach
    void setUp() {
        MockitoAnnotations.openMocks(this);
        yourClass = new YourClassName(featureExtraDataService);
    }

    @Test
    void testLogFeatureExtraDataDeleted_WhenSuccessIsTrue() {
        // Arrange
        int featureId = 1;
        String extraDataName = "extraData1";
        boolean success = true;

        // Act
        yourClass.logFeatureExtraDataDeleted(joinPoint, featureId, extraDataName, success);

        // Assert
        ArgumentCaptor<Integer> featureIdCaptor = ArgumentCaptor.forClass(Integer.class);
        ArgumentCaptor<DataType> dataTypeCaptor = ArgumentCaptor.forClass(DataType.class);
        ArgumentCaptor<ChangeType> changeTypeCaptor = ArgumentCaptor.forClass(ChangeType.class);
        ArgumentCaptor<String> detailCaptor = ArgumentCaptor.forClass(String.class);

        verify(featureExtraDataService).saveChangeLog(featureIdCaptor.capture(), isNull(), dataTypeCaptor.capture(),
                changeTypeCaptor.capture(), detailCaptor.capture());

        assertEquals(featureId, featureIdCaptor.getValue());
        assertEquals(DataType.EXTRA_DATA, dataTypeCaptor.getValue());
        assertEquals(ChangeType.DELETED, changeTypeCaptor.getValue());

        Map<String, String> expectedDetail = new HashMap<>();
        expectedDetail.put("extraDataName", extraDataName);
        assertEquals(expectedDetail.toString(), detailCaptor.getValue());
    }

    @Test
    void testLogFeatureExtraDataDeleted_WhenSuccessIsFalse() {
        // Arrange
        int featureId = 2;
        String extraDataName = "extraData2";
        boolean success = false;

        // Act
        yourClass.logFeatureExtraDataDeleted(joinPoint, featureId, extraDataName, success);

        // Assert
        verify(featureExtraDataService, Mockito.never()).saveChangeLog(anyInt(), any(), any(), any(), any());
    }
}
Leave a Comment