Skip to content

Commit

Permalink
Added account blocked publisher create,patch abilities
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimanaev committed Dec 16, 2020
1 parent dd8a426 commit 4aee6ac
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/taboola/backstage/Backstage.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public Backstage build() {
new CampaignItemsServiceImpl(performClientValidations, endpointsFactory.createEndpoint(BackstageCampaignItemsEndpoint.class)),
new DictionaryServiceImpl(endpointsFactory.createEndpoint(BackstageDictionaryEndpoint.class)),
new ReportsServiceImpl(endpointsFactory.createEndpoint(BackstageMediaReportsEndpoint.class), endpointsFactory.createEndpoint(BackstagePublisherReportsEndpoint.class), internalTools, organizeDynamicColumns),
new AccountsServiceImpl(endpointsFactory.createEndpoint(BackstageAccountEndpoint.class)),
new AccountsServiceImpl(performClientValidations, endpointsFactory.createEndpoint(BackstageAccountEndpoint.class)),
new CampaignPostalTargetingServiceImpl(performClientValidations, endpointsFactory.createEndpoint(BackstagePostalTargetingEndpoint.class)),
new CampaignAudienceTargetingServiceImpl(performClientValidations, endpointsFactory.createEndpoint(BackstageAudienceTargetingEndpoint.class))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
import com.taboola.backstage.model.dictionary.AudienceSegment;
import com.taboola.backstage.model.dictionary.LookalikeAudience;

import com.taboola.backstage.model.media.campaigns.AccountBlockedPublishers;
import com.taboola.backstage.model.media.account.AccountBlockedPublishersPatch;
import com.taboola.backstage.model.media.account.AccountBlockedPublishers;

import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Headers;
import retrofit2.http.PATCH;
import retrofit2.http.POST;
import retrofit2.http.Path;

/**
Expand Down Expand Up @@ -57,4 +62,16 @@ Results<LookalikeAudience> getSpecificCountryLookalikeAudiences(@Header("Authori
@Headers("Content-Type: application/json")
AccountBlockedPublishers getAccountBlockedPublishers(@Header("Authorization") String accessToken,
@Path("account_id") String accountId) throws BackstageAPIException;

@POST(BackstagePaths.BACKSTAGE_API_PATH_PREFIX + "/{account_id}/block-publisher")
@Headers("Content-Type: application/json")
AccountBlockedPublishers createAccountBlockedPublishers(@Header("Authorization") String accessToken,
@Path("account_id") String accountId,
@Body AccountBlockedPublishers accountBlockedPublishers) throws BackstageAPIException;

@PATCH(BackstagePaths.BACKSTAGE_API_PATH_PREFIX + "/{account_id}/block-publisher")
@Headers("Content-Type: application/json")
AccountBlockedPublishersPatch patchAccountBlockedPublishers(@Header("Authorization") String accessToken,
@Path("account_id") String accountId,
@Body AccountBlockedPublishersPatch accountBlockedPublishersPatch) throws BackstageAPIException;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.taboola.backstage.model.media.campaigns;
package com.taboola.backstage.model.media.account;

import java.util.Objects;
import java.util.Set;
import java.util.StringJoiner;

import com.taboola.backstage.annotations.Required;

public class AccountBlockedPublishers {

@Required
private Set<String> sites;

public Set<String> getSites() {
Expand All @@ -18,12 +20,8 @@ public void setSites(Set<String> sites) {

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AccountBlockedPublishers that = (AccountBlockedPublishers) o;
return Objects.equals(sites, that.sites);
}
Expand All @@ -35,8 +33,8 @@ public int hashCode() {

@Override
public String toString() {
return new StringJoiner(", ", AccountBlockedPublishers.class.getSimpleName() + "[", "]")
.add("sites=" + sites)
.toString();
return "AccountBlockedPublishers{" +
"sites=" + sites +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.taboola.backstage.model.media.account;

import java.util.Objects;
import java.util.Set;

import com.taboola.backstage.annotations.Required;
import com.taboola.backstage.model.Patch;

/**
* Created by vladi.m
* Date 16/12/2020
* Time 11:29
* Copyright Taboola
*/
public class AccountBlockedPublishersPatch extends Patch {

@Required
private Set<String> sites;

public Set<String> getSites() {
return sites;
}

public void setSites(Set<String> sites) {
this.sites = sites;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AccountBlockedPublishersPatch that = (AccountBlockedPublishersPatch) o;
return Objects.equals(sites, that.sites);
}

@Override
public int hashCode() {
return Objects.hash(sites);
}

@Override
public String toString() {
return "AccountBlockedPublishersPatch{" +
"patchOperation=" + patchOperation +
", sites=" + sites +
'}';
}
}
30 changes: 28 additions & 2 deletions src/main/java/com/taboola/backstage/services/AccountsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import com.taboola.backstage.model.auth.BackstageAuthentication;
import com.taboola.backstage.model.dictionary.AudienceSegment;
import com.taboola.backstage.model.dictionary.LookalikeAudience;
import com.taboola.backstage.model.media.campaigns.AccountBlockedPublishers;
import com.taboola.backstage.model.media.account.AccountBlockedPublishers;
import com.taboola.backstage.model.media.account.AccountBlockedPublishersPatch;

/**
* Accounts service allow fetching data regarding different account types (Partners, Publishers)
Expand Down Expand Up @@ -80,7 +81,7 @@ public interface AccountsService {

/**
* Read account-wide blocked publishers
*
* @param auth Authentication object {@link BackstageAuthentication}
* @param accountId {@link com.taboola.backstage.model.Account Account}. Taken from {@link com.taboola.backstage.model.Account#getAccountId Account.getAccountId()}
* @return Account blocked publishers model
Expand All @@ -89,4 +90,29 @@ public interface AccountsService {
* @throws BackstageAPIRequestException Bad request (HTTP status 4xx)
*/
AccountBlockedPublishers readAccountBlockedPublishers(BackstageAuthentication auth, String accountId) throws BackstageAPIUnauthorizedException, BackstageAPIConnectivityException, BackstageAPIRequestException;

/**
* Create account-wide blocked publishers
*
* @param auth Authentication object {@link BackstageAuthentication}
* @param accountId {@link com.taboola.backstage.model.Account Account}. Taken from {@link com.taboola.backstage.model.Account#getAccountId Account.getAccountId()}
* @return Account blocked publishers model
* @throws BackstageAPIUnauthorizedException {@link com.taboola.backstage.model.auth.Token Token} is expired or bad credentials
* @throws BackstageAPIConnectivityException Connectivity issues (HTTP status 5xx)
* @throws BackstageAPIRequestException Bad request (HTTP status 4xx)
*/
AccountBlockedPublishers createAccountBlockedPublishers(BackstageAuthentication auth, String accountId, AccountBlockedPublishers accountBlockedPublishers) throws BackstageAPIUnauthorizedException, BackstageAPIConnectivityException, BackstageAPIRequestException;

/**
* Patch account-wide blocked publishers
*
* @param auth Authentication object {@link BackstageAuthentication}
* @param accountId {@link com.taboola.backstage.model.Account Account}. Taken from {@link com.taboola.backstage.model.Account#getAccountId Account.getAccountId()}
* @return Account blocked publishers model in patch form
* @throws BackstageAPIUnauthorizedException {@link com.taboola.backstage.model.auth.Token Token} is expired or bad credentials
* @throws BackstageAPIConnectivityException Connectivity issues (HTTP status 5xx)
* @throws BackstageAPIRequestException Bad request (HTTP status 4xx)
*/
AccountBlockedPublishersPatch patchAccountBlockedPublishers(BackstageAuthentication auth, String accountId, AccountBlockedPublishersPatch accountBlockedPublishersPatch) throws BackstageAPIUnauthorizedException, BackstageAPIConnectivityException, BackstageAPIRequestException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import com.taboola.backstage.exceptions.BackstageAPIRequestException;
import com.taboola.backstage.exceptions.BackstageAPIUnauthorizedException;
import com.taboola.backstage.internal.BackstageAccountEndpoint;
import com.taboola.backstage.internal.FieldsValidator;
import com.taboola.backstage.model.Account;
import com.taboola.backstage.model.Results;
import com.taboola.backstage.model.auth.BackstageAuthentication;
import com.taboola.backstage.model.dictionary.AudienceSegment;
import com.taboola.backstage.model.dictionary.LookalikeAudience;
import com.taboola.backstage.model.media.campaigns.AccountBlockedPublishers;
import com.taboola.backstage.model.media.account.AccountBlockedPublishersPatch;
import com.taboola.backstage.model.media.account.AccountBlockedPublishers;

/**
* Created by vladi
Expand All @@ -19,9 +21,11 @@
*/
public class AccountsServiceImpl implements AccountsService {

private final Boolean performClientValidations;
private final BackstageAccountEndpoint endpoint;

public AccountsServiceImpl(BackstageAccountEndpoint endpoint) {
public AccountsServiceImpl(Boolean performClientValidations, BackstageAccountEndpoint endpoint) {
this.performClientValidations = performClientValidations;
this.endpoint = endpoint;
}

Expand Down Expand Up @@ -60,4 +64,24 @@ public AccountBlockedPublishers readAccountBlockedPublishers(BackstageAuthentica
String accessToken = auth.getToken().getAccessTokenForHeader();
return endpoint.getAccountBlockedPublishers(accessToken, accountId);
}

@Override
public AccountBlockedPublishers createAccountBlockedPublishers(BackstageAuthentication auth, String accountId, AccountBlockedPublishers accountBlockPublishers) throws BackstageAPIUnauthorizedException, BackstageAPIConnectivityException, BackstageAPIRequestException {
if(performClientValidations) {
FieldsValidator.validateCreateOperation(accountBlockPublishers);
}

String accessToken = auth.getToken().getAccessTokenForHeader();
return endpoint.createAccountBlockedPublishers(accessToken, accountId, accountBlockPublishers);
}

@Override
public AccountBlockedPublishersPatch patchAccountBlockedPublishers(BackstageAuthentication auth, String accountId, AccountBlockedPublishersPatch accountBlockedPublishersPatch) throws BackstageAPIUnauthorizedException, BackstageAPIConnectivityException, BackstageAPIRequestException {
if(performClientValidations) {
FieldsValidator.validateCreateOperation(accountBlockedPublishersPatch);
}

String accessToken = auth.getToken().getAccessTokenForHeader();
return endpoint.patchAccountBlockedPublishers(accessToken, accountId, accountBlockedPublishersPatch);
}
}
7 changes: 6 additions & 1 deletion src/test/java/com/taboola/backstage/BackstageTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import com.taboola.backstage.model.dynamic.DynamicFields;
import com.taboola.backstage.model.dynamic.DynamicFieldsMetadata;
import com.taboola.backstage.model.dynamic.DynamicRow;
import com.taboola.backstage.model.media.campaigns.AccountBlockedPublishers;
import com.taboola.backstage.model.media.account.AccountBlockedPublishers;
import com.taboola.backstage.model.media.account.AccountBlockedPublishersPatch;
import com.taboola.backstage.model.media.campaigns.Campaign;
import com.taboola.backstage.model.media.campaigns.CampaignLookalikeAudienceTargeting;
import com.taboola.backstage.model.media.campaigns.CampaignMultiTargetingCollection;
Expand Down Expand Up @@ -224,4 +225,8 @@ protected LookalikeAudience generateDummyLookalikeAudiences() {
protected AccountBlockedPublishers generateDummyAccountBlockedPublishers() {
return factory.manufacturePojo(AccountBlockedPublishers.class);
}

protected AccountBlockedPublishersPatch generateDummyAccountBlockedPublishersPatch() {
return factory.manufacturePojo(AccountBlockedPublishersPatch.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import com.taboola.backstage.model.Account;
import com.taboola.backstage.model.Results;
import com.taboola.backstage.model.auth.BackstageAuthentication;
import com.taboola.backstage.model.media.campaigns.AccountBlockedPublishers;
import com.taboola.backstage.model.media.account.AccountBlockedPublishers;
import org.junit.Before;
import org.junit.Test;
import com.taboola.backstage.BackstageTestBase;
import com.taboola.backstage.model.dictionary.AudienceSegment;
import com.taboola.backstage.model.dictionary.LookalikeAudience;
import com.taboola.backstage.model.media.account.AccountBlockedPublishersPatch;

import java.util.Collections;

Expand All @@ -31,7 +32,7 @@ public class AccountsServiceImplTest extends BackstageTestBase {
@Before
public void beforeTest() {
accountsEndpointMock = mock(BackstageAccountEndpoint.class);
testInstance = new AccountsServiceImpl(accountsEndpointMock);
testInstance = new AccountsServiceImpl(false, accountsEndpointMock);

reset(accountsEndpointMock);
}
Expand Down Expand Up @@ -100,4 +101,24 @@ public void testReadAccountBlockedPublishers() {
AccountBlockedPublishers actual = testInstance.readAccountBlockedPublishers(auth, "123");
assertEquals("Invalid publishers", publishers, actual);
}

@Test
public void testCreateReadAccountBlockedPublishers() {
AccountBlockedPublishers blockedPublishers = generateDummyAccountBlockedPublishers();
when(accountsEndpointMock.createAccountBlockedPublishers(any(), any(), any())).thenReturn(blockedPublishers);

BackstageAuthentication auth = generateDummyClientCredentialsBackstageAuth();
AccountBlockedPublishers actual = testInstance.createAccountBlockedPublishers(auth, "123", blockedPublishers);
assertEquals("Invalid blockedPublishers", blockedPublishers, actual);
}

@Test
public void testPatchAccountBlockedPublishers() {
AccountBlockedPublishersPatch blockedPublishersPatch = generateDummyAccountBlockedPublishersPatch();
when(accountsEndpointMock.patchAccountBlockedPublishers(any(), any(), any())).thenReturn(blockedPublishersPatch);

BackstageAuthentication auth = generateDummyClientCredentialsBackstageAuth();
AccountBlockedPublishersPatch actual = testInstance.patchAccountBlockedPublishers(auth, "123", blockedPublishersPatch);
assertEquals("Invalid blockedPublishersPatch", blockedPublishersPatch, actual);
}
}

0 comments on commit 4aee6ac

Please sign in to comment.