Untitled
unknown
plain_text
2 years ago
12 kB
8
Indexable
package com.sopromadze.blogapi.dashBordTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.mock.web.MockMultipartFile;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
public class TssServiceImplTest {
@Mock
private FeatureService featureService;
@Mock
private FeatureRuleConfigService featureRuleConfigService;
@Mock
private FeatureExtraDataService featureExtraDataService;
@InjectMocks
private TssServiceImpl tssService;
@BeforeEach
public void setup() {
MockitoAnnotations.openMocks(this);
}
@Test
public void testApplyCustomerFile_WhenFeatureNotFound_ThrowsException() {
// Arrange
ParameterDTO parameter = new ParameterDTO();
MockMultipartFile customerFile = new MockMultipartFile("customerFile", "test.xml", "text/xml", new byte[0]);
when(featureService.getFeature(anyString())).thenReturn(null);
// Act & Assert
assertThrows(FeatureNotFoundException.class, () -> tssService.applyCustomerFile(parameter, customerFile));
}
@Test
public void testApplyCustomerFile_WhenOperatorCodeMissing_ThrowsException() {
// Arrange
ParameterDTO parameter = new ParameterDTO();
MockMultipartFile customerFile = new MockMultipartFile("customerFile", "test.xml", "text/xml", new byte[0]);
Feature tssFeature = new Feature();
tssFeature.setId(1L);
when(featureService.getFeature(anyString())).thenReturn(tssFeature);
// Act & Assert
assertThrows(InvalidParameterException.class, () -> tssService.applyCustomerFile(parameter, customerFile));
}
@Test
public void testApplyCustomerFile_WhenFeatureRuleConfigNotFound_CreatesNewConfig() {
// Arrange
ParameterDTO parameter = new ParameterDTO();
parameter.setOperatorCode("OC123");
MockMultipartFile customerFile = new MockMultipartFile("customerFile", "test.xml", "text/xml", new byte[0]);
Feature tssFeature = new Feature();
tssFeature.setId(1L);
when(featureService.getFeature(anyString())).thenReturn(tssFeature);
when(featureRuleConfigService.getFeatureRuleConfig(anyLong(), any(Rule.class))).thenReturn(null);
// Act
tssService.applyCustomerFile(parameter, customerFile);
// Assert
verify(featureRuleConfigService, times(1)).createFeatureRuleConfig(anyLong(), any(Rule.class));
verify(featureRuleConfigService, times(1)).updateFeatureRuleConfigById(anyLong(), any(FeatureRuleConfig.class));
}
@Test
public void testApplyCustomerFile_WhenFeatureRuleConfigFound_UpdatesConfig() {
// Arrange
ParameterDTO parameter = new ParameterDTO();
parameter.setOperatorCode("OC123");
MockMultipartFile customerFile = new MockMultipartFile("customerFile", "test.xml", "text/xml", new byte[0]);
Feature tssFeature = new Feature();
tssFeature.setId(1L);
when(featureService.getFeature(anyString())).thenReturn(tssFeature);
FeatureRuleConfig existingConfig = new FeatureRuleConfig();
existingConfig.setId(1L);
existingConfig.setFields(new ArrayList<>());
when(featureRuleConfigService.getFeatureRuleConfig(anyLong(), any(Rule.class))).thenReturn(existingConfig);
// Act
tssService.applyCustomerFile(parameter, customerFile);
// Assert
verify(featureRuleConfigService, never()).createFeatureRuleConfig(anyLong(), any(Rule.class));
verify(featureRuleConfigService, times(1)).updateFeatureRuleConfigById(anyLong(), any(FeatureRuleConfig.class));
}
// Add more test cases for other scenarios...
@Test
public void testApplyCustomerFile_WhenCustomerFileUploadFails_ThrowsException() {
// Arrange
ParameterDTO parameter = new ParameterDTO();
parameter.setOperatorCode("OC123");
MockMultipartFile customerFile = new MockMultipartFile("customerFile", "test.xml", "text/xml", new byte[0]);
Feature tssFeature = new Feature();
tssFeature.setId(1L);
when(featureService.getFeature(anyString())).thenReturn(tssFeature);
FeatureRuleConfig existingConfig = new FeatureRuleConfig();
existingConfig.setId(1L);
existingConfig.setFields(new ArrayList<>());
when(featureRuleConfigService.getFeatureRuleConfig(anyLong(), any(Rule.class))).thenReturn(existingConfig);
when(featureExtraDataService.uploadFeatureExtraData(
anyInt(), any(MultipartFile[].class), any(ExtraDataType.class), anyString(), anyBoolean()))
.thenThrow(new MultipleExtraDataUploadException("Failed to upload customer file"));
// Act & Assert
assertThrows(MultipleExtraDataUploadException.class, () -> tssService.applyCustomerFile(parameter, customerFile));
verify(featureRuleConfigService, never()).updateFeatureRuleConfigById(anyLong(), any(FeatureRuleConfig.class));
}
@Test
public void testApplyCustomerFile_WhenBaseRuleNotFound_ThrowsException() {
// Arrange
ParameterDTO parameter = new ParameterDTO();
parameter.setOperatorCode("OC123");
MockMultipartFile customerFile = new MockMultipartFile("customerFile", "test.xml", "text/xml", new byte[0]);
Feature tssFeature = new Feature();
tssFeature.setId(1L);
when(featureService.getFeature(anyString())).thenReturn(tssFeature);
FeatureRuleConfig existingConfig = new FeatureRuleConfig();
existingConfig.setId(1L);
existingConfig.setFields(new ArrayList<>());
when(featureRuleConfigService.getFeatureRuleConfig(anyLong(), any(Rule.class))).thenReturn(existingConfig);
when(featureRuleConfigService.getAllFeatureRuleConfigs(anyLong()))
.thenReturn(Collections.emptyList());
// Act & Assert
assertThrows(BaseRuleNotFoundException.class, () -> tssService.applyCustomerFile(parameter, customerFile));
verify(featureRuleConfigService, never()).createFeatureRuleConfig(anyLong(), any(Rule.class));
verify(featureRuleConfigService, never()).updateFeatureRuleConfigById(anyLong(), any(FeatureRuleConfig.class));
}
@Test
public void testApplyCustomerFile_WhenCustomerFileUploadSucceeds_CallsUploadFeatureExtraData() {
// Arrange
ParameterDTO parameter = new ParameterDTO();
parameter.setOperatorCode("OC123");
MockMultipartFile customerFile = new MockMultipartFile("customerFile", "test.xml", "text/xml", new byte[0]);
Feature tssFeature = new Feature();
tssFeature.setId(1L);
when(featureService.getFeature(anyString())).thenReturn(tssFeature);
FeatureRuleConfig existingConfig = new FeatureRuleConfig();
existingConfig.setId(1L);
existingConfig.setFields(new ArrayList<>());
when(featureRuleConfigService.getFeatureRuleConfig(anyLong(), any(Rule.class))).thenReturn(existingConfig);
when(featureExtraDataService.uploadFeatureExtraData(
anyInt(), any(MultipartFile[].class), any(ExtraDataType.class), anyString(), anyBoolean()))
.thenReturn("https://example.com/customer.xml");
// Act
tssService.applyCustomerFile(parameter, customerFile);
// Assert
verify(featureExtraDataService, times(1)).uploadFeatureExtraData(
anyInt(), any(MultipartFile[].class), any(ExtraDataType.class), anyString(), anyBoolean());
}
}
/////////////////////////////
package com.sopromadze.blogapi.dashBordTest;
@RunWith(MockitoJUnitRunner.class)
public class TssApiControllerTest {
@InjectMocks
private TssApiController tssApiController;
@Mock
private TssService tssService;
@Test
public void testApplyCustomerFile() throws Exception {
// Arrange
MockMultipartFile mockFile = new MockMultipartFile(
"customerFile", "test.xml", "text/xml", "<xml>data</xml>".getBytes());
HttpServletRequest mockRequest = mock(HttpServletRequest.class);
// Act
ResultDTO result = tssApiController.applyCustomerFile(mockRequest, mockFile);
// Assert
assertEquals(HttpStatus.OK, result.getStatus());
verify(tssService).applyCustomerFile(any(ParameterDTO.class), eq(mockFile));
}
}
///////////////////////////////////
@RunWith(MockitoJUnitRunner.class)
public class ChangeLogServiceImplTest {
@InjectMocks
private ChangeLogServiceImpl changeLogService;
@Mock
private FeatureService featureService;
@Mock
private ChangeLogRepository changeLogRepository;
@Test
public void testGetAll() {
// Arrange
List<ChangeLog> expectedChangeLogs = new ArrayList<>();
expectedChangeLogs.add(new ChangeLog());
expectedChangeLogs.add(new ChangeLog());
Mockito.when(changeLogRepository.findAll()).thenReturn(expectedChangeLogs);
// Act
List<ChangeLog> result = changeLogService.getAll();
// Assert
assertEquals(expectedChangeLogs, result);
}
@Test
public void testGetAllWithPageable() {
// Arrange
Pageable pageable = PageRequest.of(0, 10);
List<ChangeLog> expectedChangeLogs = new ArrayList<>();
expectedChangeLogs.add(new ChangeLog());
expectedChangeLogs.add(new ChangeLog());
Page<ChangeLog> expectedPage = new PageImpl<>(expectedChangeLogs);
Mockito.when(changeLogRepository.findAll(pageable)).thenReturn(expectedPage);
Mockito.when(featureService.getFeature(Mockito.anyInt())).thenReturn(new Feature());
// Act
Page<ChangeLog> result = changeLogService.getAll(pageable);
// Assert
assertEquals(expectedPage, result);
assertEquals("key", result.getContent().get(0).getFeatureName());
assertEquals("key", result.getContent().get(1).getFeatureName());
}
@Test
public void testGetAllWithFeatureIdAndPageable() {
// Arrange
int featureId = 1;
Pageable pageable = PageRequest.of(0, 10);
List<ChangeLog> expectedChangeLogs = new ArrayList<>();
expectedChangeLogs.add(new ChangeLog());
expectedChangeLogs.add(new ChangeLog());
Page<ChangeLog> expectedPage = new PageImpl<>(expectedChangeLogs);
Mockito.when(changeLogRepository.findAllByFeatureId(featureId, pageable)).thenReturn(expectedPage);
// Act
Page<ChangeLog> result = changeLogService.getAll(featureId, pageable);
// Assert
assertEquals(expectedPage, result);
}
@Test
public void testDeleteAll() {
// Act
changeLogService.deleteAll();
// Assert
Mockito.verify(changeLogRepository).deleteAll();
}
@Test
public void testSave() {
// Arrange
ChangeLog changeLog = new ChangeLog();
// Act
changeLogService.save(changeLog);
// Assert
Mockito.verify(changeLogRepository).save(changeLog);
}
}Editor is loading...
Leave a Comment