HiveOperationService.kt

1
package io.github.Michal_Antosiewicz.apiary_management_system.service
2
import com.fasterxml.jackson.databind.ObjectMapper
3
import io.github.Michal_Antosiewicz.apiary_management_system.domain.*
4
import io.github.Michal_Antosiewicz.apiary_management_system.dto.*
5
import io.github.Michal_Antosiewicz.apiary_management_system.event.*
6
import io.github.Michal_Antosiewicz.apiary_management_system.repository.*
7
import jakarta.validation.Validator
8
import org.springframework.context.ApplicationEventPublisher
9
import org.springframework.stereotype.Service
10
import org.springframework.transaction.annotation.Transactional
11
import java.time.Clock
12
import java.time.Instant
13
import java.time.temporal.ChronoUnit
14
import java.util.UUID
15
16
@Service
17
class HiveOperationService(private val hives: HiveRepository, private val treatments: TreatmentRepository,
18
    private val events: DomainEventEntryRepository, private val decisions: HarvestDecisionRepository,
19
    private val publisher: ApplicationEventPublisher, private val mapper: ObjectMapper,
20
    private val validator: Validator, private val clock: Clock) {
21
    private fun validate(request: Any) {
22
        val errors = validator.validate(request)
23 3 1. validate : removed conditional - replaced equality check with false → SURVIVED
2. validate$lambda$1$lambda$0 : replaced return value with null for io/github/Michal_Antosiewicz/apiary_management_system/service/HiveOperationService::validate$lambda$1$lambda$0 → NO_COVERAGE
3. validate : removed call to kotlin/jvm/internal/Intrinsics::checkNotNull → NO_COVERAGE
        require(errors.isEmpty()) { errors.joinToString("; ") { "${it.propertyPath}: ${it.message}" } }
24
    }
25
    @Transactional
26
    fun attemptHoneyHarvest(hiveId: UUID, request: HarvestHoneyRequest): HarvestHoneyResponse {
27 1 1. attemptHoneyHarvest : removed call to io/github/Michal_Antosiewicz/apiary_management_system/service/HiveOperationService::validate → SURVIVED
        validate(request)
28 1 1. attemptHoneyHarvest$lambda$2 : replaced return value with null for io/github/Michal_Antosiewicz/apiary_management_system/service/HiveOperationService::attemptHoneyHarvest$lambda$2 → NO_COVERAGE
        val hive = hives.findByIdForUpdate(hiveId).orElseThrow { IllegalArgumentException("Hive not found: $hiveId") }
29
        // Timestamp after obtaining the per-hive lock. Clients cannot backdate guard checks.
30 1 1. attemptHoneyHarvest : removed call to kotlin/jvm/internal/Intrinsics::checkNotNullExpressionValue → SURVIVED
        val event = HoneyHarvestAttemptedEvent(hiveId = hiveId, occurredAt = Instant.now(clock).truncatedTo(ChronoUnit.MICROS),
31
            targetSuperCount = request.targetSuperCount, operatorNotes = request.operatorNotes)
32 2 1. attemptHoneyHarvest : removed call to kotlin/jvm/internal/Intrinsics::checkNotNull → SURVIVED
2. attemptHoneyHarvest : removed call to io/github/Michal_Antosiewicz/apiary_management_system/service/HiveOperationService::persistAndPublish → KILLED
        persistAndPublish(hive, event)
33 1 1. attemptHoneyHarvest$lambda$3 : replaced return value with null for io/github/Michal_Antosiewicz/apiary_management_system/service/HiveOperationService::attemptHoneyHarvest$lambda$3 → NO_COVERAGE
        val decision = decisions.findById(event.eventId).orElseThrow { IllegalStateException("Harvest listener did not record a decision") }
34 1 1. attemptHoneyHarvest : removed conditional - replaced equality check with false → KILLED
        return HarvestHoneyResponse(hive.id, if (decision.approved) "APPROVED" else "REJECTED",
35
            decision.message, decision.eventId, decision.reasonCode, decision.blockedUntil)
36
    }
37
    @Transactional
38
    fun applyTreatment(hiveId: UUID, request: ApplyTreatmentRequest): TreatmentResponse {
39 1 1. applyTreatment : removed call to io/github/Michal_Antosiewicz/apiary_management_system/service/HiveOperationService::validate → SURVIVED
        validate(request)
40 3 1. applyTreatment : removed conditional - replaced equality check with false → KILLED
2. applyTreatment : removed conditional - replaced equality check with false → KILLED
3. applyTreatment : removed conditional - replaced equality check with false → KILLED
        require(request.hiveId == null || request.hiveId == hiveId) { "hiveId in body must match the URL" }
41 1 1. applyTreatment$lambda$5 : replaced return value with null for io/github/Michal_Antosiewicz/apiary_management_system/service/HiveOperationService::applyTreatment$lambda$5 → NO_COVERAGE
        val hive = hives.findByIdForUpdate(hiveId).orElseThrow { IllegalArgumentException("Hive not found: $hiveId") }
42 2 1. applyTreatment : removed conditional - replaced equality check with false → SURVIVED
2. applyTreatment : removed conditional - replaced equality check with false → KILLED
        require(hive.operationalState != OperationalState.DECOMMISSIONED) { "Cannot treat a decommissioned hive" }
43
        val now = Instant.now(clock).truncatedTo(ChronoUnit.MICROS)
44 1 1. applyTreatment : removed call to kotlin/jvm/internal/Intrinsics::checkNotNull → SURVIVED
        val treatment = treatments.saveAndFlush(Treatment(hive = hive, medicineName = request.medicineName,
45 1 1. applyTreatment : removed call to kotlin/jvm/internal/Intrinsics::checkNotNull → SURVIVED
            activeSubstance = request.activeSubstance, startDate = now,
46 1 1. applyTreatment : removed call to kotlin/jvm/internal/Intrinsics::checkNotNullExpressionValue → SURVIVED
            treatmentEndDate = now.plus(request.durationDays.toLong(), ChronoUnit.DAYS),
47
            withdrawalDays = request.withdrawalDays,
48 3 1. applyTreatment : removed call to kotlin/jvm/internal/Intrinsics::checkNotNullExpressionValue → SURVIVED
2. applyTreatment : removed call to kotlin/jvm/internal/Intrinsics::checkNotNullExpressionValue → SURVIVED
3. applyTreatment : Replaced long addition with subtraction → KILLED
            withdrawalEndDate = now.plus(request.durationDays.toLong() + request.withdrawalDays, ChronoUnit.DAYS)))
49
        val event = TreatmentAppliedEvent(hiveId = hive.id, occurredAt = now, treatmentId = treatment.id,
50
            medicineName = treatment.medicineName, activeSubstance = treatment.activeSubstance,
51
            startDate = now, treatmentEndDate = treatment.treatmentEndDate, withdrawalEndDate = treatment.withdrawalEndDate)
52 1 1. applyTreatment : removed call to io/github/Michal_Antosiewicz/apiary_management_system/service/HiveOperationService::persistAndPublish → KILLED
        persistAndPublish(hive, event)
53
        return TreatmentResponse(treatment.id, hive.id, treatment.medicineName, treatment.startDate,
54
            treatment.treatmentEndDate, treatment.withdrawalEndDate, hive.operationalState.name)
55
    }
56
    private fun persistAndPublish(hive: Hive, event: ApiaryDomainEvent) {
57
        events.saveAndFlush(DomainEventEntry(id = event.eventId, hive = hive,
58 2 1. persistAndPublish : removed call to kotlin/jvm/internal/Intrinsics::checkNotNull → SURVIVED
2. persistAndPublish : removed call to kotlin/jvm/internal/Intrinsics::checkNotNullExpressionValue → SURVIVED
            eventType = event::class.simpleName!!, payload = mapper.writeValueAsString(event), occurredAt = event.occurredAt))
59 1 1. persistAndPublish : removed call to org/springframework/context/ApplicationEventPublisher::publishEvent → KILLED
        publisher.publishEvent(event)
60
    }
61
}

