Skip to content

Commit

Permalink
Testing Broadcast- Controller and Service
Browse files Browse the repository at this point in the history
  • Loading branch information
Nic12345678 committed Apr 12, 2024
1 parent e319413 commit 67507e0
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;

/**
* Application class for starting the micro-service.
*/
Expand All @@ -36,7 +32,6 @@
}
)
@EnableAutoConfiguration
@EnableMethodSecurity(securedEnabled = true)
public class MicroServiceApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
import java.time.LocalDateTime;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Getter
@Setter
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Message {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.muenchen.oss.wahllokalsystem.domain;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.springframework.data.repository.CrudRepository;
Expand All @@ -12,4 +13,7 @@ public interface MessageRepository extends CrudRepository<Message, UUID> {

Optional<Message> findFirstByWahlbezirkIDOrderByEmpfangsZeit(String wahlbezirkID);

List<Message> findByWahlbezirkID(String number);

Message findByNachricht(String s);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void broadcast(final BroadcastMessageDTO messageToBroadcast){
log.debug("#broadcast");


// To Do Validierung Über die Validation API
// To Do Validierung Über die Validation API, correct also Test de/muenchen/oss/wahllokalsystem/rest/BroadcastControllerTest.java
/*if ( wahlbezirkIDs == null || wahlbezirkIDs.isEmpty() || Strings.isNullOrEmpty(nachricht)) {
throw WlsExceptionFactory.build(ExceptionKonstanten.CODE_NACHRICHTENABRUFEN_PARAMETER_UNVOLLSTAENDIG);
}*/
Expand All @@ -53,16 +53,13 @@ public void broadcast(final BroadcastMessageDTO messageToBroadcast){
return message;
}).toList();

log.debug("Storing {} new messages...", messagesToSave.size());

messageRepo.saveAll(messagesToSave);
}

@PreAuthorize("hasAuthority('Broadcast_BUSINESSACTION_GetMessage')")
public MessageDTO getOldestMessage(String wahlbezirkID) throws FachlicheWlsException {
log.debug("#nachrichtenAbrufen");

//ToDo: Wird später
//ToDo: Wird später aus neuem wls-common exception ExceptionKonstanten etwa gebaut

/* if (Strings.isNullOrEmpty(wahlbezirkID)) {
throw WlsExceptionFactory.build(ExceptionKonstanten.CODE_NACHRICHTENABRUFEN_PARAMETER_UNVOLLSTAENDIG);
Expand All @@ -79,17 +76,24 @@ public MessageDTO getOldestMessage(String wahlbezirkID) throws FachlicheWlsExcep

@PreAuthorize("hasAuthority('Broadcast_BUSINESSACTION_MessageRead')")
public void deleteMessage(String nachrichtID) { //TODO UUID als Parameter
log.debug("#nachrichtGelesen");

// if (Strings.isNullOrEmpty(nachrichtID)) {
// throw WlsExceptionFactory.build(ExceptionKonstanten.CODE_NACHRICHTENABRUFEN_PARAMETER_UNVOLLSTAENDIG);
// }

val nachrichtUUID = java.util.UUID.fromString(nachrichtID);
//TODO hat das Altsystem einen Fehler geworfen wenn er nichts gefunden hat?
// Das Altsystem hat so getan alswürde es werfen:
// catch (Exception e) {
// LOG.info("Message with OID:" + nachrichtUUID + "already deleted");
// }
// In Wirklichkeit wirft in diesem Fall CrudRepository keine Exception
// https://github.com/spring-projects/spring-data-commons/issues/2651
try {
messageRepo.deleteById(nachrichtUUID);
} catch (Exception e) {
log.info("Message with OID:" + nachrichtUUID + "already deleted"); //TODO hat das Altsystem einen Fehler geworfen
//ToDo aus neuen ExceptionKonstanten etwas finden
log.error("Message with OID:" + nachrichtUUID + " could not be deleted");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class UnicodeConfigurationTest {
@Test
void testForNfcNormalization() {
// Persist entity with decomposed string.
final MessageDTO messageDTO = new MessageDTO(UUID.fromString("id_1"), "wahlbezirkID_1", TEXT_ATTRIBUTE_DECOMPOSED, LocalDateTime.of(1990, 10, 3, 2, 47));
final MessageDTO messageDTO = new MessageDTO(UUID.fromString("1-2-3-4-5"), "wahlbezirkID_1", TEXT_ATTRIBUTE_DECOMPOSED, LocalDateTime.of(1990, 10, 3, 2, 47));
final TheEntityDto theEntityDto = new TheEntityDto();
theEntityDto.setMyMessageDTO(messageDTO);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import static de.muenchen.oss.wahllokalsystem.TestConstants.SPRING_NO_SECURITY_PROFILE;
import static de.muenchen.oss.wahllokalsystem.TestConstants.SPRING_TEST_PROFILE;
import static org.junit.jupiter.api.Assertions.*;

import org.assertj.core.api.Assertions;
import de.muenchen.oss.wahllokalsystem.MicroServiceApplication;
import de.muenchen.oss.wahllokalsystem.rest.BroadcastMessageDTO;
import de.muenchen.oss.wahllokalsystem.service.BroadcastService;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -26,6 +29,7 @@
}
)
@ActiveProfiles(profiles = { SPRING_TEST_PROFILE, SPRING_NO_SECURITY_PROFILE })
@Slf4j
class MessageRepositoryTest {

@Autowired
Expand All @@ -38,19 +42,24 @@ class MessageRepositoryTest {
@Transactional(propagation = Propagation.REQUIRED, noRollbackFor = Exception.class)
void testSave() {

String originalOid = "1-2-3-4-5";
String originalNachricht = "Test Nachricht";

// Implement your logic here by replacing and/or extending the code

// initialize
Message original = new Message();
original.setNachricht("test nachricht");
original.setOid(UUID.fromString(originalOid));
original.setNachricht(originalNachricht);

// persist
original = repository.save(original);

// check
Message persisted = repository.findById(original.getOid()).orElse(null);
assertNotNull(persisted);
assertEquals(original, persisted);

Assertions.assertThat(persisted).isNotNull();
Assertions.assertThat(persisted).isEqualTo(original);

}

Expand All @@ -62,6 +71,7 @@ void testSave() {
void testFirst() {

List<String> wahlbezirke = Arrays.asList("1", "2", "3", "4");
String searchedWahlbezirkID = "3";

BroadcastMessageDTO m1 = new BroadcastMessageDTO(wahlbezirke, "Ich bin Nachricht_1");
broadcast_S.broadcast(m1);
Expand All @@ -74,23 +84,23 @@ void testFirst() {
BroadcastMessageDTO m5 = new BroadcastMessageDTO(wahlbezirke, "Ich bin Nachricht_5");
broadcast_S.broadcast(m5);

Optional<Message> optionalFoundMessage = repository.findFirstByWahlbezirkIDOrderByEmpfangsZeit("3");
//Expected -sent Message
List<Message> allMessages = (List<Message>) repository.findAll();

allMessages.sort(
Comparator
.comparing((Message m) -> m.getEmpfangsZeit().toLocalDate())
.reversed()
.thenComparing(
Comparator
.comparing((Message m) -> m.getEmpfangsZeit().toLocalTime()
)

)
.thenComparing((Message m) -> m.getEmpfangsZeit().toLocalTime())
);

assertEquals(optionalFoundMessage.get(), allMessages.get(0));
Message sentMessage = allMessages.stream()
.filter(mes -> mes.getWahlbezirkID().equals(searchedWahlbezirkID))
.findFirst()
.get();

//Actual - Found
Optional<Message> optionalFoundMessage = repository.findFirstByWahlbezirkIDOrderByEmpfangsZeit(searchedWahlbezirkID);
Assertions.assertThat(sentMessage).isEqualTo(optionalFoundMessage.get());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ void failurOnWahlbezirkIdIsMissing() {
@Test
void failurOnWahlbezirkIdIsMoreThan1024Chars() {
val message = createMessageWithAllRequiredData();
message.setWahlbezirkID(StringUtils.left("", 1025));
String myString = " ".repeat(1025);
message.setWahlbezirkID(myString);

val validationResult = validator.validate(message);

Expand All @@ -63,7 +64,8 @@ void failurOnNachrichtIsNull() {
@Test
void failurOnNachrichtIsMoreThan1024Chars() {
val message = createMessageWithAllRequiredData();
message.setNachricht(StringUtils.left("", 1025));
String myString = " ".repeat(1025);
message.setNachricht(myString);

val validationResult = validator.validate(message);

Expand Down
Loading

0 comments on commit 67507e0

Please sign in to comment.