added some other asset types

This commit is contained in:
Michael Hoennig 2024-07-03 10:26:31 +02:00
parent 814de9598d
commit b2b441dafa
5 changed files with 260 additions and 125 deletions

View File

@ -159,6 +159,6 @@ public class HsHostingAssetController implements HsHostingAssetsApi {
@SuppressWarnings("unchecked")
final BiConsumer<HsHostingAssetEntity, HsHostingAssetResource> ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource)
-> HsHostingAssetEntityValidatorRegistry.forType(entity.getType())
.revampProperties(entity, (Map<String, Object>) resource.getConfig());
-> resource.setConfig(HsHostingAssetEntityValidatorRegistry.forType(entity.getType())
.revampProperties(entity, (Map<String, Object>) resource.getConfig()));
}

View File

@ -0,0 +1,234 @@
package net.hostsharing.hsadminng.hs.hosting.asset;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.hs.booking.item.HsBookingItemRepository;
import net.hostsharing.hsadminng.mapper.Array;
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.List;
import java.util.Map;
import static java.util.Map.entry;
import static net.hostsharing.hsadminng.hs.booking.item.TestHsBookingItem.TEST_CLOUD_SERVER_BOOKING_ITEM;
import static net.hostsharing.hsadminng.hs.booking.item.TestHsBookingItem.TEST_MANAGED_SERVER_BOOKING_ITEM;
import static net.hostsharing.hsadminng.hs.hosting.asset.TestHsHostingAssetEntities.TEST_MANAGED_WEBSPACE_HOSTING_ASSET;
import static net.hostsharing.hsadminng.hs.office.contact.TestHsOfficeContact.TEST_CONTACT;
import static net.hostsharing.hsadminng.rbac.test.JsonMatcher.lenientlyEquals;
import static net.hostsharing.hsadminng.rbac.test.JsonMatcher.strictlyEquals;
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 HsHostingAssetControllerRestTest {
@Autowired
MockMvc mockMvc;
@MockBean
Context contextMock;
@Autowired
Mapper mapper;
@Mock
private EntityManager em;
@MockBean
EntityManagerFactory emf;
@MockBean
@SuppressWarnings("unused") // bean needs to be present for HsHostingAssetController
private HsBookingItemRepository bookingItemRepo;
@MockBean
private HsHostingAssetRepository hostingAssetRepo;
enum ListTestCases {
CLOUD_SERVER(
List.of(
HsHostingAssetEntity.builder()
.type(HsHostingAssetType.CLOUD_SERVER)
.bookingItem(TEST_CLOUD_SERVER_BOOKING_ITEM)
.identifier("vm1234")
.caption("some fake cloud-server")
.alarmContact(TEST_CONTACT)
.build()),
"""
[
{
"type": "CLOUD_SERVER",
"identifier": "vm1234",
"caption": "some fake cloud-server",
"alarmContact": {
"caption": "some contact",
"postalAddress": "address of some contact",
"emailAddresses": {
"main": "some-contact@example.com"
}
},
"config": {}
}
]
"""),
MANAGED_SERVER(
List.of(
HsHostingAssetEntity.builder()
.type(HsHostingAssetType.MANAGED_SERVER)
.bookingItem(TEST_MANAGED_SERVER_BOOKING_ITEM)
.identifier("vm1234")
.caption("some fake managed-server")
.alarmContact(TEST_CONTACT)
.config(Map.ofEntries(
entry("monit_max_ssd_usage", 70),
entry("monit_max_cpu_usage", 80),
entry("monit_max_ram_usage", 90)
))
.build()),
"""
[
{
"type": "MANAGED_SERVER",
"identifier": "vm1234",
"caption": "some fake managed-server",
"alarmContact": {
"caption": "some contact",
"postalAddress": "address of some contact",
"emailAddresses": {
"main": "some-contact@example.com"
}
},
"config": {
"monit_max_ssd_usage": 70,
"monit_max_cpu_usage": 80,
"monit_max_ram_usage": 90
}
}
]
"""),
UNIX_USER(
List.of(
HsHostingAssetEntity.builder()
.type(HsHostingAssetType.UNIX_USER)
.parentAsset(TEST_MANAGED_WEBSPACE_HOSTING_ASSET)
.identifier("xyz00-office")
.caption("some fake Unix-User")
.config(Map.ofEntries(
entry("password", "$6$salt$hashed-salted-password"),
entry("totpKey", "0x0123456789abcdef"),
entry("shell", "/bin/bash"),
entry("SSD-soft-quota", 128),
entry("SSD-hard-quota", 256),
entry("HDD-soft-quota", 256),
entry("HDD-hard-quota", 512)))
.build()),
"""
[
{
"type": "UNIX_USER",
"identifier": "xyz00-office",
"caption": "some fake Unix-User",
"alarmContact": null,
"config": {
"SSD-soft-quota": 128,
"SSD-hard-quota": 256,
"HDD-soft-quota": 256,
"HDD-hard-quota": 512,
"shell": "/bin/bash",
"homedir": "/home/pacs/xyz00/users/office"
}
}
]
"""),
EMAIL_ALIAS(
List.of(
HsHostingAssetEntity.builder()
.type(HsHostingAssetType.EMAIL_ALIAS)
.parentAsset(TEST_MANAGED_WEBSPACE_HOSTING_ASSET)
.identifier("xyz00-office")
.caption("some fake EMail-Alias")
.config(Map.ofEntries(
entry("target", Array.of("xyz00", "xyz00-abc", "office@example.com"))
))
.build()),
"""
[
{
"type": "EMAIL_ALIAS",
"identifier": "xyz00-office",
"caption": "some fake EMail-Alias",
"alarmContact": null,
"config": {
"target": ["xyz00","xyz00-abc","office@example.com"]
}
}
]
""");
final HsHostingAssetType assetType;
final List<HsHostingAssetEntity> givenHostingAssetsOfType;
final String expectedResponse;
ListTestCases(
final List<HsHostingAssetEntity> givenHostingAssetsOfType,
final String expectedResponse) {
this.assetType = HsHostingAssetType.valueOf(name());
this.givenHostingAssetsOfType = givenHostingAssetsOfType;
this.expectedResponse = expectedResponse;
}
@SneakyThrows
String expectedConfig() {
// FIXME: iterate all indexes, not just 0
return new ObjectMapper().readTree(expectedResponse).get(0).path("config").toString();
}
}
@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(HsHostingAssetControllerRestTest.ListTestCases.class)
void shouldListAssets(final HsHostingAssetControllerRestTest.ListTestCases testCase) throws Exception {
// given
when(hostingAssetRepo.findAllByCriteria(null, null, testCase.assetType))
.thenReturn(testCase.givenHostingAssetsOfType);
// when
mockMvc.perform(MockMvcRequestBuilders
.get("/api/hs/hosting/assets?type="+testCase.name())
.header("current-user", "superuser-alex@hostsharing.net")
.accept(MediaType.APPLICATION_JSON))
// then
.andExpect(status().is2xxSuccessful())
.andExpect(jsonPath("$", lenientlyEquals(testCase.expectedResponse)))
.andExpect(jsonPath("[0].config", strictlyEquals(testCase.expectedConfig())));
}
}

