Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hw test #47

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ public static void main(String[] args) {
springApplication.addListeners(new PropertiesLogger());
springApplication.run(args);
}

}
19 changes: 0 additions & 19 deletions niffler-e-2-e-tests/src/test/java/guru/qa/niffler/api/GhApi.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,45 +1,12 @@
package guru.qa.niffler.api;

import guru.qa.niffler.model.CategoryJson;
import guru.qa.niffler.model.CurrencyValues;
import guru.qa.niffler.model.SpendJson;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.DELETE;
import retrofit2.http.GET;
import retrofit2.http.PATCH;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Query;

import java.util.List;

public interface SpendApi {

@POST("internal/spends/add")
Call<SpendJson> addSpend(@Body SpendJson spend);

@PATCH("internal/spends/edit")
Call<SpendJson> editSpend(@Body SpendJson spend);

@GET("internal/spends/{id}")
Call<SpendJson> getSpend(@Path("id") String id);

@GET("internal/spends/all")
Call<List<SpendJson>> allSpends(@Query("username") String username,
@Query("filterCurrency") CurrencyValues filterCurrency,
@Query("from") String from,
@Query("to") String to);

@DELETE("internal/spends/remove")
Call<Void> removeSpends(@Query("username") String username, @Query("ids") List<String> ids);

@POST("internal/categories/add")
Call<CategoryJson> addCategory(@Body CategoryJson category);

@PATCH("internal/categories/update")
Call<CategoryJson> updateCategory(@Body CategoryJson category);

@GET("internal/categories/all")
Call<List<CategoryJson>> allCategories(@Query("username") String username);
}
@POST("/internal/spends/add")
Call<SpendJson> createSpend(@Body SpendJson spend);
}
Original file line number Diff line number Diff line change
@@ -1,124 +1,37 @@
package guru.qa.niffler.api;

import guru.qa.niffler.config.Config;
import guru.qa.niffler.model.CategoryJson;
import guru.qa.niffler.model.CurrencyValues;
import guru.qa.niffler.model.SpendJson;
import retrofit2.Response;
import lombok.SneakyThrows;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import okhttp3.logging.HttpLoggingInterceptor.Level;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class SpendApiClient {
private static final Config CFG = Config.getInstance();

import static org.junit.jupiter.api.Assertions.assertEquals;
private final OkHttpClient okHttpClient = getOkHttpClient();

public class SpendApiClient {
public OkHttpClient getOkHttpClient() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(Level.BODY);
return new OkHttpClient.Builder().addInterceptor(logging).build();
}

private final Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Config.getInstance().spendUrl())
.client(okHttpClient)
.baseUrl(CFG.spendUrl())
.addConverterFactory(JacksonConverterFactory.create())
.build();

private final SpendApi spendApi = retrofit.create(SpendApi.class);

@SneakyThrows
public SpendJson createSpend(SpendJson spend) {
final Response<SpendJson> response;
try {
response = spendApi.addSpend(spend)
.execute();
} catch (IOException e) {
throw new AssertionError(e);
}
assertEquals(201, response.code());
return response.body();
}

public SpendJson editSpend(SpendJson spend) {
final Response<SpendJson> response;
try {
response = spendApi.editSpend(spend)
.execute();
} catch (IOException e) {
throw new AssertionError(e);
}
assertEquals(200, response.code());
return response.body();
}

public SpendJson getSpend(String id) {
final Response<SpendJson> response;
try {
response = spendApi.getSpend(id)
.execute();
} catch (IOException e) {
throw new AssertionError(e);
}
assertEquals(200, response.code());
return response.body();
}

public List<SpendJson> allSpends(String username,
CurrencyValues currency,
String from,
String to) {
final Response<List<SpendJson>> response;
try {
response = spendApi.allSpends(username, currency, from, to)
.execute();
} catch (IOException e) {
throw new AssertionError(e);
}
assertEquals(200, response.code());
return response.body();
}

public void removeSpends(@Nonnull String username, @Nonnull String... ids) {
final Response<Void> response;
try {
response = spendApi.removeSpends(username, Arrays.stream(ids).toList())
.execute();
} catch (IOException e) {
throw new AssertionError(e);
}
assertEquals(200, response.code());
}

