Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion endpoint-insights-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,26 @@
<version>3.5.7</version>
</dependency>

</dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.5.Final</version>
</dependency>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.5.Final</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</dependency>

</dependencies>

<profiles>
<profile>
Expand Down Expand Up @@ -164,6 +183,16 @@
<artifactId>lombok</artifactId>
<version>1.18.42</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.5.Final</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,62 @@

import com.vsp.endpointinsightsapi.dto.BatchRequestDTO;
import com.vsp.endpointinsightsapi.dto.BatchResponseDTO;
import com.vsp.endpointinsightsapi.model.TestBatch;
import com.vsp.endpointinsightsapi.service.BatchService;
import com.vsp.endpointinsightsapi.model.TestBatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/batches")
public class BatchesController {

private static final Logger LOG = LoggerFactory.getLogger(BatchesController.class);
private static final Logger LOG = LoggerFactory.getLogger(BatchesController.class);

private final BatchService batchService;
private final BatchService batchService;

public BatchesController(BatchService batchService) {
this.batchService = batchService;
}
public BatchesController(BatchService batchService){
this.batchService = batchService;
}

// GET /api/batches — stub list (unchanged)
// GET /api/batches — stub list (unchanged)
@GetMapping
public ResponseEntity<List<BatchResponseDTO>> listBatches() {
List<BatchResponseDTO> batches = List.of(
new BatchResponseDTO(1L, "Daily API Tests", "ACTIVE"),
new BatchResponseDTO(2L, "Weekly Regression", "INACTIVE")
);
return ResponseEntity.ok(batches);
List<BatchResponseDTO> batches = List.of(
BatchResponseDTO.builder()
.id(UUID.randomUUID())
.batchName("Daily API Tests")
.scheduleId(334523453L)
.startTime(LocalDate.now().minusDays(1))
.lastTimeRun(LocalDate.now())
.active(true)
// .jobs(Collections.emptyList())
.build(),
BatchResponseDTO.builder()
.id(UUID.randomUUID())
.batchName("Weekly Regression")
.scheduleId(42L)
.startTime(LocalDate.now().minusWeeks(1))
.lastTimeRun(LocalDate.now().minusDays(3))
.active(false)
// .jobs(Collections.emptyList())
.build()
);
return ResponseEntity.ok(batches);
}

// GET /api/batches/{id}
@GetMapping("/{id}")
public ResponseEntity<TestBatch> getBatch(@PathVariable UUID id) {
return batchService.getBatchById(id)
.map(b -> ResponseEntity.ok(b))
.orElseGet(() -> ResponseEntity.notFound().build());
public ResponseEntity<BatchResponseDTO> getBatch(@PathVariable UUID id) {
BatchResponseDTO batch = batchService.getBatchById(id);
return ResponseEntity.ok(batch);
}

// POST /api/batches
Expand All @@ -52,15 +69,20 @@ public ResponseEntity<TestBatch> createBatch(@RequestBody BatchRequestDTO reques

// PUT /api/batches/{id} — stubbed
@PutMapping("/{id}")
public ResponseEntity<BatchResponseDTO> updateBatch(@PathVariable Long id,
@RequestBody BatchRequestDTO request) {
BatchResponseDTO updated = new BatchResponseDTO(id, request.getName(), "UPDATED");
return ResponseEntity.ok(updated);
public ResponseEntity<BatchResponseDTO> updateBatch(@PathVariable UUID id, @RequestBody BatchRequestDTO request) {
BatchResponseDTO updated = BatchResponseDTO.builder()
.id(id)
.batchName(request.getName())
.lastTimeRun(LocalDate.now())
.build();

return ResponseEntity.ok(updated);
}

// DELETE /api/batches/{id} — stubbed
// DELETE /api/batches/{id}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteBatch(@PathVariable Long id) {
return ResponseEntity.noContent().build();
public ResponseEntity<Void> deleteBatch(@PathVariable UUID id) {
batchService.deleteBatchById(id);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package com.vsp.endpointinsightsapi.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import com.vsp.endpointinsightsapi.model.Job;
import lombok.*;

import java.time.LocalDate;
import java.util.List;
import java.util.UUID;


@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class BatchResponseDTO {
private Long id;
private String name;
private String status;
private UUID id;
private String batchName;
private Long scheduleId;
private LocalDate startTime;
private LocalDate lastTimeRun;
private Boolean active;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.vsp.endpointinsightsapi.exception;

import org.springframework.http.HttpStatus;

/**
* Thrown when a batch with the given ID does not exist in the database.
*/
public class BatchNotFoundException extends CustomException {

public BatchNotFoundException(String batchId) {
super(HttpStatus.NOT_FOUND,
new ErrorResponse("BATCH_NOT_FOUND", "Batch not found with ID: " + batchId, null));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.vsp.endpointinsightsapi.mapper;

import com.vsp.endpointinsightsapi.dto.BatchResponseDTO;
import com.vsp.endpointinsightsapi.model.TestBatch;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper(componentModel = "spring")
public interface BatchMapper {

// MapStruct will generate the implementation automatically
@Mapping(source = "batch_id", target = "id")
BatchResponseDTO toDto(TestBatch entity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@

import com.vsp.endpointinsightsapi.model.TestBatch;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;
import java.util.UUID;


@Repository
public interface TestBatchRepository extends JpaRepository<TestBatch, UUID> {
@Query("SELECT t FROM TestBatch t where t.batch_id = :id")
Optional<TestBatch> findById(@Param("id")UUID id);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.vsp.endpointinsightsapi.service;

import com.vsp.endpointinsightsapi.dto.BatchResponseDTO;
import com.vsp.endpointinsightsapi.exception.BatchNotFoundException;
import com.vsp.endpointinsightsapi.mapper.BatchMapper;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import com.vsp.endpointinsightsapi.dto.BatchRequestDTO;
Expand All @@ -12,23 +14,42 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.util.Objects;

@Service
public class BatchService {

private static final Logger LOG = LoggerFactory.getLogger(BatchService.class);

private final TestBatchRepository batchRepository;
private final TestBatchRepository testBatchRepository;
private final BatchMapper batchMapper;
private final JobRepository jobRepository;

public BatchService(TestBatchRepository batchRepository, JobRepository jobRepository) {
this.batchRepository = batchRepository;
this.jobRepository = jobRepository;
public BatchService(TestBatchRepository testBatchRepository, BatchMapper batchMapper, JobRepository jobRepository) {
this.testBatchRepository = Objects.requireNonNull(testBatchRepository, "testBatchRepository must not be null");
this.batchMapper = Objects.requireNonNull(batchMapper, "batchMapper must not be null");
this.jobRepository = Objects.requireNonNull(jobRepository, "jobRepository must not be null");
}

//Get Batch — used by GET /api/batches/{id}
public BatchResponseDTO getBatchById(UUID batchId) {
TestBatch b = testBatchRepository.findById(batchId)
.orElseThrow(() -> {
LOG.debug("Batch {} not found", batchId);
return new BatchNotFoundException(batchId.toString());
});
return batchMapper.toDto(b);
}

//GET by id — used by BatchesController and its unit test
public Optional<TestBatch> getBatchById(UUID id) {
return batchRepository.findById(id);
//Delete Batch — used by DELETE /api/batches/{id}
public void deleteBatchById(UUID batchId) {
if (!testBatchRepository.existsById(batchId)) {
LOG.debug("Batch {} not found", batchId);
throw new BatchNotFoundException(batchId.toString());
}

testBatchRepository.deleteById(batchId);
LOG.info("Deleted batch {}", batchId);

}

//Create Batch — used by POST /api/batches
Expand All @@ -43,6 +64,6 @@ public TestBatch createBatch(BatchRequestDTO request) {
}
batch.setJobs(jobs);
}
return batchRepository.save(batch);
return testBatchRepository.save(batch);
}
}

This file was deleted.

Loading
Loading