diff --git a/src/main/java/com/mercadopago/client/store/StoreClient.java b/src/main/java/com/mercadopago/client/store/StoreClient.java new file mode 100644 index 00000000..0fd4e860 --- /dev/null +++ b/src/main/java/com/mercadopago/client/store/StoreClient.java @@ -0,0 +1,278 @@ +package com.mercadopago.client.store; + +import static com.mercadopago.MercadoPagoConfig.getStreamHandler; +import static com.mercadopago.serialization.Serializer.deserializeElementsResourcesPageFromJson; +import static com.mercadopago.serialization.Serializer.deserializeFromJson; +import static com.mercadopago.serialization.Serializer.serializeToJson; + +import com.google.gson.reflect.TypeToken; +import com.mercadopago.MercadoPagoConfig; +import com.mercadopago.client.MercadoPagoClient; +import com.mercadopago.core.MPRequestOptions; +import com.mercadopago.exceptions.MPApiException; +import com.mercadopago.exceptions.MPException; +import com.mercadopago.net.HttpMethod; +import com.mercadopago.net.MPElementsResourcesPage; +import com.mercadopago.net.MPHttpClient; +import com.mercadopago.net.MPRequest; +import com.mercadopago.net.MPResponse; +import com.mercadopago.net.MPSearchRequest; +import com.mercadopago.net.UrlFormatter; +import com.mercadopago.resources.store.Store; +import com.mercadopago.serialization.Serializer; +import java.lang.reflect.Type; +import java.util.logging.Logger; +import java.util.logging.StreamHandler; + +/** + * Client for the MercadoPago Stores API. + * + *

Provides operations to create, retrieve, search, update and delete stores associated with a + * MercadoPago user account. + * + * @see + * Stores API reference + */ +public class StoreClient extends MercadoPagoClient { + + /** Class-level logger for store operations. */ + private static final Logger LOGGER = Logger.getLogger(StoreClient.class.getName()); + + /** URL template for the store creation and search endpoints. */ + private static final String URL_WITH_USER_ID = "/users/%s/stores"; + + /** URL template for the single-store endpoints. */ + private static final String URL_WITH_ID = "/stores/%s"; + + /** URL template for the update and delete endpoints. */ + private static final String URL_WITH_USER_AND_ID = "/users/%s/stores/%s"; + + /** + * Default constructor. Uses the default HTTP client provided by {@link MercadoPagoConfig}. + */ + public StoreClient() { + this(MercadoPagoConfig.getHttpClient()); + } + + /** + * Constructs a {@code StoreClient} with a custom HTTP client. + * + * @param httpClient the {@link MPHttpClient} implementation used to execute HTTP requests + */ + public StoreClient(MPHttpClient httpClient) { + super(httpClient); + StreamHandler streamHandler = getStreamHandler(); + streamHandler.setLevel(MercadoPagoConfig.getLoggingLevel()); + LOGGER.addHandler(streamHandler); + LOGGER.setLevel(MercadoPagoConfig.getLoggingLevel()); + } + + /** + * Creates a new store for the given user. + * + * @param userId the unique identifier of the store owner user + * @param request the {@link StoreCreateRequest} with the store details + * @return the created {@link Store} + * @throws MPException if a transport-level or SDK-internal error occurs + * @throws MPApiException if the API returns a non-successful HTTP status code + */ + public Store create(Long userId, StoreCreateRequest request) throws MPException, MPApiException { + return this.create(userId, request, null); + } + + /** + * Creates a new store for the given user with custom request options. + * + * @param userId the unique identifier of the store owner user + * @param request the {@link StoreCreateRequest} with the store details + * @param requestOptions optional {@link MPRequestOptions} to override access token, headers, or + * timeouts for this single request; may be {@code null} + * @return the created {@link Store} + * @throws MPException if a transport-level or SDK-internal error occurs + * @throws MPApiException if the API returns a non-successful HTTP status code + */ + public Store create(Long userId, StoreCreateRequest request, MPRequestOptions requestOptions) + throws MPException, MPApiException { + LOGGER.info("Sending create store request"); + + MPRequest mpRequest = + MPRequest.builder() + .uri(String.format(URL_WITH_USER_ID, encodePathParam(userId))) + .method(HttpMethod.POST) + .payload(Serializer.serializeToJson(request)) + .build(); + + MPResponse response = send(mpRequest, requestOptions); + Store result = deserializeFromJson(Store.class, response.getContent()); + result.setResponse(response); + + return result; + } + + /** + * Retrieves a store by its unique identifier. + * + * @param id the unique identifier of the store + * @return the requested {@link Store} + * @throws MPException if a transport-level or SDK-internal error occurs + * @throws MPApiException if the API returns a non-successful HTTP status code + */ + public Store get(String id) throws MPException, MPApiException { + return this.get(id, null); + } + + /** + * Retrieves a store by its unique identifier with custom request options. + * + * @param id the unique identifier of the store + * @param requestOptions optional {@link MPRequestOptions} to override access token, headers, or + * timeouts for this single request; may be {@code null} + * @return the requested {@link Store} + * @throws MPException if a transport-level or SDK-internal error occurs + * @throws MPApiException if the API returns a non-successful HTTP status code + */ + public Store get(String id, MPRequestOptions requestOptions) throws MPException, MPApiException { + LOGGER.info("Sending get store request"); + + MPRequest mpRequest = + MPRequest.builder() + .uri(String.format(URL_WITH_ID, encodePathParam(id))) + .method(HttpMethod.GET) + .build(); + + MPResponse response = send(mpRequest, requestOptions); + Store result = deserializeFromJson(Store.class, response.getContent()); + result.setResponse(response); + + return result; + } + + /** + * Searches for stores of the given user matching the specified criteria. + * + * @param userId the unique identifier of the store owner user + * @param searchRequest the {@link MPSearchRequest} containing search filters and pagination + * parameters + * @return an {@link MPElementsResourcesPage} of {@link Store} with matching results + * @throws MPException if a transport-level or SDK-internal error occurs + * @throws MPApiException if the API returns a non-successful HTTP status code + */ + public MPElementsResourcesPage search(Long userId, MPSearchRequest searchRequest) + throws MPException, MPApiException { + return this.search(userId, searchRequest, null); + } + + /** + * Searches for stores of the given user with custom request options. + * + * @param userId the unique identifier of the store owner user + * @param searchRequest the {@link MPSearchRequest} containing search filters and pagination + * parameters + * @param requestOptions optional {@link MPRequestOptions} to override access token, headers, or + * timeouts for this single request; may be {@code null} + * @return an {@link MPElementsResourcesPage} of {@link Store} with matching results + * @throws MPException if a transport-level or SDK-internal error occurs + * @throws MPApiException if the API returns a non-successful HTTP status code + */ + public MPElementsResourcesPage search( + Long userId, MPSearchRequest searchRequest, MPRequestOptions requestOptions) + throws MPException, MPApiException { + LOGGER.info("Sending search store request"); + + String uri = + UrlFormatter.formatSearchUrl( + String.format(URL_WITH_USER_ID, encodePathParam(userId)) + "/search", searchRequest); + MPRequest mpRequest = MPRequest.builder().uri(uri).method(HttpMethod.GET).build(); + + MPResponse response = send(mpRequest, requestOptions); + + Type responseType = new TypeToken>() {}.getType(); + MPElementsResourcesPage result = + deserializeElementsResourcesPageFromJson(responseType, response.getContent()); + result.setResponse(response); + + return result; + } + + /** + * Updates an existing store. + * + * @param userId the unique identifier of the store owner user + * @param id the unique identifier of the store to update + * @param request the {@link StoreUpdateRequest} with the updated attributes + * @return the updated {@link Store} + * @throws MPException if a transport-level or SDK-internal error occurs + * @throws MPApiException if the API returns a non-successful HTTP status code + */ + public Store update(Long userId, String id, StoreUpdateRequest request) + throws MPException, MPApiException { + return this.update(userId, id, request, null); + } + + /** + * Updates an existing store with custom request options. + * + * @param userId the unique identifier of the store owner user + * @param id the unique identifier of the store to update + * @param request the {@link StoreUpdateRequest} with the updated attributes + * @param requestOptions optional {@link MPRequestOptions} to override access token, headers, or + * timeouts for this single request; may be {@code null} + * @return the updated {@link Store} + * @throws MPException if a transport-level or SDK-internal error occurs + * @throws MPApiException if the API returns a non-successful HTTP status code + */ + public Store update( + Long userId, String id, StoreUpdateRequest request, MPRequestOptions requestOptions) + throws MPException, MPApiException { + LOGGER.info("Sending update store request"); + + MPRequest mpRequest = + MPRequest.builder() + .uri(String.format(URL_WITH_USER_AND_ID, encodePathParam(userId), encodePathParam(id))) + .method(HttpMethod.PUT) + .payload(Serializer.serializeToJson(request)) + .build(); + + MPResponse response = send(mpRequest, requestOptions); + Store result = deserializeFromJson(Store.class, response.getContent()); + result.setResponse(response); + + return result; + } + + /** + * Deletes a store. + * + * @param userId the unique identifier of the store owner user + * @param id the unique identifier of the store to delete + * @throws MPException if a transport-level or SDK-internal error occurs + * @throws MPApiException if the API returns a non-successful HTTP status code + */ + public void delete(Long userId, String id) throws MPException, MPApiException { + this.delete(userId, id, null); + } + + /** + * Deletes a store with custom request options. + * + * @param userId the unique identifier of the store owner user + * @param id the unique identifier of the store to delete + * @param requestOptions optional {@link MPRequestOptions} to override access token, headers, or + * timeouts for this single request; may be {@code null} + * @throws MPException if a transport-level or SDK-internal error occurs + * @throws MPApiException if the API returns a non-successful HTTP status code + */ + public void delete(Long userId, String id, MPRequestOptions requestOptions) + throws MPException, MPApiException { + LOGGER.info("Sending delete store request"); + + MPRequest mpRequest = + MPRequest.builder() + .uri(String.format(URL_WITH_USER_AND_ID, encodePathParam(userId), encodePathParam(id))) + .method(HttpMethod.DELETE) + .build(); + + send(mpRequest, requestOptions); + } +} diff --git a/src/main/java/com/mercadopago/client/store/StoreCreateRequest.java b/src/main/java/com/mercadopago/client/store/StoreCreateRequest.java new file mode 100644 index 00000000..a2d7441b --- /dev/null +++ b/src/main/java/com/mercadopago/client/store/StoreCreateRequest.java @@ -0,0 +1,59 @@ +package com.mercadopago.client.store; + +import com.google.gson.annotations.SerializedName; +import java.util.Map; +import lombok.Builder; +import lombok.Getter; + +/** + * Request DTO used to create a new Store via POST /users/{user_id}/stores. + * + * @see Stores API Reference + */ +@Getter +@Builder +public class StoreCreateRequest { + /** Name of the store. */ + private final String name; + + /** External identifier used to correlate the store with the merchant's own system. */ + private final String externalId; + + /** Geographic location of the store. */ + private final StoreLocationRequest location; + + /** Business hours of the store, keyed by day of the week. */ + private final Map businessHours; + + /** Geographic location details of a store. */ + @Getter + @Builder + public static class StoreLocationRequest { + /** Address line of the store (e.g., street and number). */ + private final String addressLine; + + /** Street name of the store address. */ + private final String streetName; + + /** Street number of the store address. */ + private final String streetNumber; + + /** Zip code of the store address. */ + private final String zipCode; + + /** City name of the store address. */ + private final String cityName; + + /** State name of the store address. */ + private final String stateName; + + /** Latitude of the store location. */ + private final Double latitude; + + /** Longitude of the store location. */ + private final Double longitude; + + /** Additional reference to find the store location. */ + private final String reference; + } +} diff --git a/src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java b/src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java new file mode 100644 index 00000000..9d8aedf2 --- /dev/null +++ b/src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java @@ -0,0 +1,118 @@ +package com.mercadopago.client.store; + +import com.google.gson.annotations.SerializedName; +import lombok.Builder; +import lombok.Getter; + +/** + * Request DTO used to update an existing store ({@code PUT /users/{user_id}/stores/{id}}). + * + *

Only the attributes that the Stores API accepts on update are modeled here. Any field left + * as {@code null} is omitted from the JSON payload by the serializer, so only the provided + * attributes are updated. + * + * @see + * Stores API reference + * @see StoreClient#update(Long, String, StoreUpdateRequest) + */ +@Getter +@Builder +public class StoreUpdateRequest { + /** Name of the store to be updated. */ + private final String name; + + /** Geographic location of the store. */ + private final LocationRequest location; + + /** Business hours of the store. */ + private final BusinessHoursRequest businessHours; + + /** Additional data about the store. */ + private final StoreUpdateAdditionalInfoRequest additionalInfo; + + /** + * Geographic location of the store included in an update request. + * + *

Mirrors the {@code location} object of the store update request schema. + */ + @Getter + @Builder + public static class LocationRequest { + /** Street number of the store address. */ + private final Integer streetNumber; + + /** Street name of the store address. */ + private final String streetName; + + /** City name where the store is located. */ + private final String cityName; + + /** State name where the store is located. */ + private final String stateName; + + /** Latitude coordinate of the store location. */ + private final Double latitude; + + /** Longitude coordinate of the store location. */ + private final Double longitude; + + /** Reference to help find the store location. */ + private final String reference; + } + + /** + * Business hours of the store included in an update request. + * + *

Mirrors the {@code business_hours} object of the store update request schema. + */ + @Getter + @Builder + public static class BusinessHoursRequest { + /** Business hours for Monday. */ + private final DayHoursRequest monday; + + /** Business hours for Tuesday. */ + private final DayHoursRequest tuesday; + + /** Business hours for Wednesday. */ + private final DayHoursRequest wednesday; + + /** Business hours for Thursday. */ + private final DayHoursRequest thursday; + + /** Business hours for Friday. */ + private final DayHoursRequest friday; + + /** Business hours for Saturday. */ + private final DayHoursRequest saturday; + + /** Business hours for Sunday. */ + private final DayHoursRequest sunday; + } + + /** + * Open and close hours for a single day of the week. + */ + @Getter + @Builder + public static class DayHoursRequest { + /** Opening hour of the store (e.g. {@code "08:00"}). */ + private final String open; + + /** Closing hour of the store (e.g. {@code "18:00"}). */ + private final String close; + } + + /** + * Additional data about the store included in an update request. + * + *

Mirrors the {@code additional_info} object of the store update request schema. + */ + @Getter + @Builder + public static class StoreUpdateAdditionalInfoRequest { + /** External identifier of the store in the integrator's system. */ + private final String externalId; + } +} diff --git a/src/main/java/com/mercadopago/resources/store/Store.java b/src/main/java/com/mercadopago/resources/store/Store.java new file mode 100644 index 00000000..bae83754 --- /dev/null +++ b/src/main/java/com/mercadopago/resources/store/Store.java @@ -0,0 +1,89 @@ +package com.mercadopago.resources.store; + +import com.google.gson.annotations.SerializedName; +import com.mercadopago.net.MPResource; +import lombok.Getter; + +/** + * Resource representing a MercadoPago store. + * + *

Stores are physical or logical branches registered under a seller user account, used for + * in-person payments (e.g. Point of Sale devices). A store holds business information such as + * its name, opening hours, location, and an external identifier supplied by the integrator. + * + *

Instances are returned by the create, get, update and search operations of {@link + * com.mercadopago.client.store.StoreClient}. + * + * @see com.mercadopago.client.store.StoreClient + */ +@Getter +public class Store extends MPResource { + + /** Unique numeric identifier of the store. */ + private Long id; + + /** Numeric identifier of the user (seller) that owns the store. */ + @SerializedName("user_id") + private Long userId; + + /** Name of the store. */ + private String name; + + /** Opening hours of the store, as supplied by the integrator. */ + @SerializedName("business_hours") + private Object businessHours; + + /** ISO code of the timezone the store operates in (e.g. America/Argentina/Buenos_Aires). */ + @SerializedName("time_zone") + private String timeZone; + + /** Properties describing the geographic location of the store. */ + private StoreLocation location; + + /** External identifier supplied by the integrator when the store was created. */ + private String externalId; + + /** Date when the store was created, in ISO 8601 format. */ + @SerializedName("date_created") + private String dateCreated; + + /** + * Nested location properties of the store as declared by the Store response schema. + */ + @Getter + public static class StoreLocation { + + /** Street address line of the store (street name and number). */ + @SerializedName("address_line") + private String addressLine; + + /** Street name of the store address. */ + @SerializedName("street_name") + private String streetName; + + /** House or street number of the store address. */ + @SerializedName("street_number") + private String streetNumber; + + /** Name of the city where the store is located. */ + @SerializedName("city") + private String city; + + /** Name of the state or province where the store is located. */ + @SerializedName("state") + private String state; + + /** Postal code of the store address. */ + @SerializedName("zip_code") + private String zipCode; + + /** Latitude coordinate of the store location. */ + private Double latitude; + + /** Longitude coordinate of the store location. */ + private Double longitude; + + /** Reference point or additional address hint (e.g. a nearby landmark). */ + private String reference; + } +} diff --git a/src/test/java/com/mercadopago/client/store/StoreClientTest.java b/src/test/java/com/mercadopago/client/store/StoreClientTest.java new file mode 100644 index 00000000..b8873435 --- /dev/null +++ b/src/test/java/com/mercadopago/client/store/StoreClientTest.java @@ -0,0 +1,210 @@ +package com.mercadopago.client.store; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; + +import com.mercadopago.BaseClientTest; +import com.mercadopago.core.MPRequestOptions; +import com.mercadopago.exceptions.MPApiException; +import com.mercadopago.exceptions.MPException; +import com.google.gson.JsonElement; +import com.mercadopago.helper.MockHelper; +import com.mercadopago.net.HttpMethod; +import com.mercadopago.net.HttpStatus; +import com.mercadopago.net.MPElementsResourcesPage; +import com.mercadopago.net.MPSearchRequest; +import com.mercadopago.resources.store.Store; +import java.io.IOException; +import org.apache.http.HttpHeaders; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.protocol.HttpContext; +import org.junit.jupiter.api.Test; + +/** Tests for {@link StoreClient}. */ +class StoreClientTest extends BaseClientTest { + + private static final String STORE_BASE_RESPONSE = + "/mocks/response/store/store_base.json"; + private static final String STORE_UPDATED_RESPONSE = + "/mocks/response/store/store_updated.json"; + private static final String STORE_SEARCH_RESPONSE = + "/mocks/response/store/store_search.json"; + private static final String STORE_CREATE_REQUEST = + "/mocks/request/store/store_base.json"; + private static final String STORE_UPDATE_REQUEST = + "/mocks/request/store/store_update.json"; + + @Test + void createSuccess() throws IOException, MPException, MPApiException { + HttpResponse httpResponse = + MockHelper.generateHttpResponseFromFile(STORE_BASE_RESPONSE, HttpStatus.CREATED); + httpResponse.setHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON); + doReturn(httpResponse) + .when(HTTP_CLIENT) + .execute(any(HttpRequestBase.class), any(HttpContext.class)); + + StoreClient client = new StoreClient(HTTP_CLIENT); + StoreCreateRequest request = + StoreCreateRequest.builder().name("Store Name").build(); + Store store = client.create(123L, request); + + assertNotNull(store); + assertEquals("Store Name", store.getName()); + + JsonElement requestPayload = + MockHelper.generateJsonElementFromUriRequest(httpRequestCaptor.getValue()); + assertEquals( + MockHelper.generateJsonElement(STORE_CREATE_REQUEST), requestPayload); + } + + @Test + void createWithOptionsSuccess() throws IOException, MPException, MPApiException { + HttpResponse httpResponse = + MockHelper.generateHttpResponseFromFile(STORE_BASE_RESPONSE, HttpStatus.CREATED); + httpResponse.setHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON); + doReturn(httpResponse) + .when(HTTP_CLIENT) + .execute(any(HttpRequestBase.class), any(HttpContext.class)); + + StoreClient client = new StoreClient(HTTP_CLIENT); + StoreCreateRequest request = + StoreCreateRequest.builder().name("Store Name").build(); + Store store = client.create(123L, request, buildRequestOptions()); + + assertNotNull(store); + assertEquals("Store Name", store.getName()); + } + + @Test + void getSuccess() throws IOException, MPException, MPApiException { + HttpResponse httpResponse = + MockHelper.generateHttpResponseFromFile(STORE_BASE_RESPONSE, HttpStatus.OK); + httpResponse.setHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON); + doReturn(httpResponse) + .when(HTTP_CLIENT) + .execute(any(HttpRequestBase.class), any(HttpContext.class)); + + StoreClient client = new StoreClient(HTTP_CLIENT); + Store store = client.get("123"); + + assertNotNull(store); + assertEquals("Store Name", store.getName()); + } + + @Test + void getWithOptionsSuccess() throws IOException, MPException, MPApiException { + HttpResponse httpResponse = + MockHelper.generateHttpResponseFromFile(STORE_BASE_RESPONSE, HttpStatus.OK); + httpResponse.setHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON); + doReturn(httpResponse) + .when(HTTP_CLIENT) + .execute(any(HttpRequestBase.class), any(HttpContext.class)); + + StoreClient client = new StoreClient(HTTP_CLIENT); + Store store = client.get("123", buildRequestOptions()); + + assertNotNull(store); + assertEquals("Store Name", store.getName()); + } + + @Test + void searchSuccess() throws IOException, MPException, MPApiException { + HttpResponse httpResponse = + MockHelper.generateHttpResponseFromFile(STORE_SEARCH_RESPONSE, HttpStatus.OK); + httpResponse.setHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON); + doReturn(httpResponse) + .when(HTTP_CLIENT) + .execute(any(HttpRequestBase.class), any(HttpContext.class)); + + StoreClient client = new StoreClient(HTTP_CLIENT); + MPSearchRequest searchRequest = + MPSearchRequest.builder().limit(10).offset(0).build(); + MPElementsResourcesPage page = client.search(123L, searchRequest); + + assertNotNull(page); + assertNotNull(page.getElements()); + assertEquals(1, page.getElements().size()); + assertEquals("Store Name", page.getElements().get(0).getName()); + } + + @Test + void searchWithOptionsSuccess() throws IOException, MPException, MPApiException { + HttpResponse httpResponse = + MockHelper.generateHttpResponseFromFile(STORE_SEARCH_RESPONSE, HttpStatus.OK); + httpResponse.setHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON); + doReturn(httpResponse) + .when(HTTP_CLIENT) + .execute(any(HttpRequestBase.class), any(HttpContext.class)); + + StoreClient client = new StoreClient(HTTP_CLIENT); + MPSearchRequest builder = MPSearchRequest.builder().limit(10).offset(0).build(); + MPElementsResourcesPage page = + client.search(123L, builder, buildRequestOptions()); + + assertNotNull(page); + assertNotNull(page.getElements()); + assertEquals(1, page.getElements().size()); + assertEquals("Store Name", page.getElements().get(0).getName()); + } + + @Test + void updateSuccess() throws IOException, MPException, MPApiException { + HttpResponse httpResponse = + MockHelper.generateHttpResponseFromFile(STORE_UPDATED_RESPONSE, HttpStatus.OK); + httpResponse.setHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON); + doReturn(httpResponse) + .when(HTTP_CLIENT) + .execute(any(HttpRequestBase.class), any(HttpContext.class)); + + StoreClient client = new StoreClient(HTTP_CLIENT); + StoreUpdateRequest request = + StoreUpdateRequest.builder().name("Updated Store Name").build(); + Store store = client.update(123L, "123", request); + + assertNotNull(store); + assertEquals("Updated Store Name", store.getName()); + + JsonElement requestPayload = + MockHelper.generateJsonElementFromUriRequest(httpRequestCaptor.getValue()); + assertEquals( + MockHelper.generateJsonElement(STORE_UPDATE_REQUEST), requestPayload); + } + + @Test + void updateWithOptionsSuccess() throws IOException, MPException, MPApiException { + HttpResponse httpResponse = + MockHelper.generateHttpResponseFromFile(STORE_UPDATED_RESPONSE, HttpStatus.OK); + httpResponse.setHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON); + doReturn(httpResponse) + .when(HTTP_CLIENT) + .execute(any(HttpRequestBase.class), any(HttpContext.class)); + + StoreClient client = new StoreClient(HTTP_CLIENT); + StoreUpdateRequest request = + StoreUpdateRequest.builder().name("Updated Store Name").build(); + Store store = client.update(123L, "123", request, buildRequestOptions()); + + assertNotNull(store); + assertEquals("Updated Store Name", store.getName()); + } + + @Test + void deleteSuccess() throws IOException, MPException, MPApiException { + HttpResponse httpResponse = + MockHelper.generateHttpResponseFromFile(STORE_BASE_RESPONSE, HttpStatus.OK); + httpResponse.setHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON); + doReturn(httpResponse) + .when(HTTP_CLIENT) + .execute(httpRequestCaptor.capture(), any(HttpContext.class)); + + StoreClient client = new StoreClient(HTTP_CLIENT); + client.delete(123L, "123"); + + HttpRequestBase sentRequest = httpRequestCaptor.getValue(); + assertEquals(HttpMethod.DELETE, sentRequest.getMethod()); + assertEquals("/users/123/stores/123", sentRequest.getURI().getPath()); + } +} diff --git a/src/test/java/com/mercadopago/resources/mocks/request/store/store_base.json b/src/test/java/com/mercadopago/resources/mocks/request/store/store_base.json new file mode 100644 index 00000000..e12f5d13 --- /dev/null +++ b/src/test/java/com/mercadopago/resources/mocks/request/store/store_base.json @@ -0,0 +1,70 @@ +{ + "name": "Official Store", + "user_id": 123, + "business_hours": { + "monday": [ + { + "open": "08:00", + "close": "12:00" + }, + { + "open": "14:00", + "close": "18:00" + } + ], + "tuesday": [ + { + "open": "08:00", + "close": "12:00" + }, + { + "open": "14:00", + "close": "18:00" + } + ], + "wednesday": [ + { + "open": "08:00", + "close": "12:00" + }, + { + "open": "14:00", + "close": "18:00" + } + ], + "thursday": [ + { + "open": "08:00", + "close": "12:00" + }, + { + "open": "14:00", + "close": "18:00" + } + ], + "friday": [ + { + "open": "08:00", + "close": "12:00" + }, + { + "open": "14:00", + "close": "18:00" + } + ] + }, + "external_id": "MT_store_01", + "location": { + "address_line": "Av Casas Novas nº 15 wp", + "reference": "Mercado novo", + "latitude": -23.5817273, + "longitude": -46.6532865, + "open_hours": { + "monday": "08:00-12:00", + "tuesday": "08:00-12:00", + "wednesday": "08:00-12:00", + "thursday": "08:00-12:00", + "friday": "08:00-12:00" + } + } +} diff --git a/src/test/java/com/mercadopago/resources/mocks/request/store/store_update.json b/src/test/java/com/mercadopago/resources/mocks/request/store/store_update.json new file mode 100644 index 00000000..ec742caf --- /dev/null +++ b/src/test/java/com/mercadopago/resources/mocks/request/store/store_update.json @@ -0,0 +1,35 @@ +{ + "name": "Updated Store Name", + "location": { + "address_line": "Av. das Nações Unidas, 3003", + "city": "Osasco", + "state": "SP", + "zip_code": "06233-200", + "country_code": "BR", + "latitude": -23.5223, + "longitude": -46.7987, + "reference": "Bosque dos Eucaliptos" + }, + "business_hours": { + "monday": { + "open": "08:00", + "close": "18:00" + }, + "tuesday": { + "open": "08:00", + "close": "18:00" + }, + "wednesday": { + "open": "08:00", + "close": "18:00" + }, + "thursday": { + "open": "08:00", + "close": "18:00" + }, + "friday": { + "open": "08:00", + "close": "18:00" + } + } +} diff --git a/src/test/java/com/mercadopago/resources/mocks/response/store/store_base.json b/src/test/java/com/mercadopago/resources/mocks/response/store/store_base.json new file mode 100644 index 00000000..e8fe9465 --- /dev/null +++ b/src/test/java/com/mercadopago/resources/mocks/response/store/store_base.json @@ -0,0 +1,40 @@ +{ + "id": 3021, + "user_id": 823549964, + "name": "Store Name", + "external_id": "store_3021", + "date_created": "2022-01-10T10:10:10.000+00:00", + "location": { + "address_line": "Av. das Nações Unidas, 3003", + "reference": "Floor 12", + "latitude": -23.6101, + "longitude": -46.6975, + "city": "São Paulo", + "state": "SP", + "zip_code": "06233-200", + "country_code": "BR" + }, + "business_hours": { + "monday": { + "open": "08:00", + "close": "18:00" + }, + "tuesday": { + "open": "08:00", + "close": "18:00" + }, + "wednesday": { + "open": "08:00", + "close": "18:00" + }, + "thursday": { + "open": "08:00", + "close": "18:00" + }, + "friday": { + "open": "08:00", + "close": "18:00" + } + }, + "time_zone": "America/Argentina/Buenos_Aires" +} diff --git a/src/test/java/com/mercadopago/resources/mocks/response/store/store_search.json b/src/test/java/com/mercadopago/resources/mocks/response/store/store_search.json new file mode 100644 index 00000000..0b66eab6 --- /dev/null +++ b/src/test/java/com/mercadopago/resources/mocks/response/store/store_search.json @@ -0,0 +1,44 @@ +{ + "total": 1, + "next_offset": 1, + "elements": [ + { + "id": 123456, + "user_id": 823549964, + "name": "Store Name", + "date_creation": "2022-01-10T10:10:10.000+00:00", + "business_hours": { + "monday": { + "open": "08:00", + "close": "18:00" + }, + "tuesday": { + "open": "08:00", + "close": "18:00" + }, + "wednesday": { + "open": "08:00", + "close": "18:00" + }, + "thursday": { + "open": "08:00", + "close": "18:00" + }, + "friday": { + "open": "08:00", + "close": "18:00" + } + }, + "external_id": "STORE1234", + "location": { + "address_line": "Av. das Nações Unidas, 3003", + "reference": "Floor 12", + "city": "São Paulo", + "state": "SP", + "zip_code": "06233-200", + "latitude": -23.6103, + "longitude": -46.6975 + } + } + ] +} diff --git a/src/test/java/com/mercadopago/resources/mocks/response/store/store_updated.json b/src/test/java/com/mercadopago/resources/mocks/response/store/store_updated.json new file mode 100644 index 00000000..34d0610e --- /dev/null +++ b/src/test/java/com/mercadopago/resources/mocks/response/store/store_updated.json @@ -0,0 +1,80 @@ +{ + "id": 3021063929, + "name": "Updated Store Name", + "date_created": "2022-02-03T17:09:52.992-04:00", + "business_hours": { + "monday": [ + { + "open": "08:00", + "close": "15:00" + } + ], + "tuesday": [ + { + "open": "08:00", + "close": "15:00" + } + ], + "wednesday": [ + { + "open": "08:00", + "close": "15:00" + } + ], + "thursday": [ + { + "open": "08:00", + "close": "15:00" + } + ], + "friday": [ + { + "open": "08:00", + "close": "15:00" + } + ] + }, + "external_id": "main-store-update", + "location": { + "address_line": "Updated Street Name, 303, updated neighborhood", + "reference": "Updated Front of the store", + "city": { + "id": "TUAbQo6sBWL0", + "name": "Palermo" + }, + "latitude": -34.5711558, + "longitude": -58.4231926, + "open_hours": { + "monday": [ + { + "open": "09:00", + "close": "17:00" + } + ], + "tuesday": [ + { + "open": "09:00", + "close": "17:00" + } + ], + "wednesday": [ + { + "open": "09:00", + "close": "17:00" + } + ], + "thursday": [ + { + "open": "09:00", + "close": "17:00" + } + ], + "friday": [ + { + "open": "09:00", + "close": "17:00" + } + ] + } + } +}