Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d98c2cfd authored by David Luhmer's avatar David Luhmer
Browse files

refactoring to make codacy happy

parent 8b12fac0
Loading
Loading
Loading
Loading
+19 −22
Original line number Diff line number Diff line
@@ -46,32 +46,25 @@ import retrofit2.http.Streaming;
public class NextcloudRetrofitServiceMethod<T> {

    private static String TAG = NextcloudRetrofitServiceMethod.class.getCanonicalName();
    final Annotation[] methodAnnotations;
    final Annotation[][] parameterAnnotationsArray;
    final Type[] parameterTypes;
    private final Annotation[] methodAnnotations;
    private final Annotation[][] parameterAnnotationsArray;
    private final Type[] parameterTypes;


    // Upper and lower characters, digits, underscores, and hyphens, starting with a character.
    private static final String PARAM = "[a-zA-Z][a-zA-Z0-9_-]*";
    private static final Pattern PARAM_URL_REGEX = Pattern.compile("\\{(" + PARAM + ")\\}");
    private static final Pattern PARAM_NAME_REGEX = Pattern.compile(PARAM);
    //private static final Pattern PARAM_NAME_REGEX = Pattern.compile(PARAM);

    private Method method;
    private String httpMethod;
    private @Nullable String relativeUrl;
    private @Nullable Headers headers;
    private @Nullable MediaType contentType;
    private boolean hasBody;
    private boolean isFormEncoded;
    private boolean isMultipart;
    private Type returnType;
    private boolean followRedirects = false;

    private final NextcloudRequest.Builder requestBuilder;

    private final String mApiEndpoint;
    private Set<String> relativeUrlParamNames;


