Skip to content

Commit c77a992

Browse files
author
Grzegorz Piwowarek
committed
Refactor spring-protobuf
1 parent 85ad446 commit c77a992

6 files changed

Lines changed: 61 additions & 38 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<module>spring-mvc-no-xml</module>
5151
<module>spring-mvc-xml</module>
5252
<module>spring-openid</module>
53+
<module>spring-protobuf</module>
5354
<module>spring-quartz</module>
5455
<module>spring-rest</module>
5556

‎spring-protobuf/pom.xml‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,19 @@
4040
<version>4.5.2</version>
4141
</dependency>
4242
</dependencies>
43+
44+
<build>
45+
<plugins>
46+
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-compiler-plugin</artifactId>
50+
<version>3.3</version>
51+
<configuration>
52+
<source>1.8</source>
53+
<target>1.8</target>
54+
</configuration>
55+
</plugin>
56+
</plugins>
57+
</build>
4358
</project>

‎spring-protobuf/src/main/java/com/baeldung/protobuf/Application.java‎

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
package com.baeldung.protobuf;
22

3-
import java.util.ArrayList;
4-
import java.util.Arrays;
5-
import java.util.HashMap;
6-
import java.util.List;
7-
import java.util.Map;
8-
3+
import com.baeldung.protobuf.BaeldungTraining.Course;
4+
import com.baeldung.protobuf.BaeldungTraining.Student;
5+
import com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber;
6+
import com.baeldung.protobuf.BaeldungTraining.Student.PhoneType;
97
import org.springframework.boot.autoconfigure.SpringBootApplication;
108
import org.springframework.context.annotation.Bean;
119
import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter;
1210
import org.springframework.web.client.RestTemplate;
1311

14-
import com.baeldung.protobuf.BaeldungTraining.Course;
15-
import com.baeldung.protobuf.BaeldungTraining.Student;
16-
import com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber;
17-
import com.baeldung.protobuf.BaeldungTraining.Student.PhoneType;
12+
import java.util.ArrayList;
13+
import java.util.Arrays;
14+
import java.util.HashMap;
15+
import java.util.List;
16+
import java.util.Map;
1817

1918
@SpringBootApplication
2019
public class Application {
20+
2121
@Bean
2222
RestTemplate restTemplate(ProtobufHttpMessageConverter hmc) {
2323
return new RestTemplate(Arrays.asList(hmc));
@@ -29,21 +29,28 @@ ProtobufHttpMessageConverter protobufHttpMessageConverter() {
2929
}
3030

3131
@Bean
32-
public CourseRepository createDummyCourses() {
33-
Map<Integer, Course> dummy = new HashMap<>();
34-
35-
Course course1 = Course.newBuilder().setId(1).setCourseName("REST with Spring").addAllStudent(createDummyStudents()).build();
36-
Course course2 = Course.newBuilder().setId(2).setCourseName("Learn Spring Security").addAllStudent(new ArrayList<Student>()).build();
37-
38-
dummy.put(course1.getId(), course1);
39-
dummy.put(course2.getId(), course2);
40-
41-
return new CourseRepository(dummy);
42-
}
32+
public CourseRepository createStubCourses() {
33+
Map<Integer, Course> courses = new HashMap<>();
34+
35+
Course course1 = Course.newBuilder()
36+
.setId(1)
37+
.setCourseName("REST with Spring")
38+
.addAllStudent(createStubStudents())
39+
.build();
4340

44-
private List<Student> createDummyStudents() {
45-
List<Student> studentList = new ArrayList<>();
41+
Course course2 = Course.newBuilder()
42+
.setId(2)
43+
.setCourseName("Learn Spring Security")
44+
.addAllStudent(new ArrayList<>())
45+
.build();
46+
47+
courses.put(course1.getId(), course1);
48+
courses.put(course2.getId(), course2);
49+
50+
return new CourseRepository(courses);
51+
}
4652

53+
private List<Student> createStubStudents() {
4754
PhoneNumber phone1 = createPhone("123456", PhoneType.MOBILE);
4855
Student student1 = createStudent(1, "John", "Doe", "john.doe@baeldung.com", Arrays.asList(phone1));
4956

@@ -54,8 +61,7 @@ private List<Student> createDummyStudents() {
5461
PhoneNumber phone3_2 = createPhone("456789", PhoneType.LANDLINE);
5562
Student student3 = createStudent(3, "Jane", "Doe", "jane.doe@baeldung.com", Arrays.asList(phone3_1, phone3_2));
5663

57-
studentList.addAll(Arrays.asList(student1, student2, student3));
58-
return studentList;
64+
return Arrays.asList(student1, student2, student3);
5965
}
6066

6167
private Student createStudent(int id, String firstName, String lastName, String email, List<PhoneNumber> phones) {

‎spring-protobuf/src/main/java/com/baeldung/protobuf/CourseController.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
@RestController
1111
public class CourseController {
12+
1213
@Autowired
1314
CourseRepository courseRepo;
1415

‎spring-protobuf/src/main/java/com/baeldung/protobuf/CourseRepository.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.baeldung.protobuf;
22

3-
import java.util.Map;
4-
53
import com.baeldung.protobuf.BaeldungTraining.Course;
64

5+
import java.util.Map;
6+
77
public class CourseRepository {
8-
Map<Integer, Course> courses;
8+
9+
private final Map<Integer, Course> courses;
910

1011
public CourseRepository (Map<Integer, Course> courses) {
1112
this.courses = courses;

‎spring-protobuf/src/test/java/com/baeldung/protobuf/ApplicationTest.java‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
package com.baeldung.protobuf;
22

3-
import java.io.IOException;
4-
import java.io.InputStream;
5-
3+
import com.baeldung.protobuf.BaeldungTraining.Course;
4+
import com.googlecode.protobuf.format.JsonFormat;
65
import org.apache.http.HttpResponse;
76
import org.apache.http.client.methods.HttpGet;
87
import org.apache.http.impl.client.CloseableHttpClient;
98
import org.apache.http.impl.client.HttpClients;
10-
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
1111
import org.springframework.beans.factory.annotation.Autowired;
1212
import org.springframework.boot.test.SpringApplicationConfiguration;
1313
import org.springframework.boot.test.WebIntegrationTest;
1414
import org.springframework.http.ResponseEntity;
1515
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
1616
import org.springframework.web.client.RestTemplate;
1717

18-
import org.junit.Test;
19-
import org.junit.runner.RunWith;
20-
21-
import com.baeldung.protobuf.BaeldungTraining.Course;
22-
import com.googlecode.protobuf.format.JsonFormat;
18+
import java.io.IOException;
19+
import java.io.InputStream;
2320

24-
import static org.junit.Assert.assertThat;
2521
import static org.hamcrest.CoreMatchers.containsString;
22+
import static org.junit.Assert.assertThat;
2623

2724
@RunWith(SpringJUnit4ClassRunner.class)
2825
@SpringApplicationConfiguration(classes = Application.class)
2926
@WebIntegrationTest
3027
public class ApplicationTest {
28+
3129
private static final String COURSE1_URL = "http://localhost:8080/courses/1";
30+
3231
@Autowired
3332
private RestTemplate restTemplate;
3433

0 commit comments

Comments
 (0)