BAEL-1772: Argument Matchers (#4323)
This commit is contained in:
committed by
maibin
parent
be608ae4fb
commit
c3eaeeadde
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.app.api;
|
||||
|
||||
public class Flower {
|
||||
|
||||
private String name;
|
||||
private Integer petals;
|
||||
|
||||
public Flower(String name, Integer petals) {
|
||||
this.name = name;
|
||||
this.petals = petals;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getPetals() {
|
||||
return petals;
|
||||
}
|
||||
|
||||
public void setPetals(Integer petals) {
|
||||
this.petals = petals;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.app.api;
|
||||
|
||||
public class MessageApi {
|
||||
private String from;
|
||||
private String to;
|
||||
private String text;
|
||||
|
||||
public String getFrom() {
|
||||
return from;
|
||||
}
|
||||
|
||||
public void setFrom(String from) {
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
public String getTo() {
|
||||
return to;
|
||||
}
|
||||
|
||||
public void setTo(String to) {
|
||||
this.to = to;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.app.rest;
|
||||
|
||||
import com.baeldung.app.api.Flower;
|
||||
import com.baeldung.domain.service.FlowerService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/flowers")
|
||||
public class FlowerController {
|
||||
|
||||
@Autowired
|
||||
private FlowerService flowerService;
|
||||
|
||||
@PostMapping("/isAFlower")
|
||||
public String isAFlower (@RequestBody String flower) {
|
||||
return flowerService.analize(flower);
|
||||
}
|
||||
|
||||
@PostMapping("/isABigFlower")
|
||||
public Boolean isABigFlower (@RequestBody Flower flower) {
|
||||
return flowerService.isABigFlower(flower.getName(), flower.getPetals());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.app.rest;
|
||||
|
||||
import com.baeldung.app.api.MessageApi;
|
||||
import com.baeldung.domain.model.Message;
|
||||
import com.baeldung.domain.service.MessageService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/message")
|
||||
public class MessageController {
|
||||
|
||||
@Autowired
|
||||
private MessageService messageService;
|
||||
|
||||
@PostMapping
|
||||
public Message createMessage (@RequestBody MessageApi messageDTO) {
|
||||
Message message = new Message();
|
||||
message.setText(messageDTO.getText());
|
||||
message.setFrom(messageDTO.getFrom());
|
||||
message.setTo(messageDTO.getTo());
|
||||
message.setDate(Date.from(Instant.now()));
|
||||
message.setId(UUID.randomUUID());
|
||||
|
||||
return messageService.deliverMessage(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.domain.model;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Message {
|
||||
|
||||
private String from;
|
||||
private String to;
|
||||
private String text;
|
||||
private Date date;
|
||||
private UUID id;
|
||||
|
||||
public String getFrom() {
|
||||
return from;
|
||||
}
|
||||
|
||||
public void setFrom(String from) {
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
public String getTo() {
|
||||
return to;
|
||||
}
|
||||
|
||||
public void setTo(String to) {
|
||||
this.to = to;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(Date date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(UUID id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.domain.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class FlowerService {
|
||||
|
||||
private List<String> flowers = Arrays.asList("Poppy", "Ageratum", "Carnation", "Diascia", "Lantana");
|
||||
|
||||
public String analize(String name) {
|
||||
if(flowers.contains(name)) {
|
||||
return "flower";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isABigFlower(String name, int petals) {
|
||||
if(flowers.contains(name)) {
|
||||
if(petals > 10) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.domain.service;
|
||||
|
||||
import com.baeldung.domain.model.Message;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class MessageService {
|
||||
|
||||
public Message deliverMessage (Message message) {
|
||||
|
||||
return message;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.domain.util;
|
||||
|
||||
import com.baeldung.domain.model.Message;
|
||||
import org.mockito.ArgumentMatcher;
|
||||
|
||||
public class MessageMatcher extends ArgumentMatcher<Message> {
|
||||
|
||||
private Message left;
|
||||
|
||||
public MessageMatcher(Message message) {
|
||||
this.left = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Object object) {
|
||||
if (object instanceof Message) {
|
||||
Message right = (Message) object;
|
||||
return left.getFrom().equals(right.getFrom()) &&
|
||||
left.getTo().equals(right.getTo()) &&
|
||||
left.getText().equals(right.getText()) &&
|
||||
right.getDate() != null &&
|
||||
right.getId() != null;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user