Skip to content

Commit

Permalink
solito fix dei test
Browse files Browse the repository at this point in the history
  • Loading branch information
PierpaoloSpadafora committed Dec 6, 2024
1 parent b5ae173 commit 7930bfc
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,15 @@ public class JsonController {
public ResponseEntity<Map<String, String>> importJob(@RequestBody List<JobDTO> jobs, @RequestParam("assigneeEmail") String assigneeEmail) {
try {
jobs.forEach(job -> {
if (job.getId() != null) {
Optional<Job> existingJob = jobService.getJobById(job.getId());
if (existingJob.isEmpty()) {
jobService.createJob(assigneeEmail, job);
}
} else {
if (job.getId() != null && jobService.getJobById(job.getId()).isEmpty()) {
jobService.createJob(assigneeEmail, job);
} else if (job.getId() == null) {
jobService.createJob(assigneeEmail, job);
}
});
Map<String, String> response = Map.of("message", "Jobs imported successfully.");
return ResponseEntity.ok(response);
return ResponseEntity.ok(Map.of("message", "Jobs imported successfully."));
} catch (Exception e) {
return ResponseEntity.status(400).body(Map.of("message", "Error importing jobs: " + e.getMessage()));
return ResponseEntity.badRequest().body(Map.of("message", "Error importing jobs: " + e.getMessage()));
}
}

Expand Down Expand Up @@ -136,8 +132,7 @@ public ResponseEntity<List<ScheduleWithMachineDTO>> exportJobScheduledDuration()
return ResponseEntity.ok(schedules);
}

@Operation(summary = "Export Job data to JSON", description = "Export all Job data to JSON.",
tags = {"json-controller"})
@Operation(summary = "Export RO scheduled jobs", description = "Export all RO scheduled jobs to JSON.")
@GetMapping(value = "/export-job-scheduled-ro", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<ScheduleWithMachineDTO>> exportJobScheduledRO() {
List<ScheduleWithMachineDTO> schedules = jsonService.readScheduleFile("./data/job-scheduled-ro.json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;


@Data
@NoArgsConstructor
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,60 @@ public class MachineTypeServiceImpl implements IMachineTypeService {

@Override
public MachineType createMachineType(MachineTypeDTO machineTypeDTO) {
MachineType machineType = new MachineType();
machineType.setName(machineTypeDTO.getName());
machineType.setDescription(machineTypeDTO.getDescription());
logger.info("++++++START REQUEST++++++");
logger.info("Creating machine type");
try {
if (!rateLimiter.tryAcquire()) {
logger.warn("Rate limit exceeded for createMachineType");
throw new MachineException("Rate limit exceeded for createMachineType");
}
MachineType machineType = new MachineType();
machineType.setName(machineTypeDTO.getName());
machineType.setDescription(machineTypeDTO.getDescription());

if (machineTypeDTO.getId() != null) {
Optional<MachineType> existing = machineTypeRepository.findById(machineTypeDTO.getId());
if (existing.isPresent()) {
return machineTypeRepository.save(machineType);
if (machineTypeDTO.getId() != null) {
Optional<MachineType> existing = machineTypeRepository.findById(machineTypeDTO.getId());
if (existing.isPresent()) {
return machineTypeRepository.save(machineType);
} else {
machineType.setId(machineTypeDTO.getId());
return machineTypeRepository.save(machineType);
}
} else {
machineType.setId(machineTypeDTO.getId());
return machineTypeRepository.save(machineType);
}
} else {
return machineTypeRepository.save(machineType);
} catch (Exception e) {
logger.error("Error creating machine type", e);
throw new MachineException("Error creating machine type");
} finally {
logger.info("++++++END REQUEST++++++");
}
}

@Override
public MachineType updateMachineType(Long id, MachineTypeDTO machineTypeDTO) {
Optional<MachineType> existingOpt = machineTypeRepository.findById(id);
if (existingOpt.isEmpty()) {
throw new IllegalArgumentException("MachineType con ID " + id + " non trovato.");
logger.info("++++++START REQUEST++++++");
logger.info("Updating machine type with id: " + id);
try {
if (!rateLimiter.tryAcquire()) {
logger.warn("Rate limit exceeded for updateMachineType");
throw new MachineException("Rate limit exceeded for updateMachineType");
}
Optional<MachineType> existingOpt = machineTypeRepository.findById(id);
if (existingOpt.isEmpty()) {
logger.error("Machine type with id {} not found", id);
throw new MachineException("Machine type not found");
}
MachineType existing = existingOpt.get();
existing.setName(machineTypeDTO.getName());
existing.setDescription(machineTypeDTO.getDescription());
return machineTypeRepository.save(existing);
} catch (Exception e) {
logger.error("Error updating machine type with id: {}", id, e);
throw new MachineException("Error updating machine type");
} finally {
logger.info("++++++END REQUEST++++++");
}
MachineType existing = existingOpt.get();
existing.setName(machineTypeDTO.getName());
existing.setDescription(machineTypeDTO.getDescription());
return machineTypeRepository.save(existing);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void testImportMachineType_Success() throws Exception {
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(machineTypes)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.message").value("MachineTypes imported successfully."));
.andExpect(jsonPath("$.message").value("MachineTypes importati con successo."));

verify(machineTypeService, times(1)).createMachineType(any(MachineTypeDTO.class));
}
Expand All @@ -202,7 +202,7 @@ void testImportMachineType_Failure() throws Exception {
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(machineTypes)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.message").value("Error importing MachineTypes: Error importing MachineTypes"));
.andExpect(jsonPath("$.message").value("Errore durante l'importazione dei MachineTypes: Error importing MachineTypes"));
}

@Test
Expand Down Expand Up @@ -311,4 +311,18 @@ void testExportJobScheduledDuration() throws Exception {

verify(jsonService, times(1)).readScheduleFile("./data/job-scheduled-by-duration.json");
}

@Test
void testExportJobScheduledRO() throws Exception {
List<ScheduleWithMachineDTO> schedules = Collections.singletonList(new ScheduleWithMachineDTO());

when(jsonService.readScheduleFile(anyString())).thenReturn(schedules);

mockMvc.perform(get("/api/v1/json/export-job-scheduled-ro")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().json(objectMapper.writeValueAsString(schedules)));

verify(jsonService, times(1)).readScheduleFile("./data/job-scheduled-ro.json");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ void setUp() {
testMachineTypeDTO = new MachineTypeDTO();
testMachineTypeDTO.setName(TEST_NAME);
testMachineTypeDTO.setDescription(TEST_DESCRIPTION);

when(rateLimiter.tryAcquire()).thenReturn(true);
}

@Test
void testGetAllMachineTypes_Success() {
when(rateLimiter.tryAcquire()).thenReturn(true);
when(machineTypeRepository.findAll()).thenReturn(Arrays.asList(testMachineType));

List<MachineType> machineTypes = machineTypeService.getAllMachineTypes();
Expand All @@ -77,6 +76,7 @@ void testGetAllMachineTypes_RateLimitExceeded() {

@Test
void testGetMachineTypeById_Found() {
when(rateLimiter.tryAcquire()).thenReturn(true);
when(machineTypeRepository.findById(eq(TEST_ID))).thenReturn(Optional.of(testMachineType));

Optional<MachineType> machineType = machineTypeService.getMachineTypeById(TEST_ID);
Expand All @@ -88,6 +88,7 @@ void testGetMachineTypeById_Found() {

@Test
void testGetMachineTypeById_NotFound() {
when(rateLimiter.tryAcquire()).thenReturn(true);
when(machineTypeRepository.findById(eq(TEST_ID))).thenReturn(Optional.empty());

assertThrows(MachineException.class, () -> machineTypeService.getMachineTypeById(TEST_ID));
Expand All @@ -103,6 +104,7 @@ void testGetMachineTypeById_RateLimitExceeded() {

@Test
void testCreateMachineType_Success() {
when(rateLimiter.tryAcquire()).thenReturn(true);
when(machineTypeRepository.save(any(MachineType.class))).thenReturn(testMachineType);

MachineType createdMachineType = machineTypeService.createMachineType(testMachineTypeDTO);
Expand All @@ -123,6 +125,7 @@ void testCreateMachineType_RateLimitExceeded() {

@Test
void testUpdateMachineType_Success() {
when(rateLimiter.tryAcquire()).thenReturn(true);
when(machineTypeRepository.findById(eq(TEST_ID))).thenReturn(Optional.of(testMachineType));
when(machineTypeRepository.save(any(MachineType.class))).thenReturn(testMachineType);

Expand All @@ -136,6 +139,7 @@ void testUpdateMachineType_Success() {

@Test
void testUpdateMachineType_NotFound() {
when(rateLimiter.tryAcquire()).thenReturn(true);
when(machineTypeRepository.findById(eq(TEST_ID))).thenReturn(Optional.empty());

assertThrows(MachineException.class, () -> machineTypeService.updateMachineType(TEST_ID, testMachineTypeDTO));
Expand All @@ -153,6 +157,7 @@ void testUpdateMachineType_RateLimitExceeded() {

@Test
void testDeleteMachineType_Success() {
when(rateLimiter.tryAcquire()).thenReturn(true);
when(machineTypeRepository.findById(eq(TEST_ID))).thenReturn(Optional.of(testMachineType));
doNothing().when(machineTypeRepository).deleteById(TEST_ID);

Expand All @@ -163,6 +168,7 @@ void testDeleteMachineType_Success() {

@Test
void testDeleteMachineType_NotFound() {
when(rateLimiter.tryAcquire()).thenReturn(true);
when(machineTypeRepository.findById(eq(TEST_ID))).thenReturn(Optional.empty());

assertThrows(MachineException.class, () -> machineTypeService.deleteMachineType(TEST_ID));
Expand Down

0 comments on commit 7930bfc

Please sign in to comment.