/*
 * Licensed to Laurent Broudoux (the "Author") under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Author licenses this
 * file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.github.lbroudoux.microcks.repository;

import com.github.lbroudoux.microcks.domain.Service;
import com.github.lbroudoux.microcks.domain.TestResult;
import com.github.lbroudoux.microcks.domain.TestRunnerType;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

import static org.junit.Assert.*;
/**
 * Test case for TestResultRepository implementation.
 * @author laurent
 */
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@ContextConfiguration(classes = RepositoryTestsConfiguration.class)
public class TestResultRepositoryTest {

  @Autowired
  ServiceRepository serviceRepository;

  @Autowired
  TestResultRepository repository;

  String serviceId;

  @Before
  public void setUp(){
    // Create a service...
    Service service = new Service();
    service.setName("HelloWorld");
    service.setVersion("1.0");
    serviceRepository.save(service);
    // Create a bunch of test results for this service.
    TestResult testResult = new TestResult();
    testResult.setInProgress(false);
    testResult.setRunnerType(TestRunnerType.HTTP);
    testResult.setTestNumber(1L);
    testResult.setServiceId(service.getId());
    testResult.setTestedEndpoint("http://localhost:8088/HelloWorld");
    repository.save(testResult);
    // ... another one.
    testResult = new TestResult();
    testResult.setInProgress(false);
    testResult.setRunnerType(TestRunnerType.HTTP);
    testResult.setTestNumber(2L);
    testResult.setServiceId(service.getId());
    testResult.setTestedEndpoint("http://localhost:8088/HelloWorld");
    repository.save(testResult);
    serviceId = service.getId();
  }

  @Test
  public void testFindByService() {
    long count = repository.countByServiceId(serviceId);
    assertEquals(2, count);
    List<TestResult> results = repository.findByServiceId(serviceId);
    assertEquals(2, results.size());
  }

  @Test
  public void testFindLastOnesForService() {
    List<TestResult> older = repository.findByServiceId(serviceId, new PageRequest(0, 2, Sort.Direction.DESC, "testNumber"));
    assertEquals(2, older.size());
    Long newTestNumber = older.get(0).getTestNumber() + 1L;
    assertEquals(3L, newTestNumber.longValue());
  }
}

Add test for TestResultRepository