Skip to content
This repository was archived by the owner on Mar 14, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
4 2 HW3 optional
  • Loading branch information
SLAVA committed Dec 20, 2016
commit 3a526cc6fe9e1263c4395826bed4cb020bfec430
18 changes: 0 additions & 18 deletions src/main/java/ru/javawebinar/topjava/util/MealsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import ru.javawebinar.topjava.to.MealWithExceed;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.util.*;
import java.util.stream.Collectors;

Expand All @@ -15,24 +13,8 @@
* 31.05.2015.
*/
public class MealsUtil {
public static final List<Meal> MEALS = Arrays.asList(
new Meal(LocalDateTime.of(2015, Month.MAY, 30, 10, 0), "Завтрак", 500),
new Meal(LocalDateTime.of(2015, Month.MAY, 30, 13, 0), "Обед", 1000),
new Meal(LocalDateTime.of(2015, Month.MAY, 30, 20, 0), "Ужин", 500),
new Meal(LocalDateTime.of(2015, Month.MAY, 31, 10, 0), "Завтрак", 1000),
new Meal(LocalDateTime.of(2015, Month.MAY, 31, 13, 0), "Обед", 500),
new Meal(LocalDateTime.of(2015, Month.MAY, 31, 20, 0), "Ужин", 510)
);

public static final int DEFAULT_CALORIES_PER_DAY = 2000;

public static void main(String[] args) {
List<MealWithExceed> filteredMealsWithExceeded = getFilteredWithExceeded(MEALS, LocalTime.of(7, 0), LocalTime.of(12, 0), DEFAULT_CALORIES_PER_DAY);
filteredMealsWithExceeded.forEach(System.out::println);

System.out.println(getFilteredWithExceededByCycle(MEALS, LocalTime.of(7, 0), LocalTime.of(12, 0), DEFAULT_CALORIES_PER_DAY));
}

public static List<MealWithExceed> getWithExceeded(Collection<Meal> meals, int caloriesPerDay) {
return getFilteredWithExceeded(meals, LocalTime.MIN, LocalTime.MAX, caloriesPerDay);
}
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/ru/javawebinar/topjava/MealTestData.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
import ru.javawebinar.topjava.matcher.ModelMatcher;
import ru.javawebinar.topjava.model.Meal;

import java.time.Month;
import java.util.Arrays;
import java.util.List;

import static java.time.LocalDateTime.of;
import static ru.javawebinar.topjava.model.BaseEntity.START_SEQ;

/**
* GKislin
* 13.03.2015.
Expand All @@ -11,4 +18,25 @@ public class MealTestData {

public static final ModelMatcher<Meal> MATCHER = new ModelMatcher<>();

public static final int MEAL1_ID = START_SEQ + 2;
public static final int ADMIN_MEAL_ID = START_SEQ + 8;

public static final Meal MEAL1 = new Meal(MEAL1_ID, of(2015, Month.MAY, 30, 10, 0), "Завтрак", 500);
public static final Meal MEAL2 = new Meal(MEAL1_ID + 1, of(2015, Month.MAY, 30, 13, 0), "Обед", 1000);
public static final Meal MEAL3 = new Meal(MEAL1_ID + 2, of(2015, Month.MAY, 30, 20, 0), "Ужин", 500);
public static final Meal MEAL4 = new Meal(MEAL1_ID + 3, of(2015, Month.MAY, 31, 10, 0), "Завтрак", 500);
public static final Meal MEAL5 = new Meal(MEAL1_ID + 4, of(2015, Month.MAY, 31, 13, 0), "Обед", 1000);
public static final Meal MEAL6 = new Meal(MEAL1_ID + 5, of(2015, Month.MAY, 31, 20, 0), "Ужин", 510);
public static final Meal ADMIN_MEAL1 = new Meal(ADMIN_MEAL_ID, of(2015, Month.JUNE, 1, 14, 0), "Админ ланч", 510);
public static final Meal ADMIN_MEAL2 = new Meal(ADMIN_MEAL_ID + 1, of(2015, Month.JUNE, 1, 21, 0), "Админ ужин", 1500);

public static final List<Meal> MEALS = Arrays.asList(MEAL6, MEAL5, MEAL4, MEAL3, MEAL2, MEAL1);

public static Meal getCreated() {
return new Meal(null, of(2015, Month.JUNE, 1, 18, 0), "Созданный ужин", 300);
}

public static Meal getUpdated() {
return new Meal(MEAL1_ID, MEAL1.getDateTime(), "Обновленный завтрак", 200);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import org.springframework.stereotype.Repository;
import ru.javawebinar.topjava.model.Meal;
import ru.javawebinar.topjava.repository.MealRepository;
import ru.javawebinar.topjava.util.MealsUtil;
import ru.javawebinar.topjava.util.TimeUtil;

import java.time.LocalDateTime;
import java.time.Month;
import java.util.Collection;
import java.util.Comparator;
import java.util.Map;
Expand All @@ -17,9 +15,6 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static ru.javawebinar.topjava.UserTestData.ADMIN_ID;
import static ru.javawebinar.topjava.UserTestData.USER_ID;

/**
* GKislin
* 15.09.2015.
Expand All @@ -33,13 +28,6 @@ public class InMemoryMealRepositoryImpl implements MealRepository {
private Map<Integer, Map<Integer, Meal>> repository = new ConcurrentHashMap<>();
private AtomicInteger counter = new AtomicInteger(0);

{
MealsUtil.MEALS.forEach(um -> save(um, USER_ID));

save(new Meal(LocalDateTime.of(2015, Month.JUNE, 1, 14, 0), "Админ ланч", 510), ADMIN_ID);
save(new Meal(LocalDateTime.of(2015, Month.JUNE, 1, 21, 0), "Админ ужин", 1500), ADMIN_ID);
}

@Override
public Meal save(Meal meal, int userId) {
Objects.requireNonNull(meal);
Expand Down
92 changes: 92 additions & 0 deletions src/test/java/ru/javawebinar/topjava/service/MealServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package ru.javawebinar.topjava.service;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import ru.javawebinar.topjava.MealTestData;
import ru.javawebinar.topjava.model.Meal;
import ru.javawebinar.topjava.util.DbPopulator;
import ru.javawebinar.topjava.util.exception.NotFoundException;

import java.time.LocalDate;
import java.time.Month;
import java.util.Arrays;

import static ru.javawebinar.topjava.MealTestData.*;
import static ru.javawebinar.topjava.UserTestData.ADMIN_ID;
import static ru.javawebinar.topjava.UserTestData.USER_ID;

@ContextConfiguration({
"classpath:spring/spring-app.xml",
"classpath:spring/spring-db.xml"
})
@RunWith(SpringJUnit4ClassRunner.class)
public class MealServiceTest {

@Autowired
protected MealService service;

@Autowired
private DbPopulator dbPopulator;

@Before
public void setUp() throws Exception {
dbPopulator.execute();
}

@Test
public void testDelete() throws Exception {
service.delete(MealTestData.MEAL1_ID, USER_ID);
MATCHER.assertCollectionEquals(Arrays.asList(MEAL6, MEAL5, MEAL4, MEAL3, MEAL2), service.getAll(USER_ID));
}

@Test(expected = NotFoundException.class)
public void testDeleteNotFound() throws Exception {
service.delete(MEAL1_ID, 1);
}

@Test
public void testSave() throws Exception {
Meal created = getCreated();
service.save(created, USER_ID);
MATCHER.assertCollectionEquals(Arrays.asList(created, MEAL6, MEAL5, MEAL4, MEAL3, MEAL2, MEAL1), service.getAll(USER_ID));
}

@Test
public void testGet() throws Exception {
Meal actual = service.get(ADMIN_MEAL_ID, ADMIN_ID);
MATCHER.assertEquals(ADMIN_MEAL1, actual);
}

@Test(expected = NotFoundException.class)
public void testGetNotFound() throws Exception {
service.get(MEAL1_ID, ADMIN_ID);
}

@Test
public void testUpdate() throws Exception {
Meal updated = getUpdated();
service.update(updated, USER_ID);
MATCHER.assertEquals(updated, service.get(MEAL1_ID, USER_ID));
}

@Test(expected = NotFoundException.class)
public void testNotFoundUpdate() throws Exception {
Meal item = service.get(MEAL1_ID, USER_ID);
service.update(item, ADMIN_ID);
}

@Test
public void testGetAll() throws Exception {
MATCHER.assertCollectionEquals(MEALS, service.getAll(USER_ID));
}

@Test
public void testGetBetween() throws Exception {
MATCHER.assertCollectionEquals(Arrays.asList(MEAL3, MEAL2, MEAL1),
service.getBetweenDates(LocalDate.of(2015, Month.MAY, 30), LocalDate.of(2015, Month.MAY, 30), USER_ID));
}
}