InspectionService.kt

1
package io.github.Michal_Antosiewicz.apiary_management_system.service
2
3
import com.fasterxml.jackson.databind.ObjectMapper
4
import io.github.Michal_Antosiewicz.apiary_management_system.domain.DomainEventEntry
5
import io.github.Michal_Antosiewicz.apiary_management_system.domain.Inspection
6
import io.github.Michal_Antosiewicz.apiary_management_system.dto.CreateInspectionRequest
7
import io.github.Michal_Antosiewicz.apiary_management_system.dto.InspectionAlertDto
8
import io.github.Michal_Antosiewicz.apiary_management_system.dto.InspectionResponse
9
import io.github.Michal_Antosiewicz.apiary_management_system.event.InspectionRecordedEvent
10
import io.github.Michal_Antosiewicz.apiary_management_system.repository.DecisionAlertRepository
11
import io.github.Michal_Antosiewicz.apiary_management_system.repository.DomainEventEntryRepository
12
import io.github.Michal_Antosiewicz.apiary_management_system.repository.HiveRepository
13
import io.github.Michal_Antosiewicz.apiary_management_system.repository.InspectionRepository
14
import org.springframework.context.ApplicationEventPublisher
15
import org.springframework.stereotype.Service
16
import org.springframework.transaction.annotation.Transactional
17
import java.time.Instant
18
import java.time.Clock
19
import java.time.temporal.ChronoUnit
20
import jakarta.validation.Validator
21
import io.github.Michal_Antosiewicz.apiary_management_system.domain.OperationalState
22
import java.util.UUID
23
24
@Service
25
class InspectionService(
26
    private val hiveRepository: HiveRepository,
27
    private val inspectionRepository: InspectionRepository,
28
    private val domainEventRepository: DomainEventEntryRepository,
29
    private val alertRepository: DecisionAlertRepository,
30
    private val eventPublisher: ApplicationEventPublisher,
31
    private val objectMapper: ObjectMapper,
32
    private val validator: Validator,
33
    private val clock: Clock
34
) {
35
36
    @Transactional
37
    fun recordInspection(request: CreateInspectionRequest): InspectionResponse {
38
        val errors = validator.validate(request)
39 3 1. recordInspection : removed conditional - replaced equality check with false → SURVIVED
2. recordInspection : removed call to kotlin/jvm/internal/Intrinsics::checkNotNull → NO_COVERAGE
3. recordInspection$lambda$1$lambda$0 : replaced return value with null for io/github/Michal_Antosiewicz/apiary_management_system/service/InspectionService::recordInspection$lambda$1$lambda$0 → NO_COVERAGE
        require(errors.isEmpty()) { errors.joinToString("; ") { "${it.propertyPath}: ${it.message}" } }
40
        val hive = hiveRepository.findByIdForUpdate(request.hiveId)
41 1 1. recordInspection$lambda$2 : replaced return value with null for io/github/Michal_Antosiewicz/apiary_management_system/service/InspectionService::recordInspection$lambda$2 → NO_COVERAGE
            .orElseThrow { IllegalArgumentException("Hive not found with ID: ${request.hiveId}") }
42
43 2 1. recordInspection : removed conditional - replaced equality check with false → SURVIVED
2. recordInspection : removed conditional - replaced equality check with false → KILLED
        require(hive.operationalState != OperationalState.DECOMMISSIONED) { "Cannot inspect a decommissioned hive" }
44
        val inspection = inspectionRepository.save(
45
            Inspection(
46 1 1. recordInspection : removed call to kotlin/jvm/internal/Intrinsics::checkNotNullExpressionValue → SURVIVED
                id = UUID.randomUUID(),
47 1 1. recordInspection : removed call to kotlin/jvm/internal/Intrinsics::checkNotNull → SURVIVED
                hive = hive,
48 2 1. recordInspection : removed call to kotlin/jvm/internal/Intrinsics::checkNotNullExpressionValue → SURVIVED
2. recordInspection : removed call to kotlin/jvm/internal/Intrinsics::checkNotNullExpressionValue → SURVIVED
                inspectionDate = Instant.now(clock).truncatedTo(ChronoUnit.MICROS),
49
                framesOfBees = request.framesOfBees,
50
                framesOfBrood = request.framesOfBrood,
51
                honeyStoresKg = request.honeyStoresKg,
52
                freeCombSpacePercent = request.freeCombSpacePercent,
53
                queenSeen = request.queenSeen,
54
                broodPattern = request.broodPattern,
55
                queenCellsCount = request.queenCellsCount,
56
                queenCellsPlacement = request.queenCellsPlacement,
57
                queenCellsStage = request.queenCellsStage,
58
                varroaMiteCount = request.varroaMiteCount,
59
                signsOfPests = request.signsOfPests,
60
                temperamentScore = request.temperamentScore,
61
                notes = request.notes
62
            )
63
        )
64
65
        val domainEvent = InspectionRecordedEvent(
66 1 1. recordInspection : removed call to kotlin/jvm/internal/Intrinsics::checkNotNullExpressionValue → SURVIVED
            eventId = UUID.randomUUID(),
67
            hiveId = hive.id,
68
            occurredAt = inspection.inspectionDate,
69
            inspectionId = inspection.id,
70
            framesOfBees = inspection.framesOfBees,
71
            framesOfBrood = inspection.framesOfBrood,
72
            honeyStoresKg = inspection.honeyStoresKg,
73
            freeCombSpacePercent = inspection.freeCombSpacePercent,
74
            queenSeen = inspection.queenSeen,
75
            broodPattern = inspection.broodPattern,
76
            queenCellsCount = inspection.queenCellsCount,
77
            queenCellsPlacement = inspection.queenCellsPlacement,
78
            queenCellsStage = inspection.queenCellsStage,
79
            varroaMiteCount = inspection.varroaMiteCount,
80
            signsOfPests = inspection.signsOfPests,
81
            temperamentScore = inspection.temperamentScore
82
        )
83
84
        // Persist event JSON before synchronous consumers create foreign-key references
85
        val payloadJson = objectMapper.writeValueAsString(domainEvent)
86
        domainEventRepository.saveAndFlush(
87
            DomainEventEntry(
88
                id = domainEvent.eventId,
89
                hive = hive,
90 1 1. recordInspection : removed conditional - replaced equality check with false → SURVIVED
                eventType = domainEvent::class.simpleName ?: "InspectionRecordedEvent",
91 1 1. recordInspection : removed call to kotlin/jvm/internal/Intrinsics::checkNotNull → SURVIVED
                payload = payloadJson,
92
                occurredAt = domainEvent.occurredAt
93
            )
94
        )
95
96
        // Publish event to invoke FSM transitions and DSS rule engine listeners
97 1 1. recordInspection : removed call to org/springframework/context/ApplicationEventPublisher::publishEvent → KILLED
        eventPublisher.publishEvent(domainEvent)
98
99
        // Return only alerts linked to this inspection event; historical active alerts have a separate endpoint
100
        val activeAlerts = alertRepository.findByEventId(domainEvent.eventId)
101
            .map { alert ->
102
                InspectionAlertDto(
103
                    alertType = alert.alertType,
104
                    severity = alert.severity,
105
                    riskScore = alert.riskScore,
106
                    recommendationText = alert.recommendationText
107
                )
108
            }
109
110
        return InspectionResponse(
111
            inspectionId = inspection.id,
112
            hiveId = hive.id,
113
            inspectionDate = inspection.inspectionDate,
114
            currentBiologicalState = hive.biologicalState.name,
115
            alertsGenerated = activeAlerts
116
        )
117
    }
118
}

