introduced HsHostingAssetsControllerRestTest

This commit is contained in:
Michael Hoennig 2024-07-02 17:31:54 +02:00
parent fcbbd13a7e
commit c3fda4e4b2

View File

@ -0,0 +1,107 @@
package net.hostsharing.hsadminng.hs.hosting.asset;
import static net.hostsharing.hsadminng.rbac.test.JsonMatcher.lenientlyEquals;
import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.hs.booking.item.HsBookingItemRepository;
import net.hostsharing.hsadminng.mapper.Mapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.SynchronizationType;
import java.util.Map;
import static org.hamcrest.Matchers.is;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(HsHostingAssetController.class)
@Import(Mapper.class)
@RunWith(SpringRunner.class)
public class HsHostingAssetsControllerRestTest {
@Autowired
MockMvc mockMvc;
@MockBean
Context contextMock;
@MockBean
Mapper mapper;
@Mock
private EntityManager em;
@MockBean
EntityManagerFactory emf;
@MockBean
private HsBookingItemRepository bookingItemRepo;
@MockBean
private HsHostingAssetRepository hostingAssetRepo;
enum ListTestCases {
MANAGED_SERVER(
"?type=EMAIL_ALIAS",
"""
{
"transactionType": "DEPOSIT",
"assetValue": 128.00,
"valueDate": "2022-10-13",
"reference": "valid reference",
"comment": "valid comment"
}
""");
final String queryString;
final String expectedResponse;
ListTestCases(final String queryString, final String expectedResponse) {
this.queryString = queryString;
this.expectedResponse = expectedResponse;
}
}
@BeforeEach
void init() {
when(emf.createEntityManager()).thenReturn(em);
when(emf.createEntityManager(any(Map.class))).thenReturn(em);
when(emf.createEntityManager(any(SynchronizationType.class))).thenReturn(em);
when(emf.createEntityManager(any(SynchronizationType.class), any(Map.class))).thenReturn(em);
}
@ParameterizedTest
@EnumSource(HsHostingAssetsControllerRestTest.ListTestCases.class)
void shouldListAssets(final HsHostingAssetsControllerRestTest.ListTestCases testCase) throws Exception {
// when
mockMvc.perform(MockMvcRequestBuilders
.get("/api/hs/hosting/assets"+testCase.queryString)
.header("current-user", "superuser-alex@hostsharing.net")
//.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
// then
.andExpect(status().is2xxSuccessful())
.andExpect(jsonPath("statusCode", is(200)))
.andExpect(jsonPath("body", lenientlyEquals(testCase.expectedResponse)));
}
}