public CategoryJson createCategory(CategoryJson category) {
final Response<CategoryJson> response;
try {
response = spendApi.addCategory(category)
.execute();
} catch (IOException e) {
throw new AssertionError(e);
}
assertEquals(200, response.code());
return response.body();
}

public CategoryJson updateCategory(CategoryJson category) {
final Response<CategoryJson> response;
try {
response = spendApi.updateCategory(category)
.execute();
} catch (IOException e) {
throw new AssertionError(e);
}
assertEquals(200, response.code());
return response.body();
}

public List<CategoryJson> allCategory(String username) {
final Response<List<CategoryJson>> response;
try {
response = spendApi.allCategories(username)
.execute();
} catch (IOException e) {
throw new AssertionError(e);
}
assertEquals(200, response.code());
return response.body();
return spendApi.createSpend(spend)
.execute()
.body();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
public interface Config {

static Config getInstance() {
return LocalConfig.INSTANCE;
return LocalConfig.instance;
}

String frontUrl();

String spendUrl();

String ghUrl();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package guru.qa.niffler.config;

enum LocalConfig implements Config {
INSTANCE;
public enum LocalConfig implements Config {
instance;

@Override
public String frontUrl() {
Expand All @@ -12,9 +12,4 @@ public String frontUrl() {
public String spendUrl() {
return "http://127.0.0.1:8093/";
}

@Override
public String ghUrl() {
return "https://api.github.com/";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package guru.qa.niffler.model;
package guru.qa.niffler.enums;

public enum CurrencyValues {
RUB, USD, EUR, KZT
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package guru.qa.niffler.jupiter.extension;
package guru.qa.niffler.jupiter;

import com.codeborne.selenide.Selenide;
import com.codeborne.selenide.WebDriverRunner;
Expand All @@ -20,41 +20,37 @@ public class BrowserExtension implements
AfterEachCallback,
TestExecutionExceptionHandler,
LifecycleMethodExecutionExceptionHandler {

@Override
public void afterEach(ExtensionContext context) throws Exception {
public void afterEach(ExtensionContext context) {
if (WebDriverRunner.hasWebDriverStarted()) {
Selenide.closeWebDriver();
}
}

@Override
public void beforeEach(ExtensionContext context) throws Exception {
SelenideLogger.addListener("Allure-selenide", new AllureSelenide()
.savePageSource(false)
.screenshots(false)
);
public void beforeEach(ExtensionContext context) {
SelenideLogger.addListener("Allure-selenide", new AllureSelenide().savePageSource(false).screenshots(false));
}

@Override
public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
doScreenshot();
doScreenShot();
throw throwable;
}

@Override
public void handleBeforeEachMethodExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
doScreenshot();
doScreenShot();
throw throwable;
}

@Override
public void handleAfterEachMethodExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
doScreenshot();
doScreenShot();
throw throwable;
}

private static void doScreenshot() {
private static void doScreenShot() {
if (WebDriverRunner.hasWebDriverStarted()) {
Allure.addAttachment(
"Screen on fail",
Expand All @@ -64,4 +60,4 @@ private static void doScreenshot() {
);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package guru.qa.niffler.jupiter;

import guru.qa.niffler.api.SpendApiClient;
import guru.qa.niffler.model.SpendJson;
import guru.qa.niffler.model.submodel.CategoryJson;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import org.junit.platform.commons.support.AnnotationSupport;

import java.util.Date;

public class CreateSpendingExtension implements BeforeEachCallback {

public static final Namespace NAMESPACE = ExtensionContext.Namespace.create(CreateSpendingExtension.class);
private final SpendApiClient spendApiClient = new SpendApiClient();

@Override
public void beforeEach(ExtensionContext context) {
AnnotationSupport.findAnnotation(context.getRequiredTestMethod(), Spend.class)
.ifPresent(
anno -> {
SpendJson spendJson = new SpendJson(
null,
new Date(),
new CategoryJson(null, anno.category(), anno.username(), false),
anno.currency(),
anno.amount(),
anno.description(),
anno.username()
);
final SpendJson createdSpend = spendApiClient.createSpend(spendJson);
context.getStore(NAMESPACE)
.put(context.getUniqueId(), createdSpend);
}
);
}
}
Loading