-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add testcases, make code more robust
- Loading branch information
Thomas
committed
Mar 27, 2020
1 parent
31673f4
commit cf610bb
Showing
7 changed files
with
351 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/test/java/de/wirvsvirus/testresult/backend/rest/TestResultControllerIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))); | ||
}); | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
...test/java/de/wirvsvirus/testresult/backend/rest/TestResultControllerNotifictaionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
Oops, something went wrong.