Skip to content

Commit

Permalink
Merge pull request #269 from SeC-Hikit/266-maintenance-shall-be-creat…
Browse files Browse the repository at this point in the history
…ed-wo-a-linked-trail

Added TrailCode field, modified trailId check on Maintenance validation
  • Loading branch information
loreV authored Oct 5, 2023
2 parents b6d2ea9 + 396debe commit 2c3f98d
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public Maintenance mapToObject(Document document) {
document.getString(Maintenance.OBJECT_ID),
document.getDate(Maintenance.DATE),
document.getString(Maintenance.TRAIL_ID),
document.getString(Maintenance.TRAIL_CODE),
document.getString(Maintenance.MEETING_PLACE),
document.getString(Maintenance.DESCRIPTION),
document.getString(Maintenance.CONTACT),
Expand All @@ -39,6 +40,7 @@ public Document mapToDocument(Maintenance object) {
LOGGER.trace("mapToDocument Maintenance: {} ", object);
return new Document(Maintenance.DATE, object.getDate())
.append(Maintenance.TRAIL_ID, object.getTrailId())
.append(Maintenance.TRAIL_CODE, object.getTrailCode())
.append(Maintenance.CONTACT, object.getContact())
.append(Maintenance.DESCRIPTION, object.getDescription())
.append(Maintenance.MEETING_PLACE, object.getMeetingPlace())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ public List<Maintenance> getByTrailId(final String trailId) {
new Document(Maintenance.TRAIL_ID, trailId))));
}

public List<Maintenance> getByTrailCode(final String trailCodeTemp) {
return new ArrayList<>(toMaintenanceList(collection.find(
new Document(Maintenance.TRAIL_CODE, trailCodeTemp))));
}

