Last active
September 16, 2019 15:15
-
-
Save jorgebo10/14ebb72fa2245bae545e6a43a84e774a to your computer and use it in GitHub Desktop.
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
@Service | |
@ConfigurationProperties | |
@PropertySource(value = { "classpath:passenger-type-codes.yml" }, factory = YamlPropertySourceFactory.class) | |
public class PassengerTypeService { | |
private Map<String, String> codes; | |
public PassengerType getPassengerType(String code) { | |
String value = codes.get(code) == null ? "ADULT" : codes.get(code); | |
return PassengerType.valueOf(value); | |
} | |
public Map<String, String> getCodes() { | |
return codes; | |
} | |
public void setCodes(Map<String, String> codes) { | |
this.codes = codes; | |
} | |
} |
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
@Service | |
public class AirportGateway { | |
private Connector connector; | |
public Airport findByCode(String code) { | |
Airport airport = connector.getByCode(code) | |
if (airport != null) { | |
return airport; | |
} else { | |
return null; | |
} | |
} | |
} |
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
final class TicketDetailsByNumberMap { | |
private Map<String, Optional<TicketDetailsType>> ticketDetailsByNumber; | |
TicketDetailsByNumberMap(List<TicketDetailsType> ticketDetails) { | |
lastUpdatedTicketDetailsByTicketNumber(ticketDetails); | |
} | |
private void lastUpdatedTicketDetailsByTicketNumber(List<TicketDetailsType> ticketDetailsTypes) { | |
this.ticketDetailsByNumber = ticketDetailsTypes.stream() | |
.collect( | |
groupingBy(t -> getETicketNumber(t.getTicketNumber()), | |
maxBy((t1, t2) -> ((TicketDetailsType) t1).getTimestamp().compare(((TicketDetailsType) t2).getTimestamp())))); | |
} | |
TicketDetailsType get(String ticketNumber) { | |
return this.ticketDetailsByNumber.get(getETicketNumber(ticketNumber)).orElseThrow(IllegalArgumentException::new); | |
} | |
private static String getETicketNumber(String ticketNumber) { | |
return ticketNumber.substring(0, 13); | |
} | |
} |
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
Component | |
class TicketMapper { | |
private PassengerTypeService passengerTypeService; | |
private AirportGateway airpotGateway; | |
List<Ticket> map(List<PassengerPNRB> passengerPNRBs, TicketDetailsByNumberMap ticketDetailsByNumberMap) { | |
return passengerPNRBs.stream() | |
.filter(TicketMapperHelper::hasTicket) | |
.map(passengerPNRB -> { | |
TicketDetailsType ticketDetailsType = ticketDetailsByNumberMap.get(TicketMapperHelper.getTicketNumber(passengerPNRB)); | |
return new TicketBuilder() | |
.setPassenger(passengerTypeService.getPassengerType(passengerPNRB.getPassengerType()), | |
passengerPNRB.getFirstName(), | |
passengerPNRB.getLastName()) | |
.setOrigin(airportGateway.findByCode(ticketDetailsType.getEmissionLocation())) | |
.setTicketNumber(ticketDetailsType.getTicketNumber()) | |
.setEmissionDate(ticketDetailsType.getTimestamp()) | |
.build(); | |
}) | |
.collect(Collectors.toList()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would like to write some unit test for TicketMapper, although it is not part of my domain logic, it is important to verify the mapping logic. As this mapper has some out-of-process dependencies, the first one is reads values from filesystem and the second one does an API call to a 3er party system, i was wondering whether the use of mocks and stubs in this unit test is a good fit or maybe i can use some humble object to separate dependencies from complex logic as you mention so that this complex logic does not depends on o-o-p dependencies. I thought about using humble pattern, i should fetch all airport first, pass them as a parameter and do some search. The thing is that aiport list can become a very large list.