-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Task1 - Implement service and acceptance tests
- Some AT refactor & fixes - User controller fix for kafka event sending - Changes on the PurchaseEntity - Changes on the service side listener for PurchasEvents
- Loading branch information
1 parent
5685b0e
commit dc4ff1d
Showing
17 changed files
with
195 additions
and
49 deletions.
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
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
30 changes: 30 additions & 0 deletions
30
acceptance_test/src/test/java/com/demo/acceptance/tests/util/KafkaEventDeserializer.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,30 @@ | ||
package com.demo.acceptance.tests.util; | ||
|
||
import java.io.IOException; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.demo.service.events.PurchaseEvent; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
@Component | ||
public class KafkaEventDeserializer { | ||
|
||
private static final TypeReference<PurchaseEvent> EVENT_TYPE_REFERENCE = new TypeReference<>() { | ||
}; | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
public PurchaseEvent deserializeJsonToPurchaseEvent(final String eventJsonAsString) { | ||
PurchaseEvent deserializedEvent; | ||
try { | ||
deserializedEvent = objectMapper.readValue(eventJsonAsString, EVENT_TYPE_REFERENCE); | ||
} catch (IOException exception) { | ||
throw new RuntimeException(exception); | ||
} | ||
return deserializedEvent; | ||
} | ||
} |
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
18 changes: 11 additions & 7 deletions
18
demo_svc/src/main/java/com/demo/service/events/PurchaseEvent.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 |
---|---|---|
@@ -1,21 +1,25 @@ | ||
package com.demo.service.events; | ||
|
||
import java.util.Map; | ||
import java.util.List; | ||
|
||
import com.demo.web.entity.ProductEntity; | ||
import com.demo.service.model.PurchaseDetail; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.ToString; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@ToString | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@EqualsAndHashCode | ||
public class PurchaseEvent { | ||
|
||
private final long eventId; | ||
private final long userId; | ||
private final Map<ProductEntity, Integer> productsWithQuantities; | ||
private final Double totalValue; | ||
private long eventId; | ||
private long userId; | ||
private List<PurchaseDetail> purchaseDetails; | ||
private Double totalValue; | ||
} |
15 changes: 15 additions & 0 deletions
15
demo_svc/src/main/java/com/demo/service/model/PurchaseDetail.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,15 @@ | ||
package com.demo.service.model; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.ToString; | ||
|
||
@Getter | ||
@ToString | ||
@NoArgsConstructor | ||
public class PurchaseDetail { | ||
private String productId; | ||
private String productName; | ||
private Double price; | ||
private int quantity; | ||
} |
26 changes: 0 additions & 26 deletions
26
demo_svc/src/main/java/com/demo/service/model/PurchaseModel.java
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
demo_svc/src/main/java/com/demo/service/service/PurchaseService.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,8 @@ | ||
package com.demo.service.service; | ||
|
||
import com.demo.web.entity.PurchaseEntity; | ||
|
||
public interface PurchaseService { | ||
|
||
PurchaseEntity savePurchase(PurchaseEntity purchase); | ||
} |
23 changes: 23 additions & 0 deletions
23
demo_svc/src/main/java/com/demo/service/service/impl/PurchaseServiceImpl.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,23 @@ | ||
package com.demo.service.service.impl; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.demo.service.service.PurchaseService; | ||
import com.demo.web.entity.PurchaseEntity; | ||
import com.demo.web.repository.PurchaseRepository; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Component | ||
public class PurchaseServiceImpl implements PurchaseService { | ||
|
||
@Autowired | ||
private PurchaseRepository purchaseRepository; | ||
|
||
@Override | ||
public PurchaseEntity savePurchase(final PurchaseEntity purchase) { | ||
log.info("Saving the purchase event content to the database."); | ||
return purchaseRepository.save(purchase); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
demo_svc/src/main/java/com/demo/service/service/kafka/KafkaEventListener.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 |
---|---|---|
@@ -1,22 +1,69 @@ | ||
package com.demo.service.service.kafka; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.CountDownLatch; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.kafka.annotation.KafkaListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.demo.service.events.PurchaseEvent; | ||
import com.demo.service.exception.ProductNotFoundException; | ||
import com.demo.service.exception.UserNotFoundException; | ||
import com.demo.service.service.ProductService; | ||
import com.demo.service.service.PurchaseService; | ||
import com.demo.service.service.UserService; | ||
import com.demo.web.entity.ProductEntity; | ||
import com.demo.web.entity.PurchaseEntity; | ||
import com.demo.web.entity.UserEntity; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Component | ||
public class KafkaEventListener { | ||
|
||
@Autowired | ||
private PurchaseService purchaseService; | ||
|
||
@Autowired | ||
private UserService userService; | ||
|
||
@Autowired | ||
private ProductService productService; | ||
|
||
private final CountDownLatch purchaseEventLatch = new CountDownLatch(1); | ||
|
||
@KafkaListener(topics = "${purchase.topic.name}", containerFactory = "purchaseEventKafkaListenerContainerFactory") | ||
public void purchaseEventListener(final PurchaseEvent purchaseEvent) { | ||
log.info("Received purchaseEvent message: {}", purchaseEvent); | ||
this.purchaseEventLatch.countDown(); | ||
|
||
final UserEntity userFromThePurchase = userService.findByUserId(purchaseEvent.getUserId()) | ||
.orElseThrow(() -> new UserNotFoundException(purchaseEvent.getUserId())); | ||
|
||
final List<ProductEntity> productsFromThePurchase = new ArrayList<>(); | ||
final Map<Long, Integer> productIdsWithQuantities = new HashMap<>(); | ||
|
||
purchaseEvent.getPurchaseDetails().forEach(details -> { | ||
final Long productId = Long.parseLong(details.getProductId()); | ||
productsFromThePurchase.add( | ||
productService.findByProductId(productId).orElseThrow(() -> new ProductNotFoundException(productId)) | ||
); | ||
|
||
productIdsWithQuantities.put(productId, details.getQuantity()); | ||
}); | ||
|
||
final PurchaseEntity receivedPurchase = PurchaseEntity.builder() | ||
.id(purchaseEvent.getEventId()) | ||
.user(userFromThePurchase) | ||
.productEntities(productsFromThePurchase) | ||
.productIdsWithQuantities(productIdsWithQuantities) | ||
.totalValue(purchaseEvent.getTotalValue()) | ||
.build(); | ||
|
||
purchaseService.savePurchase(receivedPurchase); | ||
} | ||
} |
Oops, something went wrong.