84 lines
3.6 KiB
Java
Raw Normal View History

/*
2018-07-02 00:42:04 +05:30
* The MIT License
2019-10-12 20:05:54 +03:00
* Copyright © 2014-2019 Ilkka Seppälä
2018-07-02 00:42:04 +05:30
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2018-07-02 00:42:04 +05:30
package com.iluwatar.collectionpipeline;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
2018-09-04 20:52:11 +05:30
import java.util.HashMap;
2018-07-02 00:42:04 +05:30
import java.util.List;
2018-08-26 23:12:33 +05:30
import java.util.Map;
2018-07-02 00:42:04 +05:30
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2018-07-02 00:42:04 +05:30
/**
* Tests that Collection Pipeline methods work as expected.
*/
public class AppTest {
private static final Logger LOGGER = LoggerFactory.getLogger(AppTest.class);
2018-08-26 23:12:33 +05:30
private List<Car> cars = CarFactory.createCars();
2018-07-02 00:42:04 +05:30
@Test
public void testGetModelsAfter2000UsingFor() {
2018-08-26 23:12:33 +05:30
List<String> models = ImperativeProgramming.getModelsAfter2000(cars);
2018-09-04 20:52:11 +05:30
assertEquals(Arrays.asList("Avenger", "Wrangler", "Focus", "Cascada"), models);
2018-07-02 00:42:04 +05:30
}
@Test
public void testGetModelsAfter2000UsingPipeline() {
2018-08-26 23:12:33 +05:30
List<String> models = FunctionalProgramming.getModelsAfter2000(cars);
2018-09-04 20:52:11 +05:30
assertEquals(Arrays.asList("Avenger", "Wrangler", "Focus", "Cascada"), models);
2018-07-02 00:42:04 +05:30
}
2018-08-26 23:12:33 +05:30
@Test
public void testGetGroupingOfCarsByCategory() {
2018-09-04 20:52:11 +05:30
Map<Category, List<Car>> modelsExpected = new HashMap<>();
modelsExpected.put(Category.CONVERTIBLE, Arrays.asList(new Car("Buick", "Cascada", 2016, Category.CONVERTIBLE),
new Car("Chevrolet", "Geo Metro", 1992, Category.CONVERTIBLE)));
modelsExpected.put(Category.SEDAN, Arrays.asList(new Car("Dodge", "Avenger", 2010, Category.SEDAN),
new Car("Ford", "Focus", 2012, Category.SEDAN)));
modelsExpected.put(Category.JEEP, Arrays.asList(new Car("Jeep", "Wrangler", 2011, Category.JEEP),
new Car("Jeep", "Comanche", 1990, Category.JEEP)));
2018-08-29 22:02:17 +05:30
Map<Category, List<Car>> modelsFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars);
Map<Category, List<Car>> modelsImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars);
LOGGER.info("Category " + modelsFunctional);
2018-09-04 20:52:11 +05:30
assertEquals(modelsExpected, modelsFunctional);
assertEquals(modelsExpected, modelsImperative);
2018-08-26 23:12:33 +05:30
}
@Test
public void testGetSedanCarsOwnedSortedByDate() {
Person john = new Person(cars);
2018-09-04 20:52:11 +05:30
List<Car> modelsExpected = Arrays.asList(new Car("Dodge", "Avenger", 2010, Category.SEDAN),
new Car("Ford", "Focus", 2012, Category.SEDAN));
2018-08-26 23:12:33 +05:30
List<Car> modelsFunctional = FunctionalProgramming.getSedanCarsOwnedSortedByDate(Arrays.asList(john));
List<Car> modelsImperative = ImperativeProgramming.getSedanCarsOwnedSortedByDate(Arrays.asList(john));
2018-09-04 20:52:11 +05:30
assertEquals(modelsExpected, modelsFunctional);
assertEquals(modelsExpected, modelsImperative);
2018-08-26 23:12:33 +05:30
}
2018-07-02 00:42:04 +05:30
}