diff --git a/Backend/src/main/java/unical/demacs/rdm/controller/JsonController.java b/Backend/src/main/java/unical/demacs/rdm/controller/JsonController.java index 1615ab3..7862d41 100644 --- a/Backend/src/main/java/unical/demacs/rdm/controller/JsonController.java +++ b/Backend/src/main/java/unical/demacs/rdm/controller/JsonController.java @@ -43,19 +43,15 @@ public class JsonController { public ResponseEntity> importJob(@RequestBody List jobs, @RequestParam("assigneeEmail") String assigneeEmail) { try { jobs.forEach(job -> { - if (job.getId() != null) { - Optional 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 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())); } } @@ -136,8 +132,7 @@ public ResponseEntity> 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> exportJobScheduledRO() { List schedules = jsonService.readScheduleFile("./data/job-scheduled-ro.json"); diff --git a/Backend/src/main/java/unical/demacs/rdm/persistence/dto/MachineTypeDTO.java b/Backend/src/main/java/unical/demacs/rdm/persistence/dto/MachineTypeDTO.java index 080c160..f49a8b1 100644 --- a/Backend/src/main/java/unical/demacs/rdm/persistence/dto/MachineTypeDTO.java +++ b/Backend/src/main/java/unical/demacs/rdm/persistence/dto/MachineTypeDTO.java @@ -6,7 +6,6 @@ import lombok.NoArgsConstructor; import lombok.AllArgsConstructor; - @Data @NoArgsConstructor @AllArgsConstructor diff --git a/Backend/src/main/java/unical/demacs/rdm/persistence/service/implementation/MachineTypeServiceImpl.java b/Backend/src/main/java/unical/demacs/rdm/persistence/service/implementation/MachineTypeServiceImpl.java index 69e94ae..0b1a311 100644 --- a/Backend/src/main/java/unical/demacs/rdm/persistence/service/implementation/MachineTypeServiceImpl.java +++ b/Backend/src/main/java/unical/demacs/rdm/persistence/service/implementation/MachineTypeServiceImpl.java @@ -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 existing = machineTypeRepository.findById(machineTypeDTO.getId()); - if (existing.isPresent()) { - return machineTypeRepository.save(machineType); + if (machineTypeDTO.getId() != null) { + Optional 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 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 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 diff --git a/Backend/src/test/java/unical/demacs/rdm/controller/JsonControllerTest.java b/Backend/src/test/java/unical/demacs/rdm/controller/JsonControllerTest.java index 44f9821..3e63dde 100644 --- a/Backend/src/test/java/unical/demacs/rdm/controller/JsonControllerTest.java +++ b/Backend/src/test/java/unical/demacs/rdm/controller/JsonControllerTest.java @@ -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)); } @@ -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 @@ -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 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"); + } } diff --git a/Backend/src/test/java/unical/demacs/rdm/service/MachineTypeServiceImplTest.java b/Backend/src/test/java/unical/demacs/rdm/service/MachineTypeServiceImplTest.java index 2aa33c4..c69e9e3 100644 --- a/Backend/src/test/java/unical/demacs/rdm/service/MachineTypeServiceImplTest.java +++ b/Backend/src/test/java/unical/demacs/rdm/service/MachineTypeServiceImplTest.java @@ -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 machineTypes = machineTypeService.getAllMachineTypes(); @@ -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 = machineTypeService.getMachineTypeById(TEST_ID); @@ -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)); @@ -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); @@ -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); @@ -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)); @@ -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); @@ -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));