Mutations

39

1.1
Location : recordInspection
Killed by : none
removed conditional - replaced equality check with false → SURVIVED
Covering tests

2.2
Location : recordInspection
Killed by : none
removed call to kotlin/jvm/internal/Intrinsics::checkNotNull → NO_COVERAGE

3.3
Location : recordInspection$lambda$1$lambda$0
Killed by : none
replaced return value with null for io/github/Michal_Antosiewicz/apiary_management_system/service/InspectionService::recordInspection$lambda$1$lambda$0 → NO_COVERAGE

41

1.1
Location : recordInspection$lambda$2
Killed by : none
replaced return value with null for io/github/Michal_Antosiewicz/apiary_management_system/service/InspectionService::recordInspection$lambda$2 → NO_COVERAGE

43

1.1
Location : recordInspection
Killed by : none
removed conditional - replaced equality check with false → SURVIVED
Covering tests

2.2
Location : recordInspection
Killed by : io.github.Michal_Antosiewicz.apiary_management_system.WorkflowIntegrationTest.[engine:junit-jupiter]/[class:io.github.Michal_Antosiewicz.apiary_management_system.WorkflowIntegrationTest]/[test-template:operationalConformance(io.github.Michal_Antosiewicz.apiary_management_system.domain.OperationalState, java.lang.String, java.lang.String)]/[test-template-invocation:#74]
removed conditional - replaced equality check with false → KILLED

46

1.1
Location : recordInspection
Killed by : none
removed call to kotlin/jvm/internal/Intrinsics::checkNotNullExpressionValue → SURVIVED
Covering tests

47

1.1
Location : recordInspection
Killed by : none
removed call to kotlin/jvm/internal/Intrinsics::checkNotNull → SURVIVED
Covering tests

48

1.1
Location : recordInspection
Killed by : none
removed call to kotlin/jvm/internal/Intrinsics::checkNotNullExpressionValue → SURVIVED
Covering tests

2.2
Location : recordInspection
Killed by : none
removed call to kotlin/jvm/internal/Intrinsics::checkNotNullExpressionValue → SURVIVED Covering tests

66

1.1
Location : recordInspection
Killed by : none
removed call to kotlin/jvm/internal/Intrinsics::checkNotNullExpressionValue → SURVIVED
Covering tests

90

1.1
Location : recordInspection
Killed by : none
removed conditional - replaced equality check with false → SURVIVED
Covering tests

91

1.1
Location : recordInspection
Killed by : none
removed call to kotlin/jvm/internal/Intrinsics::checkNotNull → SURVIVED
Covering tests

97

1.1
Location : recordInspection
Killed by : io.github.Michal_Antosiewicz.apiary_management_system.WorkflowIntegrationTest.[engine:junit-jupiter]/[class:io.github.Michal_Antosiewicz.apiary_management_system.WorkflowIntegrationTest]/[test-template:operationalConformance(io.github.Michal_Antosiewicz.apiary_management_system.domain.OperationalState, java.lang.String, java.lang.String)]/[test-template-invocation:#47]
removed call to org/springframework/context/ApplicationEventPublisher::publishEvent → KILLED

Active mutators

Tests examined


Report generated by PIT 1.30.2 support