package org.itevents.service;

import org.itevents.model.Location;
import org.junit.Assert;
import org.itevents.model.Event;
import org.itevents.service.RatingServiceImpl;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class RatingServiceTest {

    private static final int EXPECTED_LIST_SIZE = 5;
    private static final int TRIM_SIZE = 5;
    private static final int DESCENDING_ORDER = -1;

    private static Map<Event, Integer> mapForTest;
    private static RatingServiceImpl ratingService;
    private static SimpleDateFormat formatter;

    @BeforeClass
    public static void setup() throws ParseException {
        mapForTest = new HashMap<>();
        formatter = new SimpleDateFormat("dd.MM.yyyy");
        ratingService = new RatingServiceImpl();
        mapForTest.put(new Event(8, "Java", formatter.parse("10.08.2015"), null, "http://www.java.com.ua",
                "Beresteyska", new Location(50.458585, 30.742017), "java@gmail.com"), 1);
        mapForTest.put(new Event(9, "Java", formatter.parse("10.08.2015"), null, "http://www.java.com.ua",
                "Beresteyska", new Location(50.458585, 30.742017), "java@gmail.com"), 4);
        mapForTest.put(new Event(10, "Java", formatter.parse("10.08.2015"), null, "http://www.java.com.ua",
                "Beresteyska", new Location(50.458585, 30.742017), "java@gmail.com"), 3);
        mapForTest.put(new Event(11, "Java", formatter.parse("10.08.2015"), null, "http://www.java.com.ua",
                "Beresteyska", new Location(50.458585, 30.742017), "java@gmail.com"), 5);
        mapForTest.put(new Event(12, "Java", formatter.parse("10.08.2015"), null, "http://www.java.com.ua",
                "Beresteyska", new Location(50.458585, 30.742017), "java@gmail.com"), 8);
        mapForTest.put(new Event(13, "Java", formatter.parse("10.08.2015"), null, "http://www.java.com.ua",
                "Beresteyska", new Location(50.458585, 30.742017), "java@gmail.com"), 6);
    }

    @AfterClass
    public static void tearDown(){
        ratingService = null;
    }

    @Test
    public void testTrimToSizeMap(){
        List<Event> actualList = ratingService.getListEventByRating(TRIM_SIZE, mapForTest);
        int actualSize = actualList.size();
        int expectedListSize = EXPECTED_LIST_SIZE;
        Assert.assertEquals(expectedListSize, actualSize);
    }

    @Test
    public void testSortMapEvents() throws ParseException {
        Map<Event, Integer> resultedMap = ratingService.sortEventsMap(DESCENDING_ORDER, mapForTest);
        List<Integer> listResultedValue = new ArrayList<>(resultedMap.values());
        Integer[] array = {8, 6, 5, 4, 3, 1};
        List<Integer> listExpectedValue = new ArrayList<>(Arrays.asList(array));
        Assert.assertEquals(listExpectedValue, listResultedValue);
    }
}

@@ -133,42 +133,24 @@
 org.
-itevents.service.RatingServiceImpl
+junit.AfterClass
 ;%0Aim
@@ -156,37 +156,38 @@
 mport org.junit.
-After
+Before
 Class;%0Aimport or
@@ -198,45 +198,210 @@
 nit.
-BeforeClass;%0Aimport org.junit.Tes
+Test;%0Aimport org.junit.runner.RunWith;%0Aimport org.springframework.test.context.ContextConfiguration;%0Aimport org.springframework.test.context.junit4.SpringJUnit4ClassRunner;%0A%0Aimport javax.inject.Injec
 t;%0A
-%0A
 impo
@@ -485,16 +485,119 @@
 til.*;%0A%0A
+@RunWith(SpringJUnit4ClassRunner.class)%0A@ContextConfiguration(locations = %7B%22/applicationContext.xml%22%7D)%0A
 public c
@@ -772,32 +772,24 @@
  = -1;%0A%0A    
-private 
 static Map%3CE
@@ -838,87 +838,89 @@
 tic 
-RatingServiceImpl ratingService;%0A    private static SimpleDateFormat formatter;
+SimpleDateFormat formatter;%0A%0A    private static RatingServiceImpl ratingService;%0A
 %0A%0A  
@@ -1023,24 +1023,73 @@
 ashMap%3C%3E();%0A
+        ratingService = new RatingServiceImpl();%0A
         form
@@ -1144,46 +1144,48 @@
     
-ratingService = new RatingServiceImpl(
+Date date = formatter.parse(%2210.08.2015%22
 );%0A 
@@ -1231,37 +1231,12 @@
 a%22, 
-formatter.parse(%2210.08.2015%22)
+date
 , nu
@@ -1403,37 +1403,12 @@
 a%22, 
-formatter.parse(%2210.08.2015%22)
+date
 , nu
@@ -1576,37 +1576,12 @@
 a%22, 
-formatter.parse(%2210.08.2015%22)
+date
 , nu
@@ -1749,37 +1749,12 @@
 a%22, 
-formatter.parse(%2210.08.2015%22)
+date
 , nu

refactor BeforeClass for tests