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 3.1 #16

Open
wants to merge 13 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
2 changes: 1 addition & 1 deletion localenv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

docker run --name niffler-all -p 5432:5432 -e POSTGRES_PASSWORD=secret -e CREATE_DATABASES=niffler-auth,niffler-currency,niffler-spend,niffler-userdata -v pgdata:/var/lib/postgresql/data -v ./postgres/init-database.sh:/docker-entrypoint-initdb.d/init-database.sh -d postgres:15.1 --max_prepared_transactions=100
docker run --name niffler-all -p 5432:5432 -e POSTGRES_PASSWORD=secret -e CREATE_DATABASES=niffler-auth,niffler-currency,niffler-spend,niffler-userdata -v pgdata:/var/lib/postgresql/data -v /Users/oleg/Developer/niffler-ng-6/postgres/init-database.sh:/docker-entrypoint-initdb.d/init-database.sh -d postgres:15.1 --max_prepared_transactions=100
docker run --name=zookeeper -e ZOOKEEPER_CLIENT_PORT=2181 -p 2181:2181 -d confluentinc/cp-zookeeper:7.3.2
docker run --name=kafka -e KAFKA_BROKER_ID=1 \
-e KAFKA_ZOOKEEPER_CONNECT=$(docker inspect zookeeper --format='{{ .NetworkSettings.IPAddress }}'):2181 \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
import retrofit2.http.Path;

public interface GhApi {

@GET("repos/qa-guru/niffler/issues/{issue_number}")
@GET("repos/tankisleva/java_pft/issues/{issue_number}")
@Headers({
"Accept: application/vnd.github+json",
"X-GitHub-Api-Version: 2022-11-28"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,50 @@
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.POST;
import retrofit2.http.*;

import java.util.Date;
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,
@Query("username") String username);

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

@DELETE("internal/spends/remove")
Call<Void> deleteSpends(
@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>> getCategories(
@Query("username") String username,
@Query("excludeArchived") boolean excludeArchived
);

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
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 lombok.SneakyThrows;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

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

import static org.junit.jupiter.api.Assertions.assertEquals;

public class SpendApiClient {

private final Retrofit retrofit = new Retrofit.Builder()
Expand All @@ -15,10 +24,98 @@ public class SpendApiClient {

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

@SneakyThrows
public SpendJson createSpend(SpendJson spend) {
return spendApi.addSpend(spend)
.execute()
.body();
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, String username) {
final Response<SpendJson> response;
try {
response = spendApi.getSpend(id, username)
.execute();
} catch (IOException e) {
throw new AssertionError(e);
}
assertEquals(200, response.code());
return response.body();
}

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

public void deleteSpends(String username, List<String> ids) {
Response<Void> response;
try {
response = spendApi.deleteSpends(username, ids)
.execute();
} catch (IOException e) {
throw new AssertionError(e);
}
assertEquals(202, response.code());
}

public CategoryJson addCategory(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> getCategories(String username, boolean excludeArchived) {
final Response<List<CategoryJson>> response;
try {
response = spendApi.getCategories(username, excludeArchived)
.execute();
} catch (IOException e) {
throw new AssertionError(e);
}
assertEquals(200, response.code());
return response.body();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package guru.qa.niffler.jupiter.annotation;

import guru.qa.niffler.jupiter.extension.AddCategoryExtension;
import org.junit.jupiter.api.extension.ExtendWith;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@ExtendWith(AddCategoryExtension.class)
public @interface Category {
String username();
boolean isArchive();
String name() default "";
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package guru.qa.niffler.jupiter.annotation;

import guru.qa.niffler.jupiter.extension.SpendingExtension;
import guru.qa.niffler.jupiter.extension.CreateSpendingExtension;
import guru.qa.niffler.jupiter.extension.SpendingResolverExtension;
import org.junit.jupiter.api.extension.ExtendWith;

import java.lang.annotation.ElementType;
Expand All @@ -10,7 +11,7 @@

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@ExtendWith(SpendingExtension.class)
@ExtendWith({CreateSpendingExtension.class, SpendingResolverExtension.class})
public @interface Spending {
String username();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package guru.qa.niffler.jupiter.extension;

import com.github.javafaker.Faker;
import guru.qa.niffler.api.SpendApiClient;
import guru.qa.niffler.jupiter.annotation.Category;
import guru.qa.niffler.model.CategoryJson;
import org.junit.jupiter.api.extension.*;
import org.junit.platform.commons.support.AnnotationSupport;

public class AddCategoryExtension implements
BeforeEachCallback,
AfterTestExecutionCallback,
ParameterResolver {

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

@Override
public void beforeEach(ExtensionContext context) {
AnnotationSupport.findAnnotation(context.getRequiredTestMethod(), Category.class)
.ifPresent(anno ->{
String name = faker.backToTheFuture().quote();
CategoryJson categoryJson = new CategoryJson(
null,
anno.name().isEmpty() ? name : anno.name(),
anno.username(),
false
);

CategoryJson createCategory = spendApiClient.addCategory(categoryJson);

if (anno.isArchive()) {
CategoryJson archivedCategory = new CategoryJson(
createCategory.id(),
createCategory.name(),
createCategory.username(),
true
);
createCategory = spendApiClient.updateCategory(archivedCategory);
}

context.getStore(NAMESPACE).put(context.getUniqueId(), createCategory);
});
}

@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
return parameterContext.getParameter().getType().isAssignableFrom(CategoryJson.class);
}

@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
return extensionContext.getStore(AddCategoryExtension.NAMESPACE).get(extensionContext.getUniqueId(), CategoryJson.class);
}

@Override
public void afterTestExecution(ExtensionContext context) {
CategoryJson categoryJson = context.getStore(NAMESPACE).get(context.getUniqueId(), CategoryJson.class);

if (categoryJson.archived()) {
CategoryJson archivedCategory = new CategoryJson(
categoryJson.id(),
categoryJson.name(),
categoryJson.username(),
true
);
spendApiClient.updateCategory(archivedCategory);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import com.codeborne.selenide.logevents.SelenideLogger;
import io.qameta.allure.Allure;
import io.qameta.allure.selenide.AllureSelenide;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.LifecycleMethodExecutionExceptionHandler;
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
import org.junit.jupiter.api.extension.*;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;

import java.io.ByteArrayInputStream;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

public class BrowserExtension implements
BeforeEachCallback,
Expand Down Expand Up @@ -64,4 +64,5 @@ private static void doScreenshot() {
);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package guru.qa.niffler.jupiter.extension;

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

import java.util.Date;

public class CreateSpendingExtension implements BeforeEachCallback {

public static final ExtensionContext.Namespace NAMESPACE = ExtensionContext.Namespace.create(CreateSpendingExtension.class);

private final SpendApiClient spendApiClient = new SpendApiClient();

@Override
public void beforeEach(ExtensionContext context) throws Exception {
AnnotationSupport.findAnnotation(context.getRequiredTestMethod(), Spending.class)
.ifPresent(anno -> {
SpendJson spend = new SpendJson(
null,
new Date(),
new CategoryJson(
null,
anno.category(),
anno.username(),
false
),
CurrencyValues.RUB,
anno.amount(),
anno.description(),
anno.username()
);
context.getStore(NAMESPACE).put(
context.getUniqueId(),
spendApiClient.createSpend(spend)
);
});
}
}
Loading