Skip to content

Commit

Permalink
📦 Fin étape 4 #5
Browse files Browse the repository at this point in the history
  • Loading branch information
Elie NEHME committed Oct 5, 2022
1 parent e2b4825 commit 21158d5
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.soprasteria.newsletterapi.configuration;

import com.soprasteria.newsletterapi.domain.MessageService;
import com.soprasteria.newsletterapi.domain.model.DateService;
import com.soprasteria.newsletterapi.domain.spi.MessagePublisher;
import com.soprasteria.newsletterapi.gateway.MessageRepositoryAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class PublicationConfiguration {

@Bean
public MessageService messageService(MessageRepositoryAdapter mra, MessagePublisher mp, DateService ds) {
return new MessageService(mra, mp, ds);
}

@Bean
public DateService dateService() {
return new DateService();
}

@Bean
public MessagePublisher messagePublisher() {
return message -> log.info("Published");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.soprasteria.newsletterapi.controller;

import com.soprasteria.newsletterapi.api.PublicationApi;
import com.soprasteria.newsletterapi.api.data.Publication;
import com.soprasteria.newsletterapi.domain.MessageService;
import com.soprasteria.newsletterapi.domain.model.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

@RestController
public class PublicationController implements PublicationApi {

@Autowired
private MessageService messageService;

@Override
public ResponseEntity<Publication> createPublication(Publication publication) {
UUID idMessageCree = messageService.creer(mapToMessage(publication));
publication.setId(idMessageCree.toString());
return ResponseEntity.ok(publication);
}

@Override
public ResponseEntity<Void> deletePublication(String id) {
return null;
}

@Override
public ResponseEntity<List<Publication>> searchPublication(Boolean abonnement, String text) {
return null;
}

@Override
public ResponseEntity<Publication> updatePublication(String id, Publication publication) {
return null;
}

@Override
public ResponseEntity<Void> validatePublication(String id) {
messageService.valider(UUID.fromString(id));
return ResponseEntity.noContent().build();
}

private Message mapToMessage(Publication publication) {
Message message = new Message();
message.setTitre(publication.getTitre());
message.setDescription(publication.getDescription());
publication.getTags().forEach(tag -> message.getTags().add(tag.getNom()));
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ public class MessageService {
private final MessagePublisher messagePublisher;
private final DateService dateService;

public void creer(Message message) {
public UUID creer(Message message) {
message.setDateCreation(dateService.now());
messageRepository.persister(message);
return message.getId();
}

public void valider(UUID id) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.soprasteria.newsletterapi.controller;

import com.soprasteria.newsletterapi.domain.MessageService;
import com.soprasteria.newsletterapi.domain.model.DateService;
import com.soprasteria.newsletterapi.domain.model.Message;
import com.soprasteria.newsletterapi.domain.spi.MessagePublisher;
import com.soprasteria.newsletterapi.domain.spi.MessageRepository;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
* Cette doublure de test est injectée à la place de MessageService grâce à @Primary
*/
@Service
@Primary
public class MessageServiceDouble extends MessageService {

private UUID idMessageCree;
private Message messageCree;

public MessageServiceDouble(MessageRepository messageRepository, MessagePublisher messagePublisher,
DateService dateService) {
super(messageRepository, messagePublisher, dateService);
}

@Override
public UUID creer(Message message) {
this.messageCree = message;
return idMessageCree;
}

@Override public void modifier(Message message) {

}

@Override public void valider(UUID id) {

}

@Override public void supprimer(UUID idMessage) {

}

public void setIdMessageCree(UUID idMessageCree) {
this.idMessageCree = idMessageCree;
}

public Message getMessageCree() {
return messageCree;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.soprasteria.newsletterapi.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.soprasteria.newsletterapi.api.data.Publication;
import com.soprasteria.newsletterapi.api.data.Tag;
import com.soprasteria.newsletterapi.domain.model.Message;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
class PublicationControllerTest {
private static final String API_BASE_URL = "http://localhost:8080/api/publication/";
private static final String TITRE = "Message 1";
private static final String DESCRIPTION = "Description message 1";
private static final String TAG1_NOM = "java";
private static final String TAG2_NOM = "quickie";
private static final String ID_PUBLICATION = "9ab5ff2c-c44e-4ea6-8bd1-d29cebd2b9b2";

private ObjectMapper objectMapper;
private UUID idMessageCree;
private HttpClient httpClient;

@Autowired
private MessageServiceDouble messageServiceDouble;

@BeforeEach
void setUp() {
objectMapper = new ObjectMapper();
idMessageCree = UUID.randomUUID();
messageServiceDouble.setIdMessageCree(idMessageCree);
httpClient = HttpClient.newBuilder().build();
}

@Test
void createPublication() throws Exception {
Publication publication = publication();

HttpResponse<String> response = postPublication(publication);

assertThat(response.statusCode()).isEqualTo(200);

Publication publicationRecuperee = objectMapper.readValue(response.body(), Publication.class);
assertThat(publicationRecuperee.getId()).isEqualTo(idMessageCree.toString());
assertThat(publicationRecuperee.getTitre()).isEqualTo(TITRE);
assertThat(publicationRecuperee.getDescription()).isEqualTo(DESCRIPTION);
assertThat(publicationRecuperee.getTags().stream().map(Tag::getNom).collect(Collectors.toList())
.containsAll(List.of(TAG1_NOM, TAG2_NOM))).isTrue();

Message messageCree = messageServiceDouble.getMessageCree();
assertThat(messageCree.getTitre()).isEqualTo(TITRE);
assertThat(messageCree.getDescription()).isEqualTo(DESCRIPTION);
assertThat(messageCree.getTags().containsAll(List.of(TAG1_NOM, TAG2_NOM))).isTrue();
}

@Test
void validatePublication() throws Exception {
HttpResponse<String> response = putValidationDePublication();
assertThat(response.statusCode()).isEqualTo(204);
}

@Test
void updatePublication() {
}

@Test
void deletePublication() {
}

private HttpResponse<String> postPublication(Publication publication)
throws URISyntaxException, java.io.IOException, InterruptedException {
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(API_BASE_URL))
.POST(HttpRequest.BodyPublishers.ofString(objectMapper.writeValueAsString(publication)))
.header("Content-Type", "application/json")
.build();
return httpClient.send(request, HttpResponse.BodyHandlers.ofString());
}

private HttpResponse<String> putValidationDePublication()
throws URISyntaxException, java.io.IOException, InterruptedException {
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(API_BASE_URL + ID_PUBLICATION + "/valider"))
.PUT(HttpRequest.BodyPublishers.noBody())
.build();
return httpClient.send(request, HttpResponse.BodyHandlers.ofString());
}

private Publication publication() {
Publication publication = new Publication();
publication.setTitre(TITRE);
publication.setDescription(DESCRIPTION);
Tag tag1 = new Tag();
tag1.setNom(TAG1_NOM);
Tag tag2 = new Tag();
tag2.setNom(TAG2_NOM);
publication.setTags(List.of(tag1, tag2));
return publication;
}

}

0 comments on commit 21158d5

Please sign in to comment.