Skip to content
Snippets Groups Projects
Commit a80cf535 authored by Dominika Zemanovičová's avatar Dominika Zemanovičová
Browse files

Merge branch 'Mail-Module' into 'main'

Mail module

See merge request !12
parents 84c0f6f1 80dc8a9c
No related branches found
No related tags found
1 merge request!12Mail module
Pipeline #
...@@ -17,5 +17,16 @@ ...@@ -17,5 +17,16 @@
<properties> <properties>
<spring-boot.repackage.skip>true</spring-boot.repackage.skip> <spring-boot.repackage.skip>true</spring-boot.repackage.skip>
</properties> </properties>
<dependencies>
<dependency>
<groupId>org.ifinalframework.annotation</groupId>
<artifactId>final-annotation-web</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
</dependency>
</dependencies>
</project> </project>
\ No newline at end of file
package org.fuseri.model.dto.mail;
import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class MailDto {
@NotBlank
public
String receiver;
@NotBlank
public
String content;
public MailDto(String receiver, String content) {
this.receiver = receiver;
this.content = content;
}
}
...@@ -22,11 +22,37 @@ ...@@ -22,11 +22,37 @@
<artifactId>spring-boot-starter</artifactId> <artifactId>spring-boot-starter</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>3.0.4</version>
</dependency>
<dependency>
<groupId>org.fuseri</groupId>
<artifactId>models</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>javax.mail</groupId>-->
<!-- <artifactId>mail</artifactId>-->
<!-- <version>1.4.7</version>-->
<!-- </dependency>-->
</dependencies> </dependencies>
<build> <build>
......
package org.fuseri.modulemail.service;
import jakarta.validation.Valid;
import jakarta.validation.constraints.PositiveOrZero;
import org.fuseri.model.dto.mail.MailDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
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;
@RestController
@RequestMapping("/mail")
public class MailController {
private final MailService service;
@Autowired
public MailController(MailService service) {
this.service = service;
}
@GetMapping("/{id}")
public String getEmail(@PositiveOrZero @PathVariable("id") Long id) {
return "No mail with that id yet";
}
@DeleteMapping("/delete/{id}")
public String deleteMail(@PositiveOrZero @PathVariable("id") Long id) {
return "Nothing to delete Yet";
}
@PostMapping()
public String sendMail(@Valid @RequestBody MailDto emailDto) {
return service.send(emailDto);
}
}
package org.fuseri.modulemail.service;
import org.fuseri.model.dto.mail.MailDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
@Service
public class MailService {
private final JavaMailSender emailSender;
@Autowired
public MailService(JavaMailSender emailSender) {
this.emailSender = emailSender;
}
public String send(MailDto dto) {
var message = new SimpleMailMessage();
message.setFrom("sprachul@gmail.com");
message.setTo(dto.receiver);
message.setSubject("Sprachschul");
message.setText(dto.content);
emailSender.send(message);
// store to database once there is one
return "Success, you have sent: " + dto.content;
}
public MailDto getMail(long id) {
return new MailDto("empty","empty"); // return from database once there is one
}
public String deleteMail(long id) {
return "No mail with that Id";
}
}
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=sprachschul@gmail.com
spring.mail.password=xnyxsknctypmubbb
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
package org.fuseri.modulemail.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.fuseri.model.dto.mail.MailDto;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
class MailControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
void getEmail() throws Exception {
mockMvc.perform(get("/mail/{id}", 1))
.andExpect(status().isOk());
}
@Test
void deleteMail() throws Exception {
mockMvc.perform(delete("/mail/delete/{id}", 1))
.andExpect(status().isOk());
}
@Test
void sendMail() throws Exception {
var mailDto = new MailDto("user@example.com", "Hello");
MailController mailController = mock(MailController.class);
when(mailController.sendMail(mailDto)).thenReturn("Success, you have sent: " + mailDto.getContent());
mockMvc.perform(post("/mail")
.content(asJsonString(mailDto))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
private static String asJsonString(final Object obj) throws JsonProcessingException {
return new ObjectMapper().writeValueAsString(obj);
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment