generated from ita-social-projects/DevTemplate
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new endpoint to retrieve address for order by order id (#1608)
- Loading branch information
1 parent
adc52eb
commit 091c5f2
Showing
9 changed files
with
230 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
service-api/src/main/java/greencity/service/ubs/AddressService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package greencity.service.ubs; | ||
|
||
import greencity.dto.address.UpdateAddressDto; | ||
|
||
public interface AddressService { | ||
/** | ||
* Gets address for given order id. | ||
* | ||
* @param orderId id of order | ||
* @return address with given order id | ||
* @author Dmytro Kizerov | ||
*/ | ||
UpdateAddressDto getAddressForOrder(Long orderId); | ||
} |
32 changes: 32 additions & 0 deletions
32
service/src/main/java/greencity/mapping/location/OrderAddressToUpdateAddressDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package greencity.mapping.location; | ||
|
||
import greencity.dto.address.UpdateAddressDto; | ||
import greencity.dto.order.OrderAddressExportDetailsDtoUpdate; | ||
import greencity.entity.user.ubs.OrderAddress; | ||
import org.modelmapper.AbstractConverter; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class OrderAddressToUpdateAddressDto extends AbstractConverter<OrderAddress, UpdateAddressDto> { | ||
@Override | ||
protected UpdateAddressDto convert(OrderAddress source) { | ||
return UpdateAddressDto.builder() | ||
.orderAddressExportDetails( | ||
OrderAddressExportDetailsDtoUpdate.builder() | ||
.id(source.getId()) | ||
.district(source.getDistrict()) | ||
.districtEn(source.getDistrictEn()) | ||
.street(source.getStreet()) | ||
.streetEn(source.getStreetEn()) | ||
.houseCorpus(source.getHouseCorpus()) | ||
.entranceNumber(source.getEntranceNumber()) | ||
.houseNumber(source.getHouseNumber()) | ||
.city(source.getCity()) | ||
.cityEn(source.getCityEn()) | ||
.region(source.getRegion()) | ||
.regionEn(source.getRegionEn()) | ||
.addressComment(source.getAddressComment()) | ||
.build()) | ||
.build(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
service/src/main/java/greencity/service/ubs/AddressServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package greencity.service.ubs; | ||
|
||
import greencity.constant.ErrorMessage; | ||
import greencity.dto.address.UpdateAddressDto; | ||
import greencity.entity.user.ubs.OrderAddress; | ||
import greencity.exceptions.NotFoundException; | ||
import greencity.repository.OrderAddressRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.modelmapper.ModelMapper; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class AddressServiceImpl implements AddressService { | ||
private final OrderAddressRepository orderAddressRepository; | ||
private final ModelMapper mapper; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
@Transactional(readOnly = true) | ||
public UpdateAddressDto getAddressForOrder(Long orderId) { | ||
OrderAddress orderAddress = orderAddressRepository.findByOrderId(orderId) | ||
.orElseThrow(() -> new NotFoundException(ErrorMessage.NOT_FOUND_ADDRESS_BY_ORDER_ID + orderId)); | ||
UpdateAddressDto addressDto = mapper.map(orderAddress, UpdateAddressDto.class); | ||
addressDto.setOrderId(orderId); | ||
return addressDto; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
service/src/test/java/greencity/mapping/location/OrderAddressToUpdateAddressDtoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package greencity.mapping.location; | ||
|
||
import greencity.ModelUtils; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class OrderAddressToUpdateAddressDtoTest { | ||
@InjectMocks | ||
private OrderAddressToUpdateAddressDto mapper; | ||
|
||
@Test | ||
void convertTest() { | ||
var orderAddress = ModelUtils.getOrderAddress(); | ||
|
||
var result = mapper.convert(orderAddress); | ||
|
||
assertEquals(orderAddress.getId(), result.getOrderAddressExportDetails().getId()); | ||
assertEquals(orderAddress.getDistrict(), result.getOrderAddressExportDetails().getDistrict()); | ||
assertEquals(orderAddress.getDistrictEn(), result.getOrderAddressExportDetails().getDistrictEn()); | ||
assertEquals(orderAddress.getStreet(), result.getOrderAddressExportDetails().getStreet()); | ||
assertEquals(orderAddress.getStreetEn(), result.getOrderAddressExportDetails().getStreetEn()); | ||
assertEquals(orderAddress.getHouseCorpus(), result.getOrderAddressExportDetails().getHouseCorpus()); | ||
assertEquals(orderAddress.getEntranceNumber(), result.getOrderAddressExportDetails().getEntranceNumber()); | ||
assertEquals(orderAddress.getHouseNumber(), result.getOrderAddressExportDetails().getHouseNumber()); | ||
assertEquals(orderAddress.getCity(), result.getOrderAddressExportDetails().getCity()); | ||
assertEquals(orderAddress.getCityEn(), result.getOrderAddressExportDetails().getCityEn()); | ||
assertEquals(orderAddress.getRegion(), result.getOrderAddressExportDetails().getRegion()); | ||
assertEquals(orderAddress.getRegionEn(), result.getOrderAddressExportDetails().getRegionEn()); | ||
assertEquals(orderAddress.getAddressComment(), result.getOrderAddressExportDetails().getAddressComment()); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
service/src/test/java/greencity/service/ubs/AddressServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package greencity.service.ubs; | ||
|
||
import greencity.ModelUtils; | ||
import greencity.dto.address.UpdateAddressDto; | ||
import greencity.entity.user.ubs.OrderAddress; | ||
import greencity.exceptions.NotFoundException; | ||
import greencity.repository.OrderAddressRepository; | ||
import java.util.Optional; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.modelmapper.ModelMapper; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class AddressServiceTest { | ||
@Mock | ||
private OrderAddressRepository orderAddressRepository; | ||
@Mock | ||
private ModelMapper mapper; | ||
@InjectMocks | ||
private AddressServiceImpl addressService; | ||
|
||
@Test | ||
void getAddressForOrderTest() { | ||
Long orderId = 1L; | ||
OrderAddress orderAddress = ModelUtils.getOrderAddress1(); | ||
|
||
when(orderAddressRepository.findByOrderId(orderId)).thenReturn(Optional.of(orderAddress)); | ||
when(mapper.map(orderAddress, UpdateAddressDto.class)).thenReturn(ModelUtils.getUpdateAddressDto()); | ||
|
||
var result = addressService.getAddressForOrder(orderId); | ||
|
||
assertEquals(orderId, result.getOrderId()); | ||
assertEquals(orderAddress.getId(), result.getOrderAddressExportDetails().getId()); | ||
|
||
verify(orderAddressRepository).findByOrderId(orderId); | ||
} | ||
|
||
@Test | ||
void getNotExistingAddressForOrderTest() { | ||
Long orderId = -1L; | ||
|
||
when(orderAddressRepository.findByOrderId(orderId)).thenReturn(Optional.empty()); | ||
|
||
assertThrows(NotFoundException.class, () -> addressService.getAddressForOrder(orderId)); | ||
|
||
verify(orderAddressRepository).findByOrderId(orderId); | ||
} | ||
} |