Skip to content

Commit

Permalink
add testcases, make code more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas committed Mar 27, 2020
1 parent 31673f4 commit cf610bb
Show file tree
Hide file tree
Showing 7 changed files with 351 additions and 23 deletions.
42 changes: 38 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,48 @@
</exclusions>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<!-- <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId>
<scope>test</scope> </dependency> -->
</dependencies>

<profiles>
<profile>
<id>flapdoodle</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
</profiles>

<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Optional<TestResult> getTestResult(@PathVariable("id") String id) {
public TestResult addTestResult(@PathVariable("id") String id, @RequestBody TestResult testResult)
throws FalseInformedException {
testResult.setId(id);

//TODO logic should be moved to service
Optional<TestResult> previousResultOptional = testResultService.getTestResult(id);
if (!previousResultOptional.isPresent()) {
return informNegatives(testResult);
Expand All @@ -66,7 +66,7 @@ public class ErrorResult{
TestResult result;
String comment;
}

//TODO saving should not depend on notification, we also get updates on the status that do not trigger notification
private TestResult informNegatives(TestResult testResult) {
TestResult saveResult;
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package de.wirvsvirus.testresult.backend.service;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
Expand All @@ -17,9 +14,11 @@
@Component
@Slf4j
public class TestResultPushService {
private static final String MOBILE_PATTERN = "[+491|01]\\d+";
private static final String MOBILE_PATTERN = "^(\\+491|01|00491)\\d+";
private static final String MOBILE_CLEANUP_PATTERN = "[^(\\d|+)]";

private static final String EMAIL_REGEX = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";

private static final String FROM_VALUE = "Krankenhaus";
private static final String NEGATIV_TEXT = "Wir freuen uns Ihnen mitteilen zu können, dass ihr COVID-19 Testergebnis negativ ist. Der Virus konnte bei Ihnen nicht festegestellt werden.";

Expand All @@ -42,15 +41,15 @@ public boolean executePush(TestResult testProcess) {

boolean pushDone=false;

if(isValidEmailAddress(contact)) {
if(contact.matches(EMAIL_REGEX)) {
message.setContact(contact);
try {
emailService.sendMail(message);
pushDone=true;
} catch (MailSendingException e) {
log.debug("sending mail failed",e);
}
}else if (contact.replaceAll(MOBILE_CLEANUP_PATTERN, "").matches(MOBILE_PATTERN)) {
}else if (contact.replaceAll("\\s", "").matches(MOBILE_PATTERN)) {
try {
message.setContact(contact.replaceAll(MOBILE_CLEANUP_PATTERN, ""));
smsService.sendNegativeResultSms(message);
Expand All @@ -68,15 +67,4 @@ private PushMessage createMessage() {
message.setText(NEGATIV_TEXT);
return message;
}

public static boolean isValidEmailAddress(String email) {
boolean result = true;
try {
InternetAddress emailAddr = new InternetAddress(email);
emailAddr.validate();
} catch (AddressException ex) {
result = false;
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package de.wirvsvirus.testresult.backend.rest;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.bson.Document;
import org.junit.jupiter.api.BeforeEach;
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 de.wirvsvirus.testresult.backend.persistence.TestResultRepo;

@AutoConfigureMockMvc
@SpringBootTest
class TestResultControllerIT {

@Autowired
public MockMvc mvc;

@Autowired
TestResultRepo repo;

private static final String RESULTID = "123";

@BeforeEach
public void cleanUp() {
repo.deleteAll();
}

@Test
public void postAndGetTestResult() throws Exception {

Document postBody = new Document();
postBody.put("status", "NEGATIVE");

mvc.perform(post("/tests/" + RESULTID)
.with(user("user").password("password").roles("POSTUSER"))
.contentType(MediaType.APPLICATION_JSON)
.content(postBody.toJson()))
.andExpect(status().isOk());

mvc.perform(get("/tests/" + RESULTID))
.andExpect(status().isOk())
.andDo(r -> {
Document getResult = Document.parse(r.getResponse().getContentAsString());
assertAll("content contains all the values of a new record",
() -> assertTrue(getResult.containsKey("id")),
() -> assertEquals("123", getResult.getString("id")),

() -> assertTrue(getResult.containsKey("status")),
() -> assertEquals("NEGATIVE", getResult.getString("status")),

() -> assertTrue(getResult.containsKey("contact")), () -> assertNull(getResult.get("contact")),

() -> assertTrue(getResult.containsKey("notified")),
() -> assertEquals(Boolean.FALSE, getResult.getBoolean("notified")));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package de.wirvsvirus.testresult.backend.rest;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Optional;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import de.wirvsvirus.testresult.backend.exceptions.FalseInformedException;
import de.wirvsvirus.testresult.backend.model.TestResult;
import de.wirvsvirus.testresult.backend.model.TestResult.Result;
import de.wirvsvirus.testresult.backend.service.TestResultPushService;
import de.wirvsvirus.testresult.backend.service.TestResultService;

@ExtendWith(MockitoExtension.class)
public class TestResultControllerNotifictaionTest {

@Mock
TestResultService testResultService;
@Mock
TestResultPushService pushService;

@InjectMocks
TestResultController controller;

@Test
public void pushNotifiactionTriggeredWhenIdUnknownSoFar() throws FalseInformedException {
TestResult tr = new TestResult();
tr.setContact("some@mail.com");
tr.setNotified(false);
tr.setStatus(Result.NEGATIVE);

when(testResultService.getTestResult(eq("123"))).thenReturn(Optional.empty());
when(pushService.executePush(any())).thenReturn(Boolean.TRUE);

controller.addTestResult("123", tr);
//check existing
verify(testResultService,times(1)).getTestResult(eq("123"));
//do push
verify(pushService, times(1)).executePush(any());
//save testresult
verify(testResultService,times(1)).createTestProcess(any());
}
@Test
public void pushNotifiactionTriggeredWhenStatusIsTheSameButNotYetNotified() throws FalseInformedException {

TestResult existing = new TestResult();
existing.setContact("some@mail.com");
existing.setNotified(false);
existing.setStatus(Result.NEGATIVE);

TestResult tr = new TestResult();
tr.setContact("some@mail.com");
tr.setStatus(Result.NEGATIVE);

when(testResultService.getTestResult(eq("123"))).thenReturn(Optional.of(existing));
when(pushService.executePush(any())).thenReturn(Boolean.TRUE);

controller.addTestResult("123", tr);
//check existing
verify(testResultService,times(1)).getTestResult(eq("123"));
//do push
verify(pushService, times(1)).executePush(any());
//save testresult
verify(testResultService,times(1)).createTestProcess(any());
}


@Test
public void pushNotifiactionNotTriggeredWhenAlreadyNotified() throws FalseInformedException {

TestResult existing = new TestResult();
existing.setContact("some@mail.com");
existing.setNotified(true);
existing.setStatus(Result.NEGATIVE);

TestResult tr = new TestResult();
tr.setContact("some@mail.com");
tr.setNotified(false);
tr.setStatus(Result.NEGATIVE);

when(testResultService.getTestResult(eq("123"))).thenReturn(Optional.of(existing));

controller.addTestResult("123", tr);
//check existing
verify(testResultService,times(1)).getTestResult(eq("123"));
//do push
verify(pushService, never()).executePush(any());
//save testresult
verify(testResultService,never()).createTestProcess(any());
}

@Test
public void notifyWhenSwitchingFromPendingToNegative() throws FalseInformedException {

TestResult existing = new TestResult();
existing.setContact("some@mail.com");
existing.setNotified(false);
existing.setStatus(Result.PENDING);

TestResult tr = new TestResult();
tr.setContact("some@mail.com");
tr.setNotified(false);
tr.setStatus(Result.NEGATIVE);

when(testResultService.getTestResult(eq("123"))).thenReturn(Optional.of(existing));

controller.addTestResult("123", tr);
//check existing
verify(testResultService,times(1)).getTestResult(eq("123"));
//do push
verify(pushService, times(1)).executePush(any());
//save testresult
verify(testResultService,times(1)).createTestProcess(any());
}

}
Loading

0 comments on commit cf610bb

Please sign in to comment.