Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "allure-ee-java"]
path = allure-ee-java
url = git@github.com:qameta/allure-ee-java.git
1 change: 1 addition & 0 deletions allure-ee-java
Submodule allure-ee-java added at 776ece
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ dependencies {

implementation("com.squareup.retrofit2:converter-jackson:2.9.0")
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.squareup.okhttp3:logging-interceptor:4.12.0")

implementation("joda-time:joda-time:2.12.5")

implementation("com.google.code.findbugs:jsr305:3.0.2")

implementation("io.qameta.allure:allure-model:2.24.0")
implementation("io.qameta.allure:allure-ee-client:3.53.0")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.github.eroshenkoam.allure.client;

import io.github.eroshenkoam.allure.client.dto.Account;
import io.github.eroshenkoam.allure.client.dto.Authority;
import io.github.eroshenkoam.allure.client.dto.Page;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.*;

import java.util.List;

@SuppressWarnings("PMD.LinguisticNaming")
public interface AccountService {

/**
* Returns the authorized user.
*/
@GET("api/uaa/account/me")
Call<Account> getAccount();

@GET("api/uaa/account")
Call<Page<Account>> getAccounts();

@GET("api/uaa/account")
Call<Page<Account>> getAccounts(
@Query("query") String query,
@Query("page") int page,
@Query("size") int size
);

@GET("api/uaa/account/{username}/find")
Call<Account> findByUsername(@Path("username") String username);

@POST("api/uaa/account/register")
Call<Account> register(@Body Account account);

@POST("api/uaa/account/{id}/authority")
Call<ResponseBody> setAuthority(@Path("id") Long id, @Body List<Authority> authorities);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.eroshenkoam.allure.client;

import io.github.eroshenkoam.allure.client.dto.AccountAuthority;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;
import retrofit2.http.Path;

public interface AdminAccountService {

@POST("api/admin/account/{id}/role")
Call<ResponseBody> setRole(final @Path("id") Long id, @Body AccountAuthority authority);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.github.eroshenkoam.allure.client;

import io.github.eroshenkoam.allure.client.dto.Group;
import io.github.eroshenkoam.allure.client.dto.GroupUser;
import io.github.eroshenkoam.allure.client.dto.GroupUserAdd;
import io.github.eroshenkoam.allure.client.dto.Page;
import retrofit2.Call;
import retrofit2.http.*;

public interface GroupService {

@GET("/api/rs/accessgroup")
Call<Page<Group>> find(
@Query("query") String query,
@Query("page") int page,
@Query("size") int size
);

@POST("/api/rs/accessgroup")
Call<Group> create(@Body Group dto);

@GET("/api/rs/accessgroup/{id}/user")
Call<Page<GroupUser>> getUsers(@Path("id") Long groupId);

@POST("/api/rs/accessgroup/{id}/user")
Call<Void> addUsers(@Path("id") Long groupId, @Body GroupUserAdd dto);

@DELETE("/api/rs/accessgroup/{id}/user")
Call<Void> removeUser(@Path("id") Long groupId, @Query("username") String username);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.github.eroshenkoam.allure.client;

import io.github.eroshenkoam.allure.client.dto.CreateLaunchEvent;
import io.github.eroshenkoam.allure.client.dto.Launch;
import io.github.eroshenkoam.allure.client.dto.Page;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.DELETE;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Query;

public interface LaunchService {

/**
* Create a new launch. Created launch will linked with test pack
* found by testPackId or testPackExternalId provided in event body. If there is no
* such test pack, a new one will be created.
*/
@POST("api/rs/launch/new")
Call<Launch> create(
@Body CreateLaunchEvent event
);

/**
* Finds launch by id.
*/
@GET("api/rs/launch/{id}")
Call<Launch> findById(
@Path("id") Long id
);

/**
* Finds launches by rql.
*/
@GET("api/rs/launch/__search")
Call<Page<Launch>> findAll(@Query("projectId") Long projectId,
@Query("rql") String rql,
@Query("page") int page,
@Query("size") int size);

/**
* Delete by launch id.
*/
@DELETE("api/rs/launch/{id}")
Call<Launch> delete(
@Path("id") Long id
);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.github.eroshenkoam.allure.client;

import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import io.github.eroshenkoam.allure.client.dto.Page;
import io.github.eroshenkoam.allure.client.dto.Project;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;

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

public interface ProjectService {

@GET("api/rs/project")
Call<Page<Project>> getProjects(@Query("query") String query,
@Query("page") int page,
@Query("size") int size);

@GET("api/rs/project/{id}")
Call<Project> getProject(@Path("id") Long projectId);

@GET("api/rs/project")
Call<List<Project>> getProjectList();


default List<Project> getProjectsWrapper() throws IOException {
try {
return getProjectList().execute().body();
} catch (MismatchedInputException e) {
return getProjects("", 0, 1000).execute().body().getContent();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.github.eroshenkoam.allure.client;

/**
* @author Vitaly Bragin.
*/
public class ServerClientException extends RuntimeException {

public ServerClientException(final String message) {
super(message);
}

public ServerClientException(final String operationDescription, final String message, final Throwable e) {
super(getFormattedMessage(operationDescription, message), e);
}

public ServerClientException(final String operationDescription, final Throwable e) {
super(String.format("Could not %s", operationDescription), e);
}

public ServerClientException(final String operationDescription, final String message) {
super(getFormattedMessage(operationDescription, message));
}

protected static String getFormattedMessage(final String operationDescription, final String message) {
return String.format("Could not %s: %s", operationDescription, message);
}

}
140 changes: 140 additions & 0 deletions src/main/java/io/github/eroshenkoam/allure/client/ServiceBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package io.github.eroshenkoam.allure.client;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.eroshenkoam.allure.client.retrofit.LoginPasswordAuthenticator;
import io.github.eroshenkoam.allure.client.retrofit.NonValidatingTrustManager;
import io.github.eroshenkoam.allure.client.retrofit.ResponseCallAdapterFactory;
import io.github.eroshenkoam.allure.client.retrofit.RetryInterceptor;
import io.github.eroshenkoam.allure.client.retrofit.TokenAuthenticator;
import okhttp3.Dispatcher;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeUnit;

import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;

/**
* @author eroshenkoam (Artem Eroshenko).
*/
@SuppressWarnings({
"PMD.AvoidFieldNameMatchingTypeName",
"ClassDataAbstractionCoupling"
})
public class ServiceBuilder {

private final OkHttpClient.Builder clientBuilder;
private final Retrofit.Builder retrofitBuilder;

public ServiceBuilder(final String baseUrl) {
this.clientBuilder = new OkHttpClient.Builder()
.connectTimeout(1, TimeUnit.MINUTES)
.writeTimeout(5, TimeUnit.MINUTES)
.readTimeout(10, TimeUnit.MINUTES);
this.retrofitBuilder = new Retrofit.Builder()
.baseUrl(baseUrl);
}

public ServiceBuilder authToken(final String token) {
this.clientBuilder.addInterceptor(new TokenAuthenticator(token));
return this;
}

public ServiceBuilder authBasic(final String username, final String password) {
this.clientBuilder.addInterceptor(new LoginPasswordAuthenticator(username, password));
return this;
}

public ServiceBuilder retryCount(final int count) {
return this.retryCount(count, TimeUnit.SECONDS.toMillis(1));
}

public ServiceBuilder retryCount(final int count, final long delayInMillis) {
if (count > 0) {
this.clientBuilder.addInterceptor(new RetryInterceptor(count, delayInMillis));
}
return this;
}

public ServiceBuilder withInterceptor(final Interceptor interceptor) {
this.clientBuilder.addInterceptor(interceptor);
return this;
}

public ServiceBuilder withLog(final boolean isDebug) {
if (!isDebug) {
return this;
}
final HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
this.clientBuilder.addInterceptor(logging);
return this;
}

public ServiceBuilder withDispatcher(final Dispatcher dispatcher) {
this.clientBuilder.dispatcher(dispatcher);
return this;
}

public ServiceBuilder insecure(final boolean insecure) {
if (insecure) {
turnOffSslValidation(clientBuilder);
}
return this;
}

public ServiceBuilder writeTimeout(final long timeunit, final TimeUnit unit) {
this.clientBuilder.writeTimeout(timeunit, unit);
return this;
}

public ServiceBuilder readTimeout(final long timeunit, final TimeUnit unit) {
this.clientBuilder.readTimeout(timeunit, unit);
return this;
}

public ServiceBuilder connectTimeout(final long timeunit, final TimeUnit unit) {
this.clientBuilder.connectTimeout(timeunit, unit);
return this;
}

public ServiceBuilder withRetryOnConnectionFailure() {
this.clientBuilder.retryOnConnectionFailure(true);
return this;
}

public <T> T create(final Class<T> clazz) {
final OkHttpClient client = clientBuilder
.build();
final ObjectMapper mapper = new ObjectMapper().disable(FAIL_ON_UNKNOWN_PROPERTIES);
final Retrofit retrofit = retrofitBuilder
.addConverterFactory(JacksonConverterFactory.create(mapper))
.addCallAdapterFactory(new ResponseCallAdapterFactory<>())
.client(client)
.build();
return retrofit.create(clazz);
}

@SuppressWarnings("PMD.EmptyCatchBlock")
private static void turnOffSslValidation(final OkHttpClient.Builder builder) {
try {
final X509TrustManager trustManager = new NonValidatingTrustManager();
final SSLContext ctx = SSLContext.getInstance("TLS"); // or "SSL" ?
ctx.init(null, new TrustManager[]{trustManager}, null);
final SSLSocketFactory sslSocketFactory = ctx.getSocketFactory();
builder.sslSocketFactory(sslSocketFactory, trustManager);
builder.hostnameVerifier((hostname, session) -> true);
} catch (NoSuchAlgorithmException | KeyManagementException e) {
// do nothing
}
}
}
Loading
Loading