Skip to content

Commit

Permalink
Change way of retriving address from google map api (#1087)
Browse files Browse the repository at this point in the history
* fix saveCurrentAddressForOrder and updateCurrentAddressForOrder methods

* refactor code

* fix tests

* add tests for GoogleApiService

* format code

* add new test

* add new test for update method

* fix updateProfileData service method

* add updateProfileDataWhenAddressPlaceIdIsNull test
  • Loading branch information
sergoliarnik authored Apr 19, 2023
1 parent d204a78 commit 7da5ae9
Show file tree
Hide file tree
Showing 16 changed files with 353 additions and 165 deletions.
4 changes: 3 additions & 1 deletion core/src/main/java/greencity/controller/OrderController.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ public ResponseEntity<OrderWithAddressesResponseDto> getAllAddressesForCurrentUs
@ApiResponses(value = {
@ApiResponse(code = 201, message = HttpStatuses.CREATED, response = OrderWithAddressesResponseDto.class),
@ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST),
@ApiResponse(code = 401, message = HttpStatuses.UNAUTHORIZED)
@ApiResponse(code = 401, message = HttpStatuses.UNAUTHORIZED),
@ApiResponse(code = 404, message = HttpStatuses.NOT_FOUND)
})
@PostMapping("/save-order-address")
public ResponseEntity<OrderWithAddressesResponseDto> saveAddressForOrder(
Expand Down Expand Up @@ -267,6 +268,7 @@ public ResponseEntity<OrderWithAddressesResponseDto> updateAddressForOrder(
@ApiOperation(value = "Delete order address")
@ApiResponses(value = {
@ApiResponse(code = 200, message = HttpStatuses.CREATED, response = OrderWithAddressesResponseDto.class),
@ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST),
@ApiResponse(code = 401, message = HttpStatuses.UNAUTHORIZED),
@ApiResponse(code = 403, message = HttpStatuses.FORBIDDEN),
@ApiResponse(code = 404, message = HttpStatuses.NOT_FOUND)
Expand Down
2 changes: 2 additions & 0 deletions core/src/test/java/greencity/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public static OrderAddressDtoRequest getOrderAddressDtoRequest() {
.city("Lviv")
.region("Lvivskiy")
.actual(false)
.placeId("place_id")
.build();
}

Expand Down Expand Up @@ -551,6 +552,7 @@ public static CreateAddressRequestDto getAddressRequestDto() {
.houseNumber("1")
.houseCorpus("2")
.entranceNumber("3")
.placeId("place_id")
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyObject;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
Expand Down Expand Up @@ -206,7 +205,7 @@ void saveAddressForOrder() throws Exception {
.principal(principal)
.contentType(MediaType.APPLICATION_JSON)).andExpect(status().isCreated());

verify(ubsClientService).saveCurrentAddressForOrder(anyObject(), eq("35467585763t4sfgchjfuyetf"));
verify(ubsClientService).saveCurrentAddressForOrder(any(), eq("35467585763t4sfgchjfuyetf"));
}

@Test
Expand All @@ -223,7 +222,7 @@ void updateAddressForOrder() throws Exception {
.principal(principal)
.contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk());

verify(ubsClientService).updateCurrentAddressForOrder(anyObject(), eq("35467585763t4sfgchjfuyetf"));
verify(ubsClientService).updateCurrentAddressForOrder(any(), eq("35467585763t4sfgchjfuyetf"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public interface AddressRepository extends CrudRepository<Address, Long> {
int capacity(double latitude, double longitude);

/**
* Method returns list of not deleted {@link Address}addresses for current user.
* Method returns list of not deleted {@link Address} addresses for current
* user.
*
* @return list of {@link Address}.
*/
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

<properties>
<junit-jupiter.version>5.7.0</junit-jupiter.version>
<mockito-junit-jupiter.version>3.3.3</mockito-junit-jupiter.version>
<mockito-core.version>3.3.3</mockito-core.version>
<mockito-junit-jupiter.version>4.0.0</mockito-junit-jupiter.version>
<mockito-core.version>4.0.0</mockito-core.version>
<modelmapper.version>2.3.5</modelmapper.version>
<spring-cloud-openfeign.version>2.1.5.RELEASE</spring-cloud-openfeign.version>
<java.version>11</java.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public final class ErrorMessage {
public static final String NO_SUCH_COORDINATES = "There are no any order with coordinates: ";
public static final String INAVALID_LITRES_AMOUNT = "The amount of litres should be between 0 and 10.000 litres.";
public static final String NOT_FOUND_ADDRESS_ID_FOR_CURRENT_USER = "Not found address with such id: ";
public static final String NOT_FOUND_ADDRESS_BY_PLACE_ID = "Not found address with such place id: ";
public static final String UNDELIVERED_ORDERS_NOT_FOUND = "There are no any undelivered orders found.";
public static final String PAYMENT_VALIDATION_ERROR = "The received payment data is not valid.";
public static final String THE_USER_ALREADY_HAS_CONNECTED_TO_TELEGRAM_BOT =
Expand Down Expand Up @@ -98,6 +99,8 @@ public final class ErrorMessage {
public static final String CANNOT_ACCESS_PAYMENT_STATUS = "Cannot access another user's payment status";
public static final String USER_HAS_NO_OVERPAYMENT = "This user has no overpayment";
public static final String CANNOT_DELETE_ADDRESS = "Cannot delete another user's address";
public static final String CANNOT_DELETE_ALREADY_DELETED_ADDRESS =
"Cannot delete an address that has already been deleted.";
public static final String CANNOT_ACCESS_PERSONAL_INFO = "Cannot access another user's personal info";
public static final String CANNOT_ACCESS_ORDER_CANCELLATION_REASON =
"Cannot access another user's order cancellation reason";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ public class CreateAddressRequestDto {
private String houseCorpus;
@Pattern(regexp = "[-A-Za-zА-Яа-яЇїІіЄєҐґ 0-9.,ʼ'`ʹ!?]*", message = validationMessage)
private String addressComment;
@NotEmpty
private String placeId;
}
21 changes: 18 additions & 3 deletions service-api/src/main/java/greencity/dto/address/AddressDto.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
package greencity.dto.address;

import greencity.entity.coords.Coordinates;
import lombok.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.*;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.io.Serializable;
import static greencity.constant.ValidationConstant.*;

import static greencity.constant.ValidationConstant.CH_EN;
import static greencity.constant.ValidationConstant.CH_NUM;
import static greencity.constant.ValidationConstant.CH_UA;
import static greencity.constant.ValidationConstant.CITY_EN_REGEXP;
import static greencity.constant.ValidationConstant.CITY_UK_REGEXP;

@Getter
@Setter
Expand Down Expand Up @@ -58,4 +71,6 @@ public class AddressDto implements Serializable {
@Length(max = 30)
@Pattern(regexp = CH_EN + "{1,30}")
private String districtEn;

private String placeId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import javax.validation.constraints.Max;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;

@Getter
Expand Down Expand Up @@ -63,4 +64,6 @@ public class OrderAddressDtoRequest {
@Length(max = 30)
@Pattern(regexp = "[a-zA-Z-\\s'.]{3,30}")
private String districtEn;
@NotEmpty
private String placeId;
}
6 changes: 6 additions & 0 deletions service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@
<version>${mockito-junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>${mockito-junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ private AddressDto createAddressDto(Address address) {
return AddressDto.builder()
.id(address.getId())
.city(address.getCity())
.cityEn(address.getCityEn())
.district(address.getDistrict())
.districtEn(address.getDistrictEn())
.region(address.getRegion())
.regionEn(address.getRegionEn())
.entranceNumber(address.getEntranceNumber())
.houseCorpus(address.getHouseCorpus())
.houseNumber(address.getHouseNumber())
.street(address.getStreet())
.streetEn(address.getStreetEn())
.addressComment(address.getAddressComment())
.coordinates(Coordinates.builder()
.latitude(address.getCoordinates().getLatitude())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import com.google.maps.GeoApiContext;
import com.google.maps.GeocodingApi;
import com.google.maps.errors.ApiException;
import com.google.maps.errors.InvalidRequestException;
import com.google.maps.model.GeocodingResult;

import greencity.constant.ErrorMessage;
import greencity.exceptions.NotFoundException;
import greencity.exceptions.api.GoogleApiException;
import lombok.Data;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

Expand All @@ -22,22 +22,22 @@ public class GoogleApiService {
private static final List<Locale> locales = List.of(new Locale("uk"), new Locale("en"));

/**
* Send request to the google and receive response with geocoding.
* Send request to the Google and receive response with geocoding.
*
* @param searchRequest - address for search
* @param placeId - place id
* @return GeocodingResults - return result from geocoding service
*/
public List<GeocodingResult> getResultFromGeoCode(String searchRequest, Integer langCode) {
List<GeocodingResult> geocodingResults = new ArrayList<>();

public GeocodingResult getResultFromGeoCode(String placeId, Integer langCode) {
try {
GeocodingResult[] results = GeocodingApi.newRequest(context)
.address(searchRequest).language(locales.get(langCode).getLanguage()).await();
Collections.addAll(geocodingResults, results);
.place(placeId).language(locales.get(langCode).getLanguage()).await();
return results[0];
} catch (IOException | InterruptedException | ApiException e) {
Thread.currentThread().interrupt();
if (e instanceof InvalidRequestException) {
throw new NotFoundException(ErrorMessage.NOT_FOUND_ADDRESS_BY_PLACE_ID + placeId);
}
throw new GoogleApiException(e.getMessage());
}
return geocodingResults;
}
}
Loading

0 comments on commit 7da5ae9

Please sign in to comment.