public void Routes_should_be_one_way() { var expectedOriginBody = "Kerbin"; var expectedOriginBiome = "LaunchPad"; var expectedDestinationBody = "Mun"; var expectedDestinationBiome = "East Crater"; var expectedPayload = 5; var persistenceLayer = new TestPersister(); var expectedOriginDepot = persistenceLayer.CreateDepot(expectedOriginBody, expectedOriginBiome); var expectedDestinationDepot = persistenceLayer.CreateDepot(expectedDestinationBody, expectedDestinationBiome); persistenceLayer.CreateRoute( expectedOriginBody, expectedOriginBiome, expectedDestinationBody, expectedDestinationBiome, expectedPayload); var reverseRoute = persistenceLayer.GetRoute( expectedDestinationBody, expectedDestinationBiome, expectedOriginBody, expectedOriginBiome); Assert.Null(reverseRoute); }
public void Can_assign_resources_to_a_route() { var expectedOriginBody = "Kerbin"; var expectedOriginBiome = "LaunchPad"; var expectedDestinationBody = "Mun"; var expectedDestinationBiome = "East Crater"; var expectedPayload = 5; var persistenceLayer = new TestPersister(); var expectedOriginDepot = persistenceLayer.CreateDepot(expectedOriginBody, expectedOriginBiome); var expectedDestinationDepot = persistenceLayer.CreateDepot(expectedDestinationBody, expectedDestinationBiome); var route = new TestRoute( expectedOriginBody, expectedOriginBiome, expectedDestinationBody, expectedDestinationBiome, expectedPayload, persistenceLayer); var expectedResource = "Ore"; var expectedQuantity = 5; var startingResources = new Dictionary <string, int> { { expectedResource, expectedQuantity } }; expectedOriginDepot.NegotiateProvider(startingResources); var result = route.AddResource(expectedResource, expectedQuantity); Assert.IsType <OkNegotiationResult>(result); Assert.True(route.Resources.ContainsKey(expectedResource)); Assert.Equal(expectedQuantity, route.Resources[expectedResource]); }
public void Routes_require_established_depots_at_origin_and_destination(string missingDepot) { var expectedOriginBody = "Kerbin"; var expectedOriginBiome = "LaunchPad"; var expectedDestinationBody = "Mun"; var expectedDestinationBiome = "East Crater"; var expectedPayload = 5; var persistenceLayer = new TestPersister(); switch (missingDepot) { case "origin": persistenceLayer.CreateDepot(expectedDestinationBody, expectedDestinationBiome); break; case "destination": persistenceLayer.CreateDepot(expectedOriginBody, expectedOriginBiome); break; } Assert.Throws <DepotDoesNotExistException>(() => { persistenceLayer.CreateRoute( expectedOriginBody, expectedOriginBiome, expectedDestinationBody, expectedDestinationBiome, expectedPayload); }); }
public void Route_payload_should_be_obeyed() { var expectedOriginBody = "Kerbin"; var expectedOriginBiome = "LaunchPad"; var expectedDestinationBody = "Mun"; var expectedDestinationBiome = "East Crater"; var expectedPayload = 5; var persistenceLayer = new TestPersister(); var expectedOriginDepot = persistenceLayer.CreateDepot(expectedOriginBody, expectedOriginBiome); var expectedDestinationDepot = persistenceLayer.CreateDepot(expectedDestinationBody, expectedDestinationBiome); var route = new TestRoute( expectedOriginBody, expectedOriginBiome, expectedDestinationBody, expectedDestinationBiome, expectedPayload, persistenceLayer); var expectedResource = "Ore"; var expectedQuantity = 5; var startingResources = new Dictionary <string, int> { { expectedResource, expectedQuantity } }; expectedOriginDepot.NegotiateProvider(startingResources); var result = route.AddResource(expectedResource, expectedQuantity + 1); Assert.IsType <InsufficientPayloadNegotiationResult>(result); Assert.False(route.Resources.ContainsKey(expectedResource)); }
public void Origin_depot_must_have_requsted_resource() { var expectedOriginBody = "Kerbin"; var expectedOriginBiome = "LaunchPad"; var expectedDestinationBody = "Mun"; var expectedDestinationBiome = "East Crater"; var expectedPayload = 5; var persistenceLayer = new TestPersister(); var expectedOriginDepot = persistenceLayer.CreateDepot(expectedOriginBody, expectedOriginBiome); var expectedDestinationDepot = persistenceLayer.CreateDepot(expectedDestinationBody, expectedDestinationBiome); var route = new TestRoute( expectedOriginBody, expectedOriginBiome, expectedDestinationBody, expectedDestinationBiome, expectedPayload, persistenceLayer); var expectedResource = "Ore"; var requestedQuantity = 5; var result = route.AddResource(expectedResource, requestedQuantity); Assert.IsType <FailedNegotiationResult>(result); Assert.False(route.Resources.ContainsKey(expectedResource)); }
public void Routes_should_have_an_origin_destination_and_payload() { var expectedOriginBody = "Kerbin"; var expectedOriginBiome = "LaunchPad"; var expectedDestinationBody = "Mun"; var expectedDestinationBiome = "East Crater"; var expectedPayload = 5; var persistenceLayer = new TestPersister(); var expectedOriginDepot = persistenceLayer.CreateDepot(expectedOriginBody, expectedOriginBiome); var expectedDestinationDepot = persistenceLayer.CreateDepot(expectedDestinationBody, expectedDestinationBiome); var route = new TestRoute( expectedOriginBody, expectedOriginBiome, expectedDestinationBody, expectedDestinationBiome, expectedPayload, persistenceLayer); Assert.NotNull(route); Assert.Equal(expectedOriginBody, route.OriginBody); Assert.Equal(expectedOriginBiome, route.OriginBiome); Assert.Equal(expectedDestinationBody, route.DestinationBody); Assert.Equal(expectedDestinationBiome, route.DestinationBiome); Assert.Equal(expectedPayload, route.Payload); Assert.Equal(expectedOriginDepot, route.OriginDepot); Assert.Equal(expectedDestinationDepot, route.DestinationDepot); }
public void Should_not_allow_multiple_depots_in_the_same_biome() { var registry = new TestPersister(); var expectedBody = "Mun"; var expectedBiome = "East Crater"; var firstDepot = registry.CreateDepot(expectedBody, expectedBiome); var secondDepot = registry.CreateDepot(expectedBody, expectedBiome); Assert.Same(firstDepot, secondDepot); var depots = registry.Depots.Where(d => d.Body == expectedBody && d.Biome == expectedBiome); Assert.True(depots.Count() == 1); }
public void Should_not_allow_changes_if_destination_dependencies_would_break() { var expectedOriginBody = "Kerbin"; var expectedOriginBiome = "LaunchPad"; var expectedDestinationBody = "Mun"; var expectedDestinationBiome = "East Crater"; var expectedPayload = 5; var persistenceLayer = new TestPersister(); var expectedOriginDepot = persistenceLayer.CreateDepot(expectedOriginBody, expectedOriginBiome); var expectedDestinationDepot = persistenceLayer.CreateDepot(expectedDestinationBody, expectedDestinationBiome); var route = new TestRoute( expectedOriginBody, expectedOriginBiome, expectedDestinationBody, expectedDestinationBiome, expectedPayload, persistenceLayer); var expectedResource = "Ore"; var providedQuantity = 5; var usedQuantity = 3; var startingResources = new Dictionary <string, int> { { expectedResource, providedQuantity } }; expectedOriginDepot.NegotiateProvider(startingResources); var addResult = route.AddResource(expectedResource, providedQuantity); var consumedResources = new Dictionary <string, int> { { expectedResource, usedQuantity } }; expectedDestinationDepot.NegotiateConsumer(consumedResources); var removeResult = route.RemoveResource(expectedResource, providedQuantity); Assert.IsType <OkNegotiationResult>(addResult); Assert.IsType <BrokenNegotiationResult>(removeResult); Assert.True(route.Resources.ContainsKey(expectedResource)); Assert.Equal(providedQuantity, route.Resources[expectedResource]); var depotResources = expectedDestinationDepot.GetResources() .Where(r => r.ResourceName == expectedResource) .FirstOrDefault(); Assert.Equal(usedQuantity, depotResources.Outgoing); }
public void Can_add_depot_to_registry() { var registry = new TestPersister(); var expectedBody = "Mun"; var expectedBiome = "East Crater"; var depot = registry.CreateDepot(expectedBody, expectedBiome); Assert.Contains(depot, registry.Depots); }
public void Can_find_all_depots() { var registry = new TestPersister(); var expectedBody = "Mun"; var expectedBiome1 = "East Crater"; var expectedBiome2 = "Farside Crater"; var expectedDepot1 = registry.CreateDepot(expectedBody, expectedBiome1); var expectedDepot2 = registry.CreateDepot(expectedBody, expectedBiome2); expectedDepot1.Establish(); expectedDepot2.Establish(); var depots = registry.GetDepots(); // Should return a copy of its depot list, not its internal list // Note: XUnit's Equal assertion is List-aware, so we need to use the NotSame // assertion to do a referential comparison. Assert.NotSame(registry.Depots, depots); Assert.Equal(registry.Depots, depots); }
public void Routes_require_a_nonzero_payload() { var expectedOriginBody = "Kerbin"; var expectedOriginBiome = "LaunchPad"; var expectedDestinationBody = "Mun"; var expectedDestinationBiome = "East Crater"; var expectedPayload = 0; var persistenceLayer = new TestPersister(); var expectedOriginDepot = persistenceLayer.CreateDepot(expectedOriginBody, expectedOriginBiome); var expectedDestinationDepot = persistenceLayer.CreateDepot(expectedDestinationBody, expectedDestinationBiome); Assert.Throws <RouteInsufficientPayloadException>(() => { var route = new TestRoute( expectedOriginBody, expectedOriginBiome, expectedDestinationBody, expectedDestinationBiome, expectedPayload, persistenceLayer); }); }
public void Origin_depot_availability_should_be_obeyed() { var expectedOriginBody = "Kerbin"; var expectedOriginBiome = "LaunchPad"; var expectedDestinationBody = "Mun"; var expectedDestinationBiome = "East Crater"; var expectedPayload = 5; var persistenceLayer = new TestPersister(); var expectedOriginDepot = persistenceLayer.CreateDepot(expectedOriginBody, expectedOriginBiome); var expectedDestinationDepot = persistenceLayer.CreateDepot(expectedDestinationBody, expectedDestinationBiome); var route = new TestRoute( expectedOriginBody, expectedOriginBiome, expectedDestinationBody, expectedDestinationBiome, expectedPayload, persistenceLayer); var expectedResource = "Ore"; var requestedQuantity = 5; var availableQuantity = 4; var startingResources = new Dictionary <string, int> { { expectedResource, availableQuantity } }; expectedOriginDepot.NegotiateProvider(startingResources); var result = route.AddResource(expectedResource, requestedQuantity); Assert.IsType <FailedNegotiationResult>(result); Assert.False(route.Resources.ContainsKey(expectedResource)); var depotResource = expectedOriginDepot.GetResources() .Where(r => r.ResourceName == expectedResource) .FirstOrDefault(); Assert.Equal(availableQuantity, depotResource.Incoming); Assert.Equal(0, depotResource.Outgoing); Assert.Equal(availableQuantity, depotResource.Available); }
public void Can_remove_route_resources(int startingPayload, int amountToRemove) { var expectedOriginBody = "Kerbin"; var expectedOriginBiome = "LaunchPad"; var expectedDestinationBody = "Mun"; var expectedDestinationBiome = "East Crater"; var persistenceLayer = new TestPersister(); var expectedOriginDepot = persistenceLayer.CreateDepot(expectedOriginBody, expectedOriginBiome); var expectedDestinationDepot = persistenceLayer.CreateDepot(expectedDestinationBody, expectedDestinationBiome); var route = new TestRoute( expectedOriginBody, expectedOriginBiome, expectedDestinationBody, expectedDestinationBiome, startingPayload, persistenceLayer); var expectedResource = "Ore"; var startingResources = new Dictionary <string, int> { { expectedResource, startingPayload } }; expectedOriginDepot.NegotiateProvider(startingResources); var addResult = route.AddResource(expectedResource, startingPayload); var removeResult = route.RemoveResource(expectedResource, amountToRemove); Assert.IsType <OkNegotiationResult>(addResult); Assert.IsType <OkNegotiationResult>(removeResult); if (startingPayload == amountToRemove) { Assert.False(route.Resources.ContainsKey(expectedResource)); } else { Assert.True(route.Resources.ContainsKey(expectedResource)); Assert.Equal(startingPayload - amountToRemove, route.Resources[expectedResource]); } }
public void Route_resources_should_be_added_to_destination_depot() { var expectedOriginBody = "Kerbin"; var expectedOriginBiome = "LaunchPad"; var expectedDestinationBody = "Mun"; var expectedDestinationBiome = "East Crater"; var expectedPayload = 5; var persistenceLayer = new TestPersister(); var expectedOriginDepot = persistenceLayer.CreateDepot(expectedOriginBody, expectedOriginBiome); var expectedDestinationDepot = persistenceLayer.CreateDepot(expectedDestinationBody, expectedDestinationBiome); var route = new TestRoute( expectedOriginBody, expectedOriginBiome, expectedDestinationBody, expectedDestinationBiome, expectedPayload, persistenceLayer); var expectedResource = "Ore"; var expectedQuantity = 5; var startingResources = new Dictionary <string, int> { { expectedResource, expectedQuantity } }; expectedOriginDepot.NegotiateProvider(startingResources); var result = route.AddResource(expectedResource, expectedQuantity); Assert.IsType <OkNegotiationResult>(result); var depotResources = expectedDestinationDepot.GetResources(); var resource = depotResources .Where(r => r.ResourceName == expectedResource) .FirstOrDefault(); Assert.Equal(expectedQuantity, resource.Incoming); Assert.Equal(0, resource.Outgoing); Assert.Equal(expectedQuantity, resource.Available); }
public void Can_find_a_depot() { var registry = new TestPersister(); var expectedBody = "Mun"; var expectedBiome = "East Crater"; var createdDepot = registry.CreateDepot(expectedBody, expectedBiome); createdDepot.Establish(); var hasDepot = registry.HasEstablishedDepot(expectedBody, expectedBiome); var depot = registry.GetDepot(expectedBody, expectedBiome); Assert.True(hasDepot); Assert.Equal(expectedBody, depot.Body); Assert.Equal(expectedBiome, depot.Biome); }
public void Routes_should_be_persisted() { var configNode = new ConfigNode(); var persister = new TestPersister(); var originBody = "Mun"; var originBiome = "East Crater"; var destinationBody = "Minmus"; var destinationBiome = "Greater Flats"; var payload = 12; var resourceName1 = "SpecializedParts"; var quantity1 = 8; var resourceName2 = "ColonySupplies"; var quantity2 = 4; var originDepot = persister.CreateDepot(originBody, originBiome); persister.CreateDepot(destinationBody, destinationBiome); var startingResources = new Dictionary <string, int> { { resourceName1, quantity1 }, { resourceName2, quantity2 } }; originDepot.NegotiateProvider(startingResources); var route = persister.CreateRoute(originBody, originBiome, destinationBody, destinationBiome, payload); route.AddResource(resourceName1, quantity1); route.AddResource(resourceName2, quantity2); persister.OnSave(configNode); Assert.True(configNode.HasNode(ScenarioPersister.ROUTES_NODE_NAME)); var wolfNode = configNode.GetNode(ScenarioPersister.ROUTES_NODE_NAME); Assert.True(wolfNode.HasData); var routeNodes = wolfNode.GetNodes(); var routeNode = routeNodes.First(); Assert.True(routeNode.HasValue("OriginBody")); Assert.True(routeNode.HasValue("OriginBiome")); Assert.True(routeNode.HasValue("DestinationBody")); Assert.True(routeNode.HasValue("DestinationBiome")); Assert.True(routeNode.HasValue("Payload")); var originBodyValue = routeNode.GetValue("OriginBody"); var originBiomeValue = routeNode.GetValue("OriginBiome"); var destinationBodyValue = routeNode.GetValue("DestinationBody"); var destinationBiomeValue = routeNode.GetValue("DestinationBiome"); var payloadValue = int.Parse(routeNode.GetValue("Payload")); Assert.Equal(originBody, originBodyValue); Assert.Equal(originBiome, originBiomeValue); Assert.Equal(destinationBody, destinationBodyValue); Assert.Equal(destinationBiome, destinationBiomeValue); Assert.Equal(payload, payloadValue); Assert.True(routeNode.HasNode("RESOURCE")); var resourceNodes = routeNode.GetNodes(); Assert.Equal(2, resourceNodes.Length); var resourceNode = resourceNodes[0]; Assert.True(resourceNode.HasValue("ResourceName")); Assert.True(resourceNode.HasValue("Quantity")); var nodeResourceName = resourceNode.GetValue("ResourceName"); var nodeQuantityValue = int.Parse(resourceNode.GetValue("Quantity")); Assert.Equal(resourceName1, nodeResourceName); Assert.Equal(quantity1, nodeQuantityValue); }