Mutations

23

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

2.2
Location : validate$lambda$1$lambda$0
Killed by : none
replaced return value with null for io/github/Michal_Antosiewicz/apiary_management_system/service/HiveOperationService::validate$lambda$1$lambda$0 → NO_COVERAGE

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

27

1.1
Location : attemptHoneyHarvest
Killed by : none
removed call to io/github/Michal_Antosiewicz/apiary_management_system/service/HiveOperationService::validate → SURVIVED
Covering tests

28

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

30

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

32

1.1
Location : attemptHoneyHarvest
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:#79]
removed call to io/github/Michal_Antosiewicz/apiary_management_system/service/HiveOperationService::persistAndPublish → KILLED

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

33

1.1
Location : attemptHoneyHarvest$lambda$3
Killed by : none
replaced return value with null for io/github/Michal_Antosiewicz/apiary_management_system/service/HiveOperationService::attemptHoneyHarvest$lambda$3 → NO_COVERAGE

34

1.1
Location : attemptHoneyHarvest
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:#55]
removed conditional - replaced equality check with false → KILLED

39

1.1
Location : applyTreatment
Killed by : none
removed call to io/github/Michal_Antosiewicz/apiary_management_system/service/HiveOperationService::validate → SURVIVED
Covering tests

40

1.1
Location : applyTreatment
Killed by : io.github.Michal_Antosiewicz.apiary_management_system.WorkflowIntegrationTest.[engine:junit-jupiter]/[class:io.github.Michal_Antosiewicz.apiary_management_system.WorkflowIntegrationTest]/[test-template:invalidInputDoesNotPersistAnything(java.lang.String, java.util.Map)]/[test-template-invocation:#6]
removed conditional - replaced equality check with false → KILLED

2.2
Location : applyTreatment
Killed by : io.github.Michal_Antosiewicz.apiary_management_system.WorkflowIntegrationTest.[engine:junit-jupiter]/[class:io.github.Michal_Antosiewicz.apiary_management_system.WorkflowIntegrationTest]/[test-template:invalidInputDoesNotPersistAnything(java.lang.String, java.util.Map)]/[test-template-invocation:#6]
removed conditional - replaced equality check with false → KILLED

3.3
Location : applyTreatment
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:#63]
removed conditional - replaced equality check with false → KILLED

41

1.1
Location : applyTreatment$lambda$5
Killed by : none
replaced return value with null for io/github/Michal_Antosiewicz/apiary_management_system/service/HiveOperationService::applyTreatment$lambda$5 → NO_COVERAGE

42

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

2.2
Location : applyTreatment
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:#63]
removed conditional - replaced equality check with false → KILLED

44

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

45

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

46

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

48

1.1
Location : applyTreatment
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:#63]
Replaced long addition with subtraction → KILLED

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

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

52

1.1
Location : applyTreatment
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:#51]
removed call to io/github/Michal_Antosiewicz/apiary_management_system/service/HiveOperationService::persistAndPublish → KILLED

58

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

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

59

1.1
Location : persistAndPublish
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:#79]
removed call to org/springframework/context/ApplicationEventPublisher::publishEvent → KILLED

Active mutators

Tests examined


Report generated by PIT 1.30.2 support