public long countMaintenance(String realm) {
return collection.countDocuments(
MongoUtils.getConditionalEqFilter(realm, DB_REALM_STRUCTURE_SELECTOR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,21 @@ class MaintenanceValidator constructor(

override fun validate(request: MaintenanceDto): Set<String> {
val errors = mutableSetOf<String>()
val trailId = request.trailId
if (isEmpty(request.contact)) {
errors.add(String.format(emptyFieldError, "Contact"))
}
if (isEmpty(request.trailId)) {
errors.add(String.format(emptyFieldError, "Code"))
if (isEmpty(trailId) && isEmpty(request.trailCode)) {
errors.add(String.format(emptyFieldError, "Id and Code"))
}
if (isEmpty(request.meetingPlace)) {
errors.add(String.format(emptyFieldError, "Meeting Place"))
}
if (request.date == null) {
errors.add(String.format(emptyFieldError, "date"))
}
errors.addAll(trailExistenceValidator.validateExistenceAndRealm(request.trailId))
if (!isEmpty(trailId))
errors.addAll(trailExistenceValidator.validateExistenceAndRealm(trailId))
if (request.date.before(Date())) {
errors.add(dateInPast)
}
Expand Down
5 changes: 4 additions & 1 deletion backend/src/main/java/org/sc/manager/MaintenanceManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class MaintenanceManager @Autowired constructor(
return maintenanceDao.getByTrailId(id).map { maintenanceMapper.map(it) }
}

fun getByTrailCode(id: String): List<MaintenanceDto> {
return maintenanceDao.getByTrailCode(id).map { maintenanceMapper.map(it) }
}

fun getPast(page: Int, count: Int, realm: String): List<MaintenanceDto> =
maintenanceDao.getPastDate(page, count, getTomorrowDate().toLocalDate(), realm)
.map { maintenanceMapper.map(it) }
Expand Down Expand Up @@ -83,5 +87,4 @@ class MaintenanceManager @Autowired constructor(

private fun getTomorrowDate() = LocalDate.now().plusDays(1).atStartOfDay()


}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class MaintenanceRestIntegrationTest {
private static final String EXPECTED_NAME = "ANY";
private static final String EXPECTED_NAME_2 = "ANY_2";
private static final String EXPECTED_DESCRIPTION = "ANY_DESCRIPTION";
private static final String EXPECTED_TRAIL_CODE = "A101";

private static Date EXPECTED_DATE_IN_FUTURE() {
Calendar c = Calendar.getInstance();
Expand Down Expand Up @@ -79,9 +80,7 @@ public void setUp() {
TrailResponse trailResponse = adminTrailController.importTrail(trailImportDto);
importedTrailId = trailResponse.getContent().get(0).getId();

adminMaintenanceController.create(new MaintenanceDto(null, EXPECTED_DATE_IN_FUTURE(), importedTrailId,
EXPECTED_NAME, EXPECTED_DESCRIPTION, EXPECTED_NAME_2, new RecordDetailsDto()));
maintenanceDAO.upsert(maintenanceMapper.map(new MaintenanceDto(null, EXPECTED_DATE_IN_PAST(), importedTrailId,
maintenanceDAO.upsert(maintenanceMapper.map(new MaintenanceDto(null, EXPECTED_DATE_IN_PAST(), importedTrailId, "",
EXPECTED_NAME, EXPECTED_DESCRIPTION, EXPECTED_NAME_2, new RecordDetailsDto())));
}

Expand All @@ -94,13 +93,28 @@ public void getPast_shouldFindOne() {

@Test
public void getFuture_shouldFindOne() {
adminMaintenanceController.create(new MaintenanceDto(null, EXPECTED_DATE_IN_FUTURE(), importedTrailId, "",
EXPECTED_NAME, EXPECTED_DESCRIPTION, EXPECTED_NAME_2, new RecordDetailsDto()));

MaintenanceResponse response = maintenanceController.getFutureMaintenance(0, 2, NO_FILTERING_TOKEN);
assertThat(response.getContent().size()).isEqualTo(1);
assertThat(response.getContent().get(0).getTrailId()).isEqualTo(importedTrailId);
}

@Test
public void whenMaintenanceIsCreatedWithTrailCodeAndNotTrailId_shouldCreateAndGet() {
adminMaintenanceController.create(new MaintenanceDto(null, EXPECTED_DATE_IN_FUTURE(), "", EXPECTED_TRAIL_CODE,
EXPECTED_NAME, EXPECTED_DESCRIPTION, EXPECTED_NAME_2, new RecordDetailsDto()));
MaintenanceResponse response = maintenanceController.getFutureMaintenance(0, 1, NO_FILTERING_TOKEN);
assertThat(response.getContent().size()).isEqualTo(1);
assertThat(response.getContent().get(0).getTrailCode()).isEqualTo(EXPECTED_TRAIL_CODE);
}

@Test
public void delete() {
adminMaintenanceController.create(new MaintenanceDto(null, EXPECTED_DATE_IN_FUTURE(), importedTrailId, "",
EXPECTED_NAME, EXPECTED_DESCRIPTION, EXPECTED_NAME_2, new RecordDetailsDto()));

MaintenanceResponse response = maintenanceController.getFutureMaintenance(0, 2, NO_FILTERING_TOKEN);
String id = response.getContent().get(0).getId();

Expand All @@ -118,7 +132,7 @@ public void contextLoads() {

@After
public void setDown() {
// IntegrationUtils.emptyCollection(dataSource, Maintenance.COLLECTION_NAME);
IntegrationUtils.emptyCollection(dataSource, "core.Maintenance");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ public void shallGeneratePdfWithCycloDescription() {
TrailDto trailDto = trailResponse.getContent().stream().findFirst().get();
trailDto.setCycloDetails(new CycloDetailsDto(CycloClassification.BC_PLUS, 120, new CycloFeasibilityDto(true, 10),
new CycloFeasibilityDto(false, 0), ANY_LONG_DESC));
adminMaintenanceController.create(new MaintenanceDto(null, new Date(), trailDto.getId(), "San Lazzaro", "", "Mario Rossi", new RecordDetailsDto()));
adminMaintenanceController.create(new MaintenanceDto(null, new Date(), trailDto.getId(), "any","San Lazzaro", "", "Mario Rossi", new RecordDetailsDto()));
trailController.updateTrail(trailDto);
}


}
2 changes: 2 additions & 0 deletions root/data/src/main/java/org/sc/data/model/Maintenance.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class Maintenance {

public static final String OBJECT_ID = "_id";
public static final String TRAIL_ID = "trailId";
public static final String TRAIL_CODE = "trailCode";
public static final String DATE = "date";
public static final String DESCRIPTION = "description";
public static final String CONTACT = "contact";
Expand All @@ -24,6 +25,7 @@ public class Maintenance {
private String id;
private Date date;
private String trailId;
private String trailCode;
private String meetingPlace;
private String description;
private String contact;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package org.sc.common.rest;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.util.Date;
import java.util.Objects;

@Data
@AllArgsConstructor
public class MaintenanceDto {
private String id;
private Date date;
private String trailId;
private String trailCode;
private String meetingPlace;
private String description;
private String contact;
Expand Down

0 comments on commit 2c3f98d

Please sign in to comment.