Skip to content

Commit ee00aa4

Browse files
authored
Merge branch 'master' into BAEL-1799
2 parents 2c2915e + c3eaeea commit ee00aa4

12 files changed

Lines changed: 329 additions & 2 deletions

File tree

‎spring-mockito/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<dependencies>
2222
<dependency>
2323
<groupId>org.springframework.boot</groupId>
24-
<artifactId>spring-boot-starter</artifactId>
24+
<artifactId>spring-boot-starter-web</artifactId>
2525
</dependency>
2626
<dependency>
2727
<groupId>org.mockito</groupId>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.app.api;
2+
3+
public class Flower {
4+
5+
private String name;
6+
private Integer petals;
7+
8+
public Flower(String name, Integer petals) {
9+
this.name = name;
10+
this.petals = petals;
11+
}
12+
13+
public String getName() {
14+
return name;
15+
}
16+
17+
public void setName(String name) {
18+
this.name = name;
19+
}
20+
21+
public Integer getPetals() {
22+
return petals;
23+
}
24+
25+
public void setPetals(Integer petals) {
26+
this.petals = petals;
27+
}
28+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.app.api;
2+
3+
public class MessageApi {
4+
private String from;
5+
private String to;
6+
private String text;
7+
8+
public String getFrom() {
9+
return from;
10+
}
11+
12+
public void setFrom(String from) {
13+
this.from = from;
14+
}
15+
16+
public String getTo() {
17+
return to;
18+
}
19+
20+
public void setTo(String to) {
21+
this.to = to;
22+
}
23+
24+
public String getText() {
25+
return text;
26+
}
27+
28+
public void setText(String text) {
29+
this.text = text;
30+
}
31+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.app.rest;
2+
3+
import com.baeldung.app.api.Flower;
4+
import com.baeldung.domain.service.FlowerService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.web.bind.annotation.PostMapping;
8+
import org.springframework.web.bind.annotation.RequestBody;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
11+
@Controller
12+
@RequestMapping("/flowers")
13+
public class FlowerController {
14+
15+
@Autowired
16+
private FlowerService flowerService;
17+
18+
@PostMapping("/isAFlower")
19+
public String isAFlower (@RequestBody String flower) {
20+
return flowerService.analize(flower);
21+
}
22+
23+
@PostMapping("/isABigFlower")
24+
public Boolean isABigFlower (@RequestBody Flower flower) {
25+
return flowerService.isABigFlower(flower.getName(), flower.getPetals());
26+
}
27+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.app.rest;
2+
3+
import com.baeldung.app.api.MessageApi;
4+
import com.baeldung.domain.model.Message;
5+
import com.baeldung.domain.service.MessageService;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.web.bind.annotation.PostMapping;
9+
import org.springframework.web.bind.annotation.RequestBody;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
12+
import java.time.Instant;
13+
import java.util.Date;
14+
import java.util.UUID;
15+
16+
@Controller
17+
@RequestMapping("/message")
18+
public class MessageController {
19+
20+
@Autowired
21+
private MessageService messageService;
22+
23+
@PostMapping
24+
public Message createMessage (@RequestBody MessageApi messageDTO) {
25+
Message message = new Message();
26+
message.setText(messageDTO.getText());
27+
message.setFrom(messageDTO.getFrom());
28+
message.setTo(messageDTO.getTo());
29+
message.setDate(Date.from(Instant.now()));
30+
message.setId(UUID.randomUUID());
31+
32+
return messageService.deliverMessage(message);
33+
}
34+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.domain.model;
2+
3+
import java.util.Date;
4+
import java.util.UUID;
5+
6+
public class Message {
7+
8+
private String from;
9+
private String to;
10+
private String text;
11+
private Date date;
12+
private UUID id;
13+
14+
public String getFrom() {
15+
return from;
16+
}
17+
18+
public void setFrom(String from) {
19+
this.from = from;
20+
}
21+
22+
public String getTo() {
23+
return to;
24+
}
25+
26+
public void setTo(String to) {
27+
this.to = to;
28+
}
29+
30+
public String getText() {
31+
return text;
32+
}
33+
34+
public void setText(String text) {
35+
this.text = text;
36+
}
37+
38+
public Date getDate() {
39+
return date;
40+
}
41+
42+
public void setDate(Date date) {
43+
this.date = date;
44+
}
45+
46+
public UUID getId() {
47+
return id;
48+
}
49+
50+
public void setId(UUID id) {
51+
this.id = id;
52+
}
53+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.domain.service;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
@Service
9+
public class FlowerService {
10+
11+
private List<String> flowers = Arrays.asList("Poppy", "Ageratum", "Carnation", "Diascia", "Lantana");
12+
13+
public String analize(String name) {
14+
if(flowers.contains(name)) {
15+
return "flower";
16+
}
17+
return null;
18+
}
19+
20+
public boolean isABigFlower(String name, int petals) {
21+
if(flowers.contains(name)) {
22+
if(petals > 10) {
23+
return true;
24+
}
25+
}
26+
return false;
27+
}
28+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.domain.service;
2+
3+
import com.baeldung.domain.model.Message;
4+
import org.springframework.stereotype.Service;
5+
6+
@Service
7+
public class MessageService {
8+
9+
public Message deliverMessage (Message message) {
10+
11+
return message;
12+
}
13+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.domain.util;
2+
3+
import com.baeldung.domain.model.Message;
4+
import org.mockito.ArgumentMatcher;
5+
6+
public class MessageMatcher extends ArgumentMatcher<Message> {
7+
8+
private Message left;
9+
10+
public MessageMatcher(Message message) {
11+
this.left = message;
12+
}
13+
14+
@Override
15+
public boolean matches(Object object) {
16+
if (object instanceof Message) {
17+
Message right = (Message) object;
18+
return left.getFrom().equals(right.getFrom()) &&
19+
left.getTo().equals(right.getTo()) &&
20+
left.getText().equals(right.getText()) &&
21+
right.getDate() != null &&
22+
right.getId() != null;
23+
}
24+
25+
return false;
26+
}
27+
}

spring-mockito/src/test/java/com/baeldung/UserServiceIntegrationTest.java renamed to spring-mockito/src/test/java/com/baeldung/UserServiceIntegrationUnitTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
@ActiveProfiles("test")
1313
@RunWith(SpringJUnit4ClassRunner.class)
1414
@SpringBootTest(classes = MocksApplication.class)
15-
public class UserServiceIntegrationTest {
15+
public class UserServiceIntegrationUnitTest {
1616

1717
@Autowired
1818
private UserService userService;

0 commit comments

Comments
 (0)