Skip to content

Commit

Permalink
634 API-Endpunkt zum Schreiben des Datentransfers (#26)
Browse files Browse the repository at this point in the history
* 🎉 Methods for creating Datentransfer

* 🧑‍💻 Refactored the saving method

* ✨ POST REST API

* 🚨 Spotless and Checkstyle

* 🐛 Fixed Mapper

* ✅ Added tests

* 📄 Added license

* 🔖 Release Notes

* 🚨 Spotless

* ♻️ Test naming convention
  • Loading branch information
mirrodi committed Feb 21, 2024
1 parent 205d7d1 commit 147371b
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/src/dokumentation/RELEASENOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release-Notes

## Sprint 7 (13.02.2024 - 05.03.2024)
### Hinzugefügt
- POST Schnittstelle fürs Erstellen von Datentransferen

## Sprint 6 (23.01.2024 - 13.02.2024)
### Hinzugefügt
- License-Headers hinzugefügt
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* The MIT License
* Copyright © 2023 Landeshauptstadt München | it@M
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.muenchen.mobidam.domain.dtos;

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.UUID;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString(callSuper = true)
@NoArgsConstructor
public class DatentransferCreateDTO {

@NotEmpty
private String prozessId;

@NotNull
private LocalDateTime zeitstempel;

@NotEmpty
private String ereignis;

private String info;

@NotNull
private UUID schnittstelle;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
package de.muenchen.mobidam.domain.mappers;

import de.muenchen.mobidam.domain.Datentransfer;
import de.muenchen.mobidam.domain.dtos.DatentransferCreateDTO;
import de.muenchen.mobidam.domain.dtos.DatentransferDTO;
import de.muenchen.mobidam.domain.enums.EreignisTyp;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

Expand All @@ -33,4 +35,9 @@ public interface DatentransferMapper {
@Mapping(target = "schnittstelle", source = "schnittstelle.id")
DatentransferDTO toDTO(Datentransfer datentransfer);

@Mapping(target = "schnittstelle.id", source = "datentransferCreateDTO.schnittstelle")
@Mapping(target = "ereignis", source = "ereignis")
@Mapping(ignore = true, target = "id")
Datentransfer toEntity(DatentransferCreateDTO datentransferCreateDTO, EreignisTyp ereignis);

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@
*/
package de.muenchen.mobidam.rest;

import de.muenchen.mobidam.domain.dtos.DatentransferCreateDTO;
import de.muenchen.mobidam.domain.dtos.DatentransferDTO;
import de.muenchen.mobidam.service.DatentransferService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import java.util.Optional;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -55,4 +59,14 @@ public ResponseEntity<?> getLatestResultStateBySchnittstelle(@PathVariable Strin
return new ResponseEntity<>(datentransferDTO.get(), HttpStatus.OK);
return ResponseEntity.notFound().build();
}

@Operation(summary = "Creating a Datentransfer for an existing Schnittstelle")
@PostMapping
public ResponseEntity<?> createDatentransfer(@Valid @RequestBody DatentransferCreateDTO datentransferCreateDTO) {
Optional<DatentransferDTO> datentransferDTO = datentransferService.createDatentransfer(datentransferCreateDTO);
if (datentransferDTO.isPresent())
return new ResponseEntity<>(datentransferDTO.get(), HttpStatus.OK);
return ResponseEntity.badRequest().build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
package de.muenchen.mobidam.service;

import de.muenchen.mobidam.domain.Datentransfer;
import de.muenchen.mobidam.domain.Schnittstelle;
import de.muenchen.mobidam.domain.dtos.DatentransferCreateDTO;
import de.muenchen.mobidam.domain.dtos.DatentransferDTO;
import de.muenchen.mobidam.domain.enums.EreignisTyp;
import de.muenchen.mobidam.domain.mappers.DatentransferMapper;
import de.muenchen.mobidam.repository.DatentransferRepository;
import de.muenchen.mobidam.repository.SchnittstelleRepository;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand All @@ -43,6 +46,7 @@ public class DatentransferService {
private static final int PAGE_SIZE = 10;

private final DatentransferRepository datentransferRepository;
private final SchnittstelleRepository schnittstelleRepository;
private final DatentransferMapper datentransferMapper;

public Iterable<DatentransferDTO> getBySchnittstelle(String schnittstelleId, int page) {
Expand All @@ -62,4 +66,13 @@ public Optional<DatentransferDTO> getLatestResultStateBySchnittstelle(String sch
UUID.fromString(schnittstelleId), notResultStateEreignisTypes);
return datentransfer.map(datentransferMapper::toDTO);
}

public Optional<DatentransferDTO> createDatentransfer(DatentransferCreateDTO datentransferDTO) {
Optional<Schnittstelle> schnittstelle = schnittstelleRepository.findById(datentransferDTO.getSchnittstelle());
if (schnittstelle.isEmpty())
return Optional.empty();

Datentransfer datentransfer = datentransferMapper.toEntity(datentransferDTO, EreignisTyp.valueOf(datentransferDTO.getEreignis()));
return Optional.of(datentransferMapper.toDTO(datentransferRepository.save(datentransfer)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
*/
package de.muenchen.mobidam.rest;

import com.fasterxml.jackson.databind.ObjectMapper;
import de.muenchen.mobidam.MicroServiceApplication;
import de.muenchen.mobidam.domain.Schnittstelle;
import de.muenchen.mobidam.domain.dtos.DatentransferCreateDTO;
import de.muenchen.mobidam.domain.enums.SchnittstellenStatus;
import de.muenchen.mobidam.repository.SchnittstelleRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
Expand All @@ -35,6 +40,10 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.http.MediaType;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import static de.muenchen.mobidam.TestConstants.SPRING_NO_SECURITY_PROFILE;
Expand All @@ -60,9 +69,12 @@ class DatentransferControllerTest {
@Autowired
private DatentransferController datentransferController;

@Autowired
private SchnittstelleRepository schnittstelleRepository;

@Test
@Transactional(propagation = Propagation.REQUIRED, noRollbackFor = Exception.class)
void testSuccess() {
void test_getBySchnittstelle() {

ResponseEntity<?> datentransferDTOs = datentransferController.getBySchnittstelle(UUID.randomUUID().toString(), 0);

Expand All @@ -72,11 +84,59 @@ void testSuccess() {

@Test
@Transactional(propagation = Propagation.REQUIRED, noRollbackFor = Exception.class)
void testException() throws Exception {
void test_createDatentransfer() {

Schnittstelle schnittstelle = new Schnittstelle();
schnittstelle.setName("test");
schnittstelle.setCreationDate(LocalDate.now());
schnittstelle.setStatus(SchnittstellenStatus.AKTIVIERT);
schnittstelle = schnittstelleRepository.save(schnittstelle);

DatentransferCreateDTO datentransferCreateDTO = new DatentransferCreateDTO();
datentransferCreateDTO.setEreignis("FEHLER");
datentransferCreateDTO.setInfo("test");
datentransferCreateDTO.setZeitstempel(LocalDateTime.now());
datentransferCreateDTO.setProzessId("process");
datentransferCreateDTO.setSchnittstelle(schnittstelle.getId());
ResponseEntity<?> datentransferDTO = datentransferController.createDatentransfer(datentransferCreateDTO);

assertEquals(HttpStatus.OK, datentransferDTO.getStatusCode());
assertNotNull(datentransferDTO.getBody());
}

@Test
@Transactional(propagation = Propagation.REQUIRED, noRollbackFor = Exception.class)
void test_getBySchnittstelle_isBadRequest() throws Exception {

mockMvc.perform(get("/api/datentransfer/1/0")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest());

}

@Test
@Transactional(propagation = Propagation.REQUIRED, noRollbackFor = Exception.class)
void test_createDatentransfer_isBadRequest() throws Exception {
DatentransferCreateDTO datentransferCreateDTO = new DatentransferCreateDTO();
datentransferCreateDTO.setEreignis("wrong-type");
datentransferCreateDTO.setInfo("test");
datentransferCreateDTO.setZeitstempel(LocalDateTime.now());
datentransferCreateDTO.setProzessId("process");
datentransferCreateDTO.setSchnittstelle(UUID.randomUUID());

Map<String, Object> body = new HashMap<>();
body.put("ereignis", "wrong-type");
body.put("info", "test");
body.put("zeitstempel", "2024-02-20T16:02:26.706Z");
body.put("prozess", "test");
body.put("schnittstelle", "1");

ObjectMapper objectMapper = new ObjectMapper();

mockMvc.perform(post("/api/datentransfer")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(body)))
.andExpect(status().isBadRequest());

}
}

0 comments on commit 147371b

Please sign in to comment.