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

[Nullability Annotations to Java Classes] Add Missing Nullability Annotations to TaxonomyRestClient (safe) #2826

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
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.wordpress.android.fluxc.release;

import androidx.annotation.NonNull;

import org.greenrobot.eventbus.Subscribe;
import org.junit.Test;
import org.wordpress.android.fluxc.TestUtils;
Expand Down Expand Up @@ -27,6 +29,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class ReleaseStack_TaxonomyTestWPCom extends ReleaseStack_WPComBase {
@Inject TaxonomyStore mTaxonomyStore;
Expand Down Expand Up @@ -327,37 +330,52 @@ public void onTermUploaded(OnTermUploaded event) {
mCountDownLatch.countDown();
}

private void setupTermAttributes(TermModel term) {
private void setupTermAttributes(@NonNull TermModel term) {
term.setName(TERM_DEFAULT_NAME + "-" + RandomStringUtils.randomAlphanumeric(4));
term.setDescription(TERM_DEFAULT_DESCRIPTION);
}

@NonNull
private TermModel createNewCategory() {
TermModel term = mTaxonomyStore.instantiateCategory(sSite);

assertEquals(0, term.getRemoteTermId());
assertNotSame(0, term.getId());
assertNotSame(0, term.getLocalSiteId());
if (term != null) {
assertEquals(0, term.getRemoteTermId());
assertNotSame(0, term.getId());
assertNotSame(0, term.getLocalSiteId());
} else {
fail("Failed to instantiate new category!");
}

return term;
}

@NonNull
private TermModel createNewTag() {
TermModel term = mTaxonomyStore.instantiateTag(sSite);

assertEquals(0, term.getRemoteTermId());
assertNotSame(0, term.getId());
assertNotSame(0, term.getLocalSiteId());
if (term != null) {
assertEquals(0, term.getRemoteTermId());
assertNotSame(0, term.getId());
assertNotSame(0, term.getLocalSiteId());
} else {
fail("Failed to instantiate new tag!");
}

return term;
}

private TermModel createNewTerm(TaxonomyModel taxonomy) {
@NonNull
private TermModel createNewTerm(@NonNull TaxonomyModel taxonomy) {
TermModel term = mTaxonomyStore.instantiateTerm(sSite, taxonomy);

assertEquals(0, term.getRemoteTermId());
assertNotSame(0, term.getId());
assertNotSame(0, term.getLocalSiteId());
if (term != null) {
assertEquals(0, term.getRemoteTermId());
assertNotSame(0, term.getId());
assertNotSame(0, term.getLocalSiteId());
} else {
fail("Failed to instantiate new term!");
}

return term;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,15 @@ private void fetchTagsFirstSite() {

private void createCategory() {
TermModel newCategory = mTaxonomyStore.instantiateCategory(getFirstSite());
newCategory.setName("FluxC-category-" + new Random().nextLong());
newCategory.setDescription("From FluxC example app");
if (newCategory != null) {
newCategory.setName("FluxC-category-" + new Random().nextLong());
newCategory.setDescription("From FluxC example app");

RemoteTermPayload payload = new RemoteTermPayload(newCategory, getFirstSite());
mDispatcher.dispatch(TaxonomyActionBuilder.newPushTermAction(payload));
RemoteTermPayload payload = new RemoteTermPayload(newCategory, getFirstSite());
mDispatcher.dispatch(TaxonomyActionBuilder.newPushTermAction(payload));
} else {
prependToLog("Error: no category found!");
}
}

@SuppressWarnings("unused")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import androidx.annotation.NonNull;

import com.android.volley.RequestQueue;
import com.android.volley.Response.Listener;

import org.apache.commons.text.StringEscapeUtils;
import org.wordpress.android.fluxc.Dispatcher;
Expand All @@ -18,8 +17,6 @@
import org.wordpress.android.fluxc.network.UserAgent;
import org.wordpress.android.fluxc.network.rest.wpcom.BaseWPComRestClient;
import org.wordpress.android.fluxc.network.rest.wpcom.WPComGsonRequest;
import org.wordpress.android.fluxc.network.rest.wpcom.WPComGsonRequest.WPComErrorListener;
import org.wordpress.android.fluxc.network.rest.wpcom.WPComGsonRequest.WPComGsonNetworkError;
import org.wordpress.android.fluxc.network.rest.wpcom.auth.AccessToken;
import org.wordpress.android.fluxc.network.rest.wpcom.taxonomy.TermWPComRestResponse.TermsResponse;
import org.wordpress.android.fluxc.store.TaxonomyStore.FetchTermResponsePayload;
Expand All @@ -39,86 +36,73 @@

@Singleton
public class TaxonomyRestClient extends BaseWPComRestClient {
@Inject public TaxonomyRestClient(Context appContext,
Dispatcher dispatcher,
@Named("regular") RequestQueue requestQueue,
AccessToken accessToken,
UserAgent userAgent) {
@Inject public TaxonomyRestClient(
Context appContext,
Dispatcher dispatcher,
@Named("regular") RequestQueue requestQueue,
AccessToken accessToken,
UserAgent userAgent) {
super(appContext, dispatcher, requestQueue, accessToken, userAgent);
}

public void fetchTerm(final TermModel term, final SiteModel site) {
public void fetchTerm(@NonNull final TermModel term, @NonNull final SiteModel site) {
final String taxonomy = term.getTaxonomy();
final String slug = term.getSlug();
String url = WPCOMREST.sites.site(site.getSiteId()).taxonomies.taxonomy(taxonomy).terms.slug(slug).getUrlV1_1();

final WPComGsonRequest request = WPComGsonRequest.buildGetRequest(url, null,
final WPComGsonRequest<TermWPComRestResponse> request = WPComGsonRequest.buildGetRequest(url, null,
TermWPComRestResponse.class,
new Listener<TermWPComRestResponse>() {
@Override
public void onResponse(TermWPComRestResponse response) {
TermModel fetchedTerm = termResponseToTermModel(response);
fetchedTerm.setId(term.getId());
fetchedTerm.setTaxonomy(taxonomy);
fetchedTerm.setLocalSiteId(site.getId());

FetchTermResponsePayload payload = new FetchTermResponsePayload(fetchedTerm, site);
mDispatcher.dispatch(TaxonomyActionBuilder.newFetchedTermAction(payload));
}
response -> {
TermModel fetchedTerm = termResponseToTermModel(response);
fetchedTerm.setId(term.getId());
fetchedTerm.setTaxonomy(taxonomy);
fetchedTerm.setLocalSiteId(site.getId());

FetchTermResponsePayload payload = new FetchTermResponsePayload(fetchedTerm, site);
mDispatcher.dispatch(TaxonomyActionBuilder.newFetchedTermAction(payload));
},
new WPComErrorListener() {
@Override
public void onErrorResponse(@NonNull WPComGsonNetworkError error) {
// Possible non-generic errors: 400 invalid_taxonomy
TaxonomyError taxonomyError = new TaxonomyError(error.apiError, error.message);
FetchTermResponsePayload payload = new FetchTermResponsePayload(term, site);
payload.error = taxonomyError;
mDispatcher.dispatch(TaxonomyActionBuilder.newFetchedTermAction(payload));
}
}
);
error -> {
// Possible non-generic errors: 400 invalid_taxonomy
TaxonomyError taxonomyError = new TaxonomyError(error.apiError, error.message);
FetchTermResponsePayload payload = new FetchTermResponsePayload(term, site);
payload.error = taxonomyError;
mDispatcher.dispatch(TaxonomyActionBuilder.newFetchedTermAction(payload));
});
add(request);
}

public void fetchTerms(final SiteModel site, final String taxonomyName) {
public void fetchTerms(@NonNull final SiteModel site, @NonNull final String taxonomyName) {
String url = WPCOMREST.sites.site(site.getSiteId()).taxonomies.taxonomy(taxonomyName).terms.getUrlV1_1();

Map<String, String> params = new HashMap<>();
params.put("number", "1000");

final WPComGsonRequest request = WPComGsonRequest.buildGetRequest(url, params,
final WPComGsonRequest<TermsResponse> request = WPComGsonRequest.buildGetRequest(url, params,
TermsResponse.class,
new Listener<TermsResponse>() {
@Override
public void onResponse(TermsResponse response) {
List<TermModel> termArray = new ArrayList<>();
TermModel term;
for (TermWPComRestResponse termResponse : response.terms) {
term = termResponseToTermModel(termResponse);
term.setTaxonomy(taxonomyName);
term.setLocalSiteId(site.getId());
termArray.add(term);
}

FetchTermsResponsePayload payload = new FetchTermsResponsePayload(new TermsModel(termArray),
site, taxonomyName);
mDispatcher.dispatch(TaxonomyActionBuilder.newFetchedTermsAction(payload));
response -> {
List<TermModel> termArray = new ArrayList<>();
TermModel term;
for (TermWPComRestResponse termResponse : response.terms) {
term = termResponseToTermModel(termResponse);
term.setTaxonomy(taxonomyName);
term.setLocalSiteId(site.getId());
termArray.add(term);
}

FetchTermsResponsePayload payload = new FetchTermsResponsePayload(new TermsModel(termArray),
site, taxonomyName);
mDispatcher.dispatch(TaxonomyActionBuilder.newFetchedTermsAction(payload));
},
new WPComErrorListener() {
@Override
public void onErrorResponse(@NonNull WPComGsonNetworkError error) {
// Possible non-generic errors: 400 invalid_taxonomy
TaxonomyError taxonomyError = new TaxonomyError(error.apiError, error.message);
FetchTermsResponsePayload payload = new FetchTermsResponsePayload(taxonomyError, taxonomyName);
mDispatcher.dispatch(TaxonomyActionBuilder.newFetchedTermsAction(payload));
}
}
);
error -> {
// Possible non-generic errors: 400 invalid_taxonomy
TaxonomyError taxonomyError = new TaxonomyError(error.apiError, error.message);
FetchTermsResponsePayload payload = new FetchTermsResponsePayload(taxonomyError, taxonomyName);
mDispatcher.dispatch(TaxonomyActionBuilder.newFetchedTermsAction(payload));
});
add(request);
}

public void pushTerm(final TermModel term, final SiteModel site) {
public void pushTerm(@NonNull final TermModel term, @NonNull final SiteModel site) {
final String taxonomy = term.getTaxonomy();
TaxonomiesEndpoint endpoint = WPCOMREST.sites.site(site.getSiteId()).taxonomies;
String url = term.getRemoteTermId() > 0
Expand All @@ -127,80 +111,67 @@ public void pushTerm(final TermModel term, final SiteModel site) {

Map<String, Object> body = termModelToParams(term);

final WPComGsonRequest request = WPComGsonRequest.buildPostRequest(url, body,
final WPComGsonRequest<TermWPComRestResponse> request = WPComGsonRequest.buildPostRequest(url, body,
TermWPComRestResponse.class,
new Listener<TermWPComRestResponse>() {
@Override
public void onResponse(TermWPComRestResponse response) {
TermModel uploadedTerm = termResponseToTermModel(response);
response -> {
TermModel uploadedTerm = termResponseToTermModel(response);

uploadedTerm.setId(term.getId());
uploadedTerm.setLocalSiteId(site.getId());
uploadedTerm.setTaxonomy(taxonomy);
uploadedTerm.setId(term.getId());
uploadedTerm.setLocalSiteId(site.getId());
uploadedTerm.setTaxonomy(taxonomy);

RemoteTermPayload payload = new RemoteTermPayload(uploadedTerm, site);
mDispatcher.dispatch(TaxonomyActionBuilder.newPushedTermAction(payload));
}
RemoteTermPayload payload = new RemoteTermPayload(uploadedTerm, site);
mDispatcher.dispatch(TaxonomyActionBuilder.newPushedTermAction(payload));
},
new WPComErrorListener() {
@Override
public void onErrorResponse(@NonNull WPComGsonNetworkError error) {
// Possible non-generic errors: 400 invalid_taxonomy, 409 duplicate
RemoteTermPayload payload = new RemoteTermPayload(term, site);
payload.error = new TaxonomyError(error.apiError, error.message);
mDispatcher.dispatch(TaxonomyActionBuilder.newPushedTermAction(payload));
}
}
);
error -> {
// Possible non-generic errors: 400 invalid_taxonomy, 409 duplicate
RemoteTermPayload payload = new RemoteTermPayload(term, site);
payload.error = new TaxonomyError(error.apiError, error.message);
mDispatcher.dispatch(TaxonomyActionBuilder.newPushedTermAction(payload));
});

request.addQueryParameter("context", "edit");

request.disableRetries();
add(request);
}

public void deleteTerm(final TermModel term, final SiteModel site) {
public void deleteTerm(@NonNull final TermModel term, @NonNull final SiteModel site) {
final String taxonomy = term.getTaxonomy();
String url = WPCOMREST.sites.site(site.getSiteId()).taxonomies.taxonomy(taxonomy).terms
.slug(term.getSlug()).delete.getUrlV1_1();

final WPComGsonRequest request = WPComGsonRequest.buildPostRequest(url, null,
final WPComGsonRequest<TermWPComRestResponse> request = WPComGsonRequest.buildPostRequest(url, null,
TermWPComRestResponse.class,
new Listener<TermWPComRestResponse>() {
@Override
public void onResponse(TermWPComRestResponse response) {
RemoteTermPayload payload = new RemoteTermPayload(term, site);
mDispatcher.dispatch(TaxonomyActionBuilder.newDeletedTermAction(payload));
}
response -> {
RemoteTermPayload payload = new RemoteTermPayload(term, site);
mDispatcher.dispatch(TaxonomyActionBuilder.newDeletedTermAction(payload));
},
new WPComErrorListener() {
@Override
public void onErrorResponse(@NonNull WPComGsonNetworkError error) {
// Possible non-generic errors: 400 invalid_taxonomy, 409 duplicate
RemoteTermPayload payload = new RemoteTermPayload(term, site);
payload.error = new TaxonomyError(error.apiError, error.message);
mDispatcher.dispatch(TaxonomyActionBuilder.newDeletedTermAction(payload));
}
}
);
error -> {
// Possible non-generic errors: 400 invalid_taxonomy, 409 duplicate
RemoteTermPayload payload = new RemoteTermPayload(term, site);
payload.error = new TaxonomyError(error.apiError, error.message);
mDispatcher.dispatch(TaxonomyActionBuilder.newDeletedTermAction(payload));
});

request.disableRetries();
add(request);
}

private TermModel termResponseToTermModel(TermWPComRestResponse from) {
@NonNull
private TermModel termResponseToTermModel(@NonNull TermWPComRestResponse from) {
TermModel term = new TermModel();
term.setRemoteTermId(from.ID);
term.setName(StringEscapeUtils.unescapeHtml4(from.name));
term.setSlug(from.slug);
term.setDescription(StringEscapeUtils.unescapeHtml4(from.description));
term.setParentRemoteId(from.parent);
term.setPostCount(from.post_count);

return term;
}

private Map<String, Object> termModelToParams(TermModel term) {
@NonNull
private Map<String, Object> termModelToParams(@NonNull TermModel term) {
Map<String, Object> body = new HashMap<>();

body.put("name", StringUtils.notNullStr(term.getName()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
package org.wordpress.android.fluxc.network.rest.wpcom.taxonomy;

import androidx.annotation.NonNull;

import org.wordpress.android.fluxc.network.Response;

import java.util.List;

@SuppressWarnings("NotNullFieldNotInitialized")
public class TermWPComRestResponse implements Response {
public class TermsResponse {
public List<TermWPComRestResponse> terms;
public static class TermsResponse {
irfano marked this conversation as resolved.
Show resolved Hide resolved
@NonNull public List<TermWPComRestResponse> terms;
}

public long ID;
public String name;
public String slug;
public String description;
@NonNull public String name;
@NonNull public String slug;
@NonNull public String description;
irfano marked this conversation as resolved.
Show resolved Hide resolved
public int post_count;
public long parent;
public Meta meta;

public class Meta {
public Links links;

public class Links {
public String self;
public String help;
public String site;
}
}
}
Loading