Skip to content
This repository has been archived by the owner on Oct 18, 2018. It is now read-only.

API and DB+API tests #70

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
59 changes: 59 additions & 0 deletions api-tests-module/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>school-2016</artifactId>
<groupId>ru.qatools.school</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>api-tests-module</artifactId>
<name>API Tests</name>

<dependencies>
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-gson</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>ru.yandex.qatools.allure</groupId>
<artifactId>allure-junit-adaptor</artifactId>
<version>1.4.23</version>
</dependency>
<dependency>
<groupId>ru.qatools.school</groupId>
<artifactId>dbclient-module</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>ru.yandex.qatools.matchers</groupId>
<artifactId>collection-matchers</artifactId>
<version>1.3</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ru.qatools.school.Responses;

/**
* @author ava1on
*/
public class CitiesResponse {

public CitiesResponse() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.qatools.school.Responses;

/**
* @author ava1on
*/
public class SuggestResponse {
private int id;

public SuggestResponse(int id){
this.id = id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ru.qatools.school.Responses;

/**
* @author ava1on
*/
public class TemperatureValues {
private double value;

public TemperatureValues(double value) {
this.value = value;
}

public double getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.qatools.school.Responses;

/**
* @author ava1on
*/
public class WeatherResponse {
private TemperatureValues[] temperatures;

public WeatherResponse(TemperatureValues[] temperatures) {
this.temperatures = temperatures;
}

public TemperatureValues[] getTemperatures() {
return temperatures;
}

public Double recalculateCelsiusToFahrenheit(){
return (this.getTemperatures()[0].getValue()*9/5+32);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ru.qatools.school.apitests;

import org.apache.http.HttpStatus;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import ru.qatools.school.DbClient;
import ru.yandex.qatools.allure.annotations.Title;

import java.util.List;

import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static ru.qatools.school.data.Constants.*;

/**
* @author ava1on
*/
public class DbAPITest {
private DbClient dbClient;

@Before
public void setupDbConnection(){
dbClient = new DbClient();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

@After
public void closeDbConnection(){
dbClient.close();
}

@Test
@Title("Списки городов, полученные через API и Database, должны совпадать")
public void apiResponseShouldMatchDatabaseData(){
List<Integer> ids =
given()
.baseUri(BASE_URL)
.param(QUERY_PARAMETER, CITYNAME_PART)
.when()
.get(SUGGEST_REQUEST)
.then()
.assertThat().statusCode(HttpStatus.SC_OK)
.and().extract().body().path("id");
assertThat("Полученные через API данные не соответствуют Database", ids,
is(dbClient.getCitiesByNamePart(CITYNAME_PART)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ru.qatools.school.apitests;

import org.apache.http.HttpStatus;
import org.junit.Test;
import ru.qatools.school.Responses.CitiesResponse;
import ru.qatools.school.Responses.WeatherResponse;
import ru.yandex.qatools.allure.annotations.Title;

import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.is;
import static ru.qatools.school.data.Constants.*;

/**
* @author ava1on
*/
public class RestAssuredAPITests {

@Test
@Title("Должны получить заданное в параметре количество городов в ответе")
public void shouldReturnRequestedNumberOfCities(){
CitiesResponse[] citiesArray =
given().
baseUri(BASE_URL).
param(LIMIT_PARAMETER, LIMIT_VALUE).
when().
get(CITIES_REQUEST).
then().statusCode(HttpStatus.SC_OK).
and().extract().
body().as(CitiesResponse[].class);
assertThat("Неверное количество городов в ответе", citiesArray.length, is(LIMIT_VALUE));
}

@Test
@Title("Значение температуры по шкале Фаренгейта должно соответствовать значению по шкале Цельсия")
public void shouldReturnCompatibleTemperatureValuesForCelsiusAndFahrenheit(){
WeatherResponse weatherResponse =
given().
baseUri(BASE_URL).
param(CITY_PARAMETER, CITY_VALUE).
when().
get(WEATHER_REQUEST).
then().statusCode(HttpStatus.SC_OK).
and().extract().
body().as(WeatherResponse.class);
assertThat("Значение температуры не правильно переведено из шкалы Цельсия в шкалу Фаренгейта",
weatherResponse.getTemperatures()[2].getValue(),
is(closeTo(weatherResponse.recalculateCelsiusToFahrenheit(), TEMPERATURE_ERROR)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package ru.qatools.school.apitests;

import org.apache.http.HttpStatus;
import org.junit.BeforeClass;
import org.junit.Test;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import ru.qatools.school.Responses.CitiesResponse;
import ru.qatools.school.Responses.WeatherResponse;
import ru.qatools.school.interfaces.Cities;
import ru.qatools.school.interfaces.Weather;
import ru.yandex.qatools.allure.annotations.Title;

import java.io.IOException;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static ru.qatools.school.data.Constants.*;

/**
* @author ava1on
*/
public class RetrofitAPITests {

private static Retrofit retrofit;

@BeforeClass
public static void initRetrofit(){
retrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
}

@Test
@Title("Должны получить заданное в параметре количество городов в ответе")
public void shouldReturnRequestedNumberOfCities() throws IOException{
Cities cities = retrofit.create(Cities.class);
Response<List<CitiesResponse>> response = cities.citiesList(LIMIT_VALUE).execute();
assertThat("Код ответа неверный", response.code(), is(HttpStatus.SC_OK));
assertThat("Неверное количество городов в ответе", response.body(), hasSize(LIMIT_VALUE));
}

@Test
@Title("Значение температуры по шкале Фаренгейта должно соответствовать значению по шкале Цельсия")
public void shouldReturnCompatibleTemperatureValuesForCelsiusAndFahrenheit() throws IOException{
Weather weather = retrofit.create(Weather.class);
Response<WeatherResponse> response = weather.weather(CITY_PARAMETER, null).execute();
assertThat("Код ответа неверный", response.code(), is(HttpStatus.SC_OK));
assertThat("Значение температуры не правильно переведено из шкалы Цельсия в шкалу Фаренгейта",
response.body().getTemperatures()[2].getValue(),
is(closeTo(response.body().recalculateCelsiusToFahrenheit(), TEMPERATURE_ERROR)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ru.qatools.school.data;

/**
* @author ava1on
*/
public class Constants {
public static final String BASE_URL = "http://weather.lanwen.ru/api/";
public static final String LIMIT_PARAMETER = "limit";
public static final String CITIES_REQUEST = "cities";
public static final int LIMIT_VALUE = 3;
public static final String WEATHER_REQUEST = "weather";
public static final String CITY_PARAMETER = "city";
public static final String CITY_VALUE = "Saint Petersburg";
public static final double TEMPERATURE_ERROR = 0.1;
public static final String REGION_PARAMETER = "region";
public static final String SUGGEST_REQUEST = "suggest";
public static final String QUERY_PARAMETER = "query";
public static final String CITYNAME_PART = "Saint";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.qatools.school.interfaces;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
import ru.qatools.school.Responses.CitiesResponse;

import java.util.List;

import static ru.qatools.school.data.Constants.LIMIT_PARAMETER;

/**
* @author ava1on
*/
public interface Cities {
@GET("cities")
Call<List<CitiesResponse>> citiesList(@Query(LIMIT_PARAMETER) int limitValue);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ru.qatools.school.interfaces;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
import ru.qatools.school.Responses.WeatherResponse;

import static ru.qatools.school.data.Constants.CITY_PARAMETER;
import static ru.qatools.school.data.Constants.REGION_PARAMETER;

/**
* @author ava1on
*/
public interface Weather {
@GET("weather")
Call<WeatherResponse> weather(@Query(CITY_PARAMETER) String city, @Query(REGION_PARAMETER) String region);
}
Loading