-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add SettlementEntityManagerTest for placeParcel
- Loading branch information
Showing
7 changed files
with
176 additions
and
70 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
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
114 changes: 114 additions & 0 deletions
114
src/test/java/org/terasology/dynamicCities/settlements/SettlementEntityManagerTest.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,114 @@ | ||
// Copyright 2021 The Terasology Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package org.terasology.dynamicCities.settlements; | ||
|
||
import org.joml.RoundingMode; | ||
import org.joml.Vector2i; | ||
import org.joml.Vector2ic; | ||
import org.joml.Vector3fc; | ||
import org.joml.Vector3i; | ||
import org.joml.Vector3ic; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.terasology.dynamicCities.buildings.BuildingManager; | ||
import org.terasology.dynamicCities.buildings.BuildingQueue; | ||
import org.terasology.dynamicCities.districts.DistrictManager; | ||
import org.terasology.dynamicCities.districts.DistrictType; | ||
import org.terasology.dynamicCities.parcels.DynParcel; | ||
import org.terasology.dynamicCities.parcels.ParcelList; | ||
import org.terasology.dynamicCities.population.CultureComponent; | ||
import org.terasology.dynamicCities.population.CultureManager; | ||
import org.terasology.dynamicCities.settlements.components.DistrictFacetComponent; | ||
import org.terasology.dynamicCities.sites.SiteComponent; | ||
import org.terasology.engine.context.Context; | ||
import org.terasology.engine.entitySystem.entity.EntityManager; | ||
import org.terasology.engine.entitySystem.entity.EntityRef; | ||
import org.terasology.engine.logic.location.LocationComponent; | ||
import org.terasology.engine.registry.In; | ||
import org.terasology.engine.registry.InjectionHelper; | ||
import org.terasology.moduletestingenvironment.MTEExtension; | ||
import org.terasology.moduletestingenvironment.extension.Dependencies; | ||
import org.terasology.namegenerator.town.TownAssetTheme; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import static com.google.common.truth.Truth8.assertThat; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.when; | ||
|
||
@Tag("MteTest") | ||
@ExtendWith(MTEExtension.class) | ||
@Dependencies("DynamicCities") | ||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
class SettlementEntityManagerTest { | ||
|
||
static final int MAX_PLACEMENT_ATTEMPTS = 20; | ||
static final String zone = "TestZone"; | ||
static final Vector2ic buildingSize = new Vector2i(9, 9); | ||
static final Vector3ic siteLocation = new Vector3i(1234, 0, -5678); | ||
static final int population = 3; | ||
|
||
@In | ||
Context context; | ||
|
||
@BeforeAll | ||
void initialiseZones() { | ||
// FIXME: mockito kludge to give us a way to define buildings. | ||
// Should have a way for this test to add a building prefab, | ||
// or a way to add to BuildingManager without prefabs. | ||
BuildingManager buildingManager = spy(context.get(BuildingManager.class)); | ||
when(buildingManager.getMinMaxSizePerZone()).thenReturn(Map.of(zone, List.of(buildingSize, buildingSize))); | ||
context.put(BuildingManager.class, buildingManager); | ||
|
||
// re-inject this so it has the mock version. | ||
InjectionHelper.inject(context.get(SettlementEntityManager.class), context); | ||
} | ||
|
||
@BeforeAll | ||
void initialiseCulture(CultureManager cultures) { | ||
CultureComponent culture = new CultureComponent(); | ||
culture.name = "Testers"; | ||
culture.theme = TownAssetTheme.FANTASY.name(); | ||
culture.availableBuildings.add("Test Building"); | ||
culture.buildingNeedPerZone.put(zone, 1f); | ||
culture.residentialZones.add(zone); | ||
|
||
cultures.addCulture(culture); | ||
} | ||
|
||
@BeforeAll | ||
void initialiseDistricts(DistrictManager districts) { | ||
districts.addDistrict(new DistrictType("The District", zone)); | ||
} | ||
|
||
EntityRef newSite() { | ||
SiteComponent site = new SiteComponent(siteLocation.x(), siteLocation.z()); | ||
return context.get(EntityManager.class).create( | ||
site, | ||
new SettlementComponent(site, population), | ||
new LocationComponent(siteLocation) | ||
); | ||
} | ||
|
||
@Test | ||
void placeParcel(SettlementEntityManager manager) { | ||
ParcelList parcels = new ParcelList(); | ||
BuildingQueue buildingQueue = new BuildingQueue(); | ||
|
||
EntityRef site = newSite(); | ||
EntityRef settlement = manager.createSettlement(site); | ||
Vector3fc center = settlement.getComponent(LocationComponent.class).getLocalPosition(); | ||
|
||
Optional<DynParcel> parcel = manager.placeParcel( | ||
new Vector3i(center, RoundingMode.FLOOR), zone, parcels, buildingQueue, | ||
settlement.getComponent(DistrictFacetComponent.class), MAX_PLACEMENT_ATTEMPTS | ||
); | ||
assertThat(parcel).isPresent(); | ||
} | ||
} |