Skip to content

Commit

Permalink
add and improve tests (#910)
Browse files Browse the repository at this point in the history
  • Loading branch information
goekay committed Dec 3, 2022
1 parent ba78860 commit 0e9c579
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
import de.rwth.idsg.steve.service.OcppTagService;
import de.rwth.idsg.steve.utils.DateTimeUtils;
import de.rwth.idsg.steve.web.dto.OcppTagForm;
import de.rwth.idsg.steve.web.dto.OcppTagQueryForm;
import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
Expand All @@ -41,6 +43,7 @@
import java.util.List;

import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyList;
Expand Down Expand Up @@ -176,6 +179,7 @@ public void test5() throws Exception {
.andExpect(jsonPath("$[0].inTransaction").value("false"))
.andExpect(jsonPath("$[0].blocked").value("true"))
.andExpect(jsonPath("$[0].expiryDate").value("2020-10-01T00:00:00.000Z"))
.andExpect(jsonPath("$[0].expiryDateFormatted").doesNotExist())
.andExpect(jsonPath("$[0].maxActiveTransactionCount").value("4"))
.andExpect(jsonPath("$[0].activeTransactionCount").value("0"))
.andExpect(jsonPath("$[0].note").value("some note"));
Expand Down Expand Up @@ -424,6 +428,97 @@ public void test17() throws Exception {
verify(ocppTagRepository, times(0)).deleteOcppTag(anyInt());
}

@Test
@DisplayName("POST: Cannot create because entity exists already, returns 422")
public void test18() throws Exception {
// given
int ocppTagPk = 123;

OcppTagForm form = new OcppTagForm();
form.setIdTag("id-123");

// when
when(ocppTagRepository.addOcppTag(eq(form))).thenThrow(new SteveException.AlreadyExists("A user with idTag '%s' already exists.", ocppTagPk));

// then
mockMvc.perform(
post("/api/v1/ocppTags")
.content(objectMapper.writeValueAsString(form))
.contentType(CONTENT_TYPE)
)
.andExpect(status().isUnprocessableEntity())
.andExpectAll(errorJsonMatchers());

verify(ocppTagService, times(0)).removeUnknown(anyList());
verify(ocppTagRepository, times(0)).getOverview(any(OcppTagQueryForm.ForApi.class));
}

@Test
@DisplayName("GET all: Query param 'expired' is translated correctly, while others are defaulted")
public void test19() throws Exception {
// given
ArgumentCaptor<OcppTagQueryForm.ForApi> formToCapture = ArgumentCaptor.forClass(OcppTagQueryForm.ForApi.class);

// when
when(ocppTagRepository.getOverview(any())).thenReturn(Collections.emptyList());

// then
mockMvc.perform(get("/api/v1/ocppTags")
.param("expired", "FALSE"))
.andExpect(status().isOk());

verify(ocppTagRepository).getOverview(formToCapture.capture());
OcppTagQueryForm.ForApi capturedForm = formToCapture.getValue();

assertEquals(capturedForm.getExpired(), OcppTagQueryForm.BooleanType.FALSE);
assertEquals(capturedForm.getInTransaction(), OcppTagQueryForm.BooleanType.ALL);
assertEquals(capturedForm.getBlocked(), OcppTagQueryForm.BooleanType.ALL);
}

@Test
@DisplayName("GET all: Query param 'inTransaction' is translated correctly, while others are defaulted")
public void test20() throws Exception {
// given
ArgumentCaptor<OcppTagQueryForm.ForApi> formToCapture = ArgumentCaptor.forClass(OcppTagQueryForm.ForApi.class);

// when
when(ocppTagRepository.getOverview(any())).thenReturn(Collections.emptyList());

// then
mockMvc.perform(get("/api/v1/ocppTags")
.param("inTransaction", "TRUE"))
.andExpect(status().isOk());

verify(ocppTagRepository).getOverview(formToCapture.capture());
OcppTagQueryForm.ForApi capturedForm = formToCapture.getValue();

assertEquals(capturedForm.getExpired(), OcppTagQueryForm.BooleanType.ALL);
assertEquals(capturedForm.getInTransaction(), OcppTagQueryForm.BooleanType.TRUE);
assertEquals(capturedForm.getBlocked(), OcppTagQueryForm.BooleanType.ALL);
}

@Test
@DisplayName("GET all: Query param 'inTransaction' is translated correctly, while others are defaulted")
public void test21() throws Exception {
// given
ArgumentCaptor<OcppTagQueryForm.ForApi> formToCapture = ArgumentCaptor.forClass(OcppTagQueryForm.ForApi.class);

// when
when(ocppTagRepository.getOverview(any())).thenReturn(Collections.emptyList());

// then
mockMvc.perform(get("/api/v1/ocppTags")
.param("blocked", "FALSE"))
.andExpect(status().isOk());

verify(ocppTagRepository).getOverview(formToCapture.capture());
OcppTagQueryForm.ForApi capturedForm = formToCapture.getValue();

assertEquals(capturedForm.getExpired(), OcppTagQueryForm.BooleanType.ALL);
assertEquals(capturedForm.getInTransaction(), OcppTagQueryForm.BooleanType.ALL);
assertEquals(capturedForm.getBlocked(), OcppTagQueryForm.BooleanType.FALSE);
}

private static ResultMatcher[] errorJsonMatchers() {
return new ResultMatcher[]{
jsonPath("$.timestamp").exists(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@

import de.rwth.idsg.steve.repository.TransactionRepository;
import de.rwth.idsg.steve.repository.dto.Transaction;
import de.rwth.idsg.steve.web.dto.TransactionQueryForm;
import org.joda.time.DateTime;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
Expand All @@ -35,7 +38,9 @@
import java.util.List;

import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
Expand Down Expand Up @@ -144,8 +149,8 @@ public void test6() throws Exception {
public void test7() throws Exception {
mockMvc.perform(get("/api/v1/transactions")
.param("periodType", "FROM_TO")
.param("from", "2022-10-01 00:00")
.param("to", "2022-09-01 00:00")
.param("from", "2022-10-01T00:00")
.param("to", "2022-09-01T00:00")
)
.andExpect(status().isBadRequest())
.andExpectAll(errorJsonMatchers());
Expand All @@ -154,9 +159,14 @@ public void test7() throws Exception {
@Test
@DisplayName("Sets all valid params, expected 200")
public void test8() throws Exception {
DateTime start = DateTime.parse("2022-10-01T00:00Z");
DateTime stop = start.plusHours(2);

// given
Transaction transaction = Transaction
.builder()
.startTimestamp(start)
.stopTimestamp(stop)
.id(1)
.chargeBoxId("cb-2")
.ocppIdTag("id-3")
Expand All @@ -179,9 +189,67 @@ public void test8() throws Exception {
.andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$[0].id").value("1"))
.andExpect(jsonPath("$[0].chargeBoxId").value("cb-2"))
.andExpect(jsonPath("$[0].ocppIdTag").value("id-3"));
.andExpect(jsonPath("$[0].ocppIdTag").value("id-3"))
.andExpect(jsonPath("$[0].ocppIdTag").value("id-3"))
.andExpect(jsonPath("$[0].startTimestamp").value(start.toString()))
.andExpect(jsonPath("$[0].startTimestampFormatted").doesNotExist())
.andExpect(jsonPath("$[0].stopTimestamp").value(stop.toString()))
.andExpect(jsonPath("$[0].stopTimestampFormatted").doesNotExist());
}

@Test
@DisplayName("from and to have are not conform with ISO")
public void test9() throws Exception {
mockMvc.perform(get("/api/v1/transactions")
.param("periodType", "FROM_TO")
.param("from", "2022-10-01 00:00")
.param("to", "2023-10-01 00:00")
)
.andExpect(status().isBadRequest())
.andExpectAll(errorJsonMatchers());
}

@Test
@DisplayName("GET all: Query param 'type' is translated correctly, while others are defaulted")
public void test10() throws Exception {
// given
ArgumentCaptor<TransactionQueryForm.ForApi> formToCapture = ArgumentCaptor.forClass(TransactionQueryForm.ForApi.class);

// when
when(transactionRepository.getTransactions(any())).thenReturn(Collections.emptyList());

// then
mockMvc.perform(get("/api/v1/transactions")
.param("type", "ACTIVE"))
.andExpect(status().isOk());

verify(transactionRepository).getTransactions(formToCapture.capture());
TransactionQueryForm.ForApi capturedForm = formToCapture.getValue();

assertEquals(capturedForm.getType(), TransactionQueryForm.QueryType.ACTIVE);
assertEquals(capturedForm.getPeriodType(), TransactionQueryForm.QueryPeriodType.ALL);
}

@Test
@DisplayName("GET all: Query param 'periodType' is translated correctly, while others are defaulted")
public void test11() throws Exception {
// given
ArgumentCaptor<TransactionQueryForm.ForApi> formToCapture = ArgumentCaptor.forClass(TransactionQueryForm.ForApi.class);

// when
when(transactionRepository.getTransactions(any())).thenReturn(Collections.emptyList());

// then
mockMvc.perform(get("/api/v1/transactions")
.param("periodType", "LAST_30"))
.andExpect(status().isOk());

verify(transactionRepository).getTransactions(formToCapture.capture());
TransactionQueryForm.ForApi capturedForm = formToCapture.getValue();

assertEquals(capturedForm.getType(), TransactionQueryForm.QueryType.ALL);
assertEquals(capturedForm.getPeriodType(), TransactionQueryForm.QueryPeriodType.LAST_30);
}

private static ResultMatcher[] errorJsonMatchers() {
return new ResultMatcher[] {
Expand Down

0 comments on commit 0e9c579

Please sign in to comment.