View File

@ -1,107 +0,0 @@
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)));
}
}

View File

@ -0,0 +1,22 @@
package net.hostsharing.hsadminng.hs.hosting.asset;
import static net.hostsharing.hsadminng.hs.booking.item.TestHsBookingItem.TEST_MANAGED_SERVER_BOOKING_ITEM;
import static net.hostsharing.hsadminng.hs.booking.item.TestHsBookingItem.TEST_MANAGED_WEBSPACE_BOOKING_ITEM;
public class TestHsHostingAssetEntities {
public static final HsHostingAssetEntity TEST_MANAGED_SERVER_HOSTING_ASSET = HsHostingAssetEntity.builder()
.type(HsHostingAssetType.MANAGED_SERVER)
.identifier("vm1234")
.caption("some managed server")
.bookingItem(TEST_MANAGED_SERVER_BOOKING_ITEM)
.build();
public static final HsHostingAssetEntity TEST_MANAGED_WEBSPACE_HOSTING_ASSET = HsHostingAssetEntity.builder()
.type(HsHostingAssetType.MANAGED_WEBSPACE)
.identifier("xyz00")
.caption("some managed webspace")
.bookingItem(TEST_MANAGED_WEBSPACE_BOOKING_ITEM)
.build();
}

View File

@ -1,7 +1,6 @@
package net.hostsharing.hsadminng.hs.hosting.asset.validators;
import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetEntity;
import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType;
import net.hostsharing.hsadminng.mapper.Array;
import org.junit.jupiter.api.Test;
@ -9,26 +8,13 @@ import java.util.Map;
import static java.util.Map.entry;
import static net.hostsharing.hsadminng.hs.booking.item.TestHsBookingItem.TEST_MANAGED_SERVER_BOOKING_ITEM;
import static net.hostsharing.hsadminng.hs.booking.item.TestHsBookingItem.TEST_MANAGED_WEBSPACE_BOOKING_ITEM;
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.EMAIL_ALIAS;
import static net.hostsharing.hsadminng.hs.hosting.asset.TestHsHostingAssetEntities.TEST_MANAGED_SERVER_HOSTING_ASSET;
import static net.hostsharing.hsadminng.hs.hosting.asset.TestHsHostingAssetEntities.TEST_MANAGED_WEBSPACE_HOSTING_ASSET;
import static org.assertj.core.api.Assertions.assertThat;
class HsEMailAliasHostingAssetValidatorUnitTest {
private final HsHostingAssetEntity TEST_MANAGED_SERVER_HOSTING_ASSET = HsHostingAssetEntity.builder()
.type(HsHostingAssetType.MANAGED_SERVER)
.identifier("vm1234")
.caption("some managed server")
.bookingItem(TEST_MANAGED_SERVER_BOOKING_ITEM)
.build();
private final HsHostingAssetEntity TEST_MANAGED_WEBSPACE_HOSTING_ASSET = HsHostingAssetEntity.builder()
.type(HsHostingAssetType.MANAGED_WEBSPACE)
.identifier("xyz00")
.caption("some managed webspace")
.bookingItem(TEST_MANAGED_WEBSPACE_BOOKING_ITEM)
.build();
@Test
void containsAllValidations() {
// when