    public NextcloudRetrofitServiceMethod(String apiEndpoint, Method method) {
        this.method = method;
@@ -79,7 +72,6 @@ public class NextcloudRetrofitServiceMethod<T> {
        this.methodAnnotations = method.getAnnotations();
        this.parameterTypes = method.getGenericParameterTypes();
        this.parameterAnnotationsArray = method.getParameterAnnotations();
        this.mApiEndpoint = apiEndpoint;

        for (Annotation annotation : methodAnnotations) {
            parseMethodAnnotation(annotation);
@@ -93,7 +85,7 @@ public class NextcloudRetrofitServiceMethod<T> {
                .setMethod(httpMethod)
                .setHeader(headers.toMultimap())
                .setFollowRedirects(followRedirects)
                .setUrl(new File(this.mApiEndpoint,relativeUrl).toString());
                .setUrl(new File(apiEndpoint, relativeUrl).toString());


        Log.d(TAG, "NextcloudRetrofitServiceMethod() called with: apiEndpoint = [" + apiEndpoint + "], method = [" + method + "]");
@@ -221,7 +213,7 @@ public class NextcloudRetrofitServiceMethod<T> {
                    this.httpMethod, httpMethod);
        }
        this.httpMethod = httpMethod;
        this.hasBody = hasBody;
        boolean hasBody1 = hasBody;

        if (value.isEmpty()) {
            return;
@@ -240,7 +232,7 @@ public class NextcloudRetrofitServiceMethod<T> {
        }

        this.relativeUrl = value;
        this.relativeUrlParamNames = parsePathParameters(value);
        //Set<String> relativeUrlParamNames = parsePathParameters(value);
    }

    private Headers parseHeaders(String[] headers) {
@@ -255,7 +247,9 @@ public class NextcloudRetrofitServiceMethod<T> {
            String headerValue = header.substring(colon + 1).trim();
            if ("Content-Type".equalsIgnoreCase(headerName)) {
                try {
                    contentType = MediaType.parse(headerValue);
                    MediaType.parse(headerValue);
                    //MediaType contentType = MediaType.parse(headerValue);
                    //Log.v(TAG, contentType.toString());
                } catch (IllegalArgumentException e) {
                    throw methodError(method, e, "Malformed content type: %s", headerValue);
                }
@@ -270,7 +264,8 @@ public class NextcloudRetrofitServiceMethod<T> {
     * Gets the set of unique path parameters used in the given URI. If a parameter is used twice
     * in the URI, it will only show up once in the set.
     */
    static Set<String> parsePathParameters(String path) {
    /*
    private static Set<String> parsePathParameters(String path) {
        Matcher m = PARAM_URL_REGEX.matcher(path);
        Set<String> patterns = new LinkedHashSet<>();
        while (m.find()) {
@@ -278,16 +273,17 @@ public class NextcloudRetrofitServiceMethod<T> {
        }
        return patterns;
    }
    */





    static RuntimeException methodError(Method method, String message, Object... args) {
    private  static RuntimeException methodError(Method method, String message, Object... args) {
        return methodError(method, null, message, args);
    }

    static RuntimeException methodError(Method method, @Nullable Throwable cause, String message,
    private  static RuntimeException methodError(Method method, @Nullable Throwable cause, String message,
                                        Object... args) {
        message = String.format(message, args);
        return new IllegalArgumentException(message
@@ -297,15 +293,16 @@ public class NextcloudRetrofitServiceMethod<T> {
                + method.getName(), cause);
    }

    static RuntimeException parameterError(Method method,
    /*
    private static RuntimeException parameterError(Method method,
                                           Throwable cause, int p, String message, Object... args) {
        return methodError(method, cause, message + " (parameter #" + (p + 1) + ")", args);
    }

    static RuntimeException parameterError(Method method, int p, String message, Object... args) {
    private static RuntimeException parameterError(Method method, int p, String message, Object... args) {
        return methodError(method, message + " (parameter #" + (p + 1) + ")", args);
    }

    */



+1 −1
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ public class NextcloudRetrofitApiBuilder {
                });
    }

    NextcloudRetrofitServiceMethod<?> loadServiceMethod(Method method) {
    private NextcloudRetrofitServiceMethod<?> loadServiceMethod(Method method) {
        NextcloudRetrofitServiceMethod<?> result = serviceMethodCache.get(method);
        if (result != null) return result;

+88 −29
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
@@ -18,11 +19,9 @@ import java.util.List;
import java.util.Map;

import io.reactivex.Completable;
import retrofit2.Call;
import retrofit2.NextcloudRetrofitApiBuilder;
import retrofit2.http.GET;
import retrofit2.http.Header;

import static junit.framework.TestCase.fail;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -47,7 +46,7 @@ public class TestRetrofitAPI {
    public MockitoRule mockitoRule = MockitoJUnit.rule();

    @Before
    public void setup() {
    public void setUp() {
        when(nextcloudApiMock.getGson()).thenReturn(new GsonBuilder().create());
        mApi = new NextcloudRetrofitApiBuilder(nextcloudApiMock, mApiEndpoint).create(API.class);
    }
@@ -77,13 +76,17 @@ public class TestRetrofitAPI {


    @Test
    public void postFolders() throws Exception {
    public void postFolders() {
        // @POST("folders")
        // Call<List<String>> postFolder(@Body Map<String, Object> folderMap);

        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "test");
        try {
            mApi.postFolder(map).execute();
        } catch (IOException e) {
            fail(e.getMessage());
        }

        String expectedBody = "{\"name\":\"test\"}";
        NextcloudRequest request = new NextcloudRequest.Builder()
@@ -93,11 +96,15 @@ public class TestRetrofitAPI {
                .build();

        Type type = new TypeToken<List<String>>() {}.getType();
        try {
            verify(nextcloudApiMock).performRequest(eq(type), eq(request));
        } catch (Exception e) {
            fail(e.getMessage());
        }
    }

    @Test
    public void putFeed() throws Exception {
    public void putFeed() {
        // @PUT("feeds/{feedId}/rename")
        // Completable putFeed(@Path("feedId") long feedId, @Body Map<String, String> paramMap);

@@ -114,12 +121,16 @@ public class TestRetrofitAPI {
                .build();

        Type type = new TypeToken<Completable>() {}.getType();
        try {
            verify(nextcloudApiMock).performRequest(eq(type), eq(request));
        } catch (Exception e) {
            fail(e.getMessage());
        }
    }


    @Test
    public void deleteFeed() throws Exception {
    public void deleteFeed() {
        // @DELETE("feeds/{feedId}")
        // Completable deleteFeed(@Path("feedId") long feedId);

@@ -130,12 +141,16 @@ public class TestRetrofitAPI {
                .build();

        Type type = new TypeToken<Completable>() {}.getType();
        try {
            verify(nextcloudApiMock).performRequest(eq(type), eq(request));
        } catch (Exception e) {
            fail(e.getMessage());
        }
    }


    @Test
    public void getItems() throws Exception {
    public void getItems() {
        //@GET("items")
        //    Call<List<String>> getItems(
        //    @Query("batchSize") long batchSize,
@@ -146,7 +161,11 @@ public class TestRetrofitAPI {
        //    @Query("oldestFirst") boolean oldestFirst
        //);

        try {
            mApi.getItems(100, 100, 1, 1, true, true).execute();
        } catch (IOException e) {
            fail(e.getMessage());
        }
        HashMap<String, String> params = new HashMap<>();
        params.put("batchSize", "100");
        params.put("offset", "100");
@@ -162,14 +181,18 @@ public class TestRetrofitAPI {
                .build();

        Type type = new TypeToken<List<String>>() {}.getType();
        try {
            verify(nextcloudApiMock).performRequest(eq(type), eq(request));
        } catch (Exception e) {
            fail(e.getMessage());
        }
    }




    @Test
    public void getStreamingUpdatedItems() throws Exception {
    public void getStreamingUpdatedItems() {
        //@GET("items/updated")
        //@Streaming
        //Observable<ResponseBody> getStreamingUpdatedItems(
@@ -191,16 +214,24 @@ public class TestRetrofitAPI {
                .setParameter(expectedParams)
                .build();

        try {
            verify(nextcloudApiMock).performNetworkRequest(eq(request));
        } catch (Exception e) {
            fail(e.getMessage());
        }
    }


    @Test
    public void putMarkItemsRead() throws Exception {
    public void putMarkItemsRead() {
        //@PUT("items/read/multiple")
        //Call<Void> putMarkItemsRead(@Body String items);

        try {
            mApi.putMarkItemsRead("[2, 3]").execute();
        } catch (IOException e) {
            fail(e.getMessage());
        }

        String expectedBody = "\"[2, 3]\"";
        NextcloudRequest request = new NextcloudRequest.Builder()
@@ -210,12 +241,16 @@ public class TestRetrofitAPI {
                .build();

        Type type = new TypeToken<Void>() {}.getType();
        try {
            verify(nextcloudApiMock).performRequest(eq(type), eq(request));
        } catch (Exception e) {
            fail(e.getMessage());
        }
    }


    @Test(expected = UnsupportedOperationException.class)
    public void testPatch() throws Exception {
    public void testPatch() throws IOException {
        //@PATCH("test")
        //Call<Void> invalidPATCH();

@@ -234,7 +269,7 @@ public class TestRetrofitAPI {
    }

    @Test
    public void testStaticHeaders() throws Exception {
    public void testStaticHeaders() {
        //@Headers({
        //    "X-Foo: Bar",
        //    "X-Ping: Pong"
@@ -242,7 +277,11 @@ public class TestRetrofitAPI {
        //@GET("test")
        //Call<Void> getWithHeader();

        try {
            mApi.getWithHeader().execute();
        } catch (IOException e) {
            fail(e.getMessage());
        }

        Map<String, List<String>> expectedHeader = new HashMap<>();
        List<String> foo = new ArrayList<>();
@@ -259,15 +298,23 @@ public class TestRetrofitAPI {
                .build();

        Type type = new TypeToken<Void>() {}.getType();
        try {
            verify(nextcloudApiMock).performRequest(eq(type), eq(request));
        } catch (Exception e) {
            fail(e.getMessage());
        }
    }

    @Test
    public void testDynamicHeaders() throws Exception {
    public void testDynamicHeaders() {
        //@GET("/test")
        //Call<Void> getDynamicHeader(@Header("Content-Range") String contentRange);

        try {
            mApi.getDynamicHeader("1").execute();
        } catch (IOException e) {
            fail(e.getMessage());
        }


        Map<String, List<String>> expectedHeader = new HashMap<>();
@@ -282,17 +329,25 @@ public class TestRetrofitAPI {
                .build();

        Type type = new TypeToken<Void>() {}.getType();
        try {
            verify(nextcloudApiMock).performRequest(eq(type), eq(request));
        } catch (Exception e) {
            fail(e.getMessage());
        }
    }


    @Test
    public void testFollowRedirects() throws Exception {
    public void testFollowRedirects() {
        //@NextcloudAPI.FollowRedirects
        //@GET("/test")
        //Call<Void> getFollowRedirects();

        try {
            mApi.getFollowRedirects().execute();
        } catch (IOException e) {
            fail(e.getMessage());
        }

        NextcloudRequest request = new NextcloudRequest.Builder()
                .setMethod("GET")
@@ -301,6 +356,10 @@ public class TestRetrofitAPI {
                .build();

        Type type = new TypeToken<Void>() {}.getType();
        try {
            verify(nextcloudApiMock).performRequest(eq(type), eq(request));
        } catch (Exception e) {
            fail(e.getMessage());
        }
    }
}