Refactor spring-protobuf

This commit is contained in:
Grzegorz Piwowarek 2016-06-18 22:08:24 +02:00
parent 85ad446ccf
commit c77a992a52
6 changed files with 61 additions and 38 deletions

View File

@ -50,6 +50,7 @@
<module>spring-mvc-no-xml</module>
<module>spring-mvc-xml</module>
<module>spring-openid</module>
<module>spring-protobuf</module>
<module>spring-quartz</module>
<module>spring-rest</module>

View File

@ -40,4 +40,19 @@
<version>4.5.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,23 +1,23 @@
package com.baeldung.protobuf;
import com.baeldung.protobuf.BaeldungTraining.Course;
import com.baeldung.protobuf.BaeldungTraining.Student;
import com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber;
import com.baeldung.protobuf.BaeldungTraining.Student.PhoneType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import com.baeldung.protobuf.BaeldungTraining.Course;
import com.baeldung.protobuf.BaeldungTraining.Student;
import com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber;
import com.baeldung.protobuf.BaeldungTraining.Student.PhoneType;
@SpringBootApplication
public class Application {
@Bean
RestTemplate restTemplate(ProtobufHttpMessageConverter hmc) {
return new RestTemplate(Arrays.asList(hmc));
@ -29,21 +29,28 @@ public class Application {
}
@Bean
public CourseRepository createDummyCourses() {
Map<Integer, Course> dummy = new HashMap<>();
Course course1 = Course.newBuilder().setId(1).setCourseName("REST with Spring").addAllStudent(createDummyStudents()).build();
Course course2 = Course.newBuilder().setId(2).setCourseName("Learn Spring Security").addAllStudent(new ArrayList<Student>()).build();
dummy.put(course1.getId(), course1);
dummy.put(course2.getId(), course2);
return new CourseRepository(dummy);
public CourseRepository createStubCourses() {
Map<Integer, Course> courses = new HashMap<>();
Course course1 = Course.newBuilder()
.setId(1)
.setCourseName("REST with Spring")
.addAllStudent(createStubStudents())
.build();
Course course2 = Course.newBuilder()
.setId(2)
.setCourseName("Learn Spring Security")
.addAllStudent(new ArrayList<>())
.build();
courses.put(course1.getId(), course1);
courses.put(course2.getId(), course2);
return new CourseRepository(courses);
}
private List<Student> createDummyStudents() {
List<Student> studentList = new ArrayList<>();
private List<Student> createStubStudents() {
PhoneNumber phone1 = createPhone("123456", PhoneType.MOBILE);
Student student1 = createStudent(1, "John", "Doe", "john.doe@baeldung.com", Arrays.asList(phone1));
@ -54,8 +61,7 @@ public class Application {
PhoneNumber phone3_2 = createPhone("456789", PhoneType.LANDLINE);
Student student3 = createStudent(3, "Jane", "Doe", "jane.doe@baeldung.com", Arrays.asList(phone3_1, phone3_2));
studentList.addAll(Arrays.asList(student1, student2, student3));
return studentList;
return Arrays.asList(student1, student2, student3);
}
private Student createStudent(int id, String firstName, String lastName, String email, List<PhoneNumber> phones) {

View File

@ -9,6 +9,7 @@ import com.baeldung.protobuf.BaeldungTraining.Course;
@RestController
public class CourseController {
@Autowired
CourseRepository courseRepo;

View File

@ -1,11 +1,12 @@
package com.baeldung.protobuf;
import java.util.Map;
import com.baeldung.protobuf.BaeldungTraining.Course;
import java.util.Map;
public class CourseRepository {
Map<Integer, Course> courses;
private final Map<Integer, Course> courses;
public CourseRepository (Map<Integer, Course> courses) {
this.courses = courses;

View File

@ -1,13 +1,13 @@
package com.baeldung.protobuf;
import java.io.IOException;
import java.io.InputStream;
import com.baeldung.protobuf.BaeldungTraining.Course;
import com.googlecode.protobuf.format.JsonFormat;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
@ -15,20 +15,19 @@ import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.client.RestTemplate;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.IOException;
import java.io.InputStream;
import com.baeldung.protobuf.BaeldungTraining.Course;
import com.googlecode.protobuf.format.JsonFormat;
import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebIntegrationTest
public class ApplicationTest {
private static final String COURSE1_URL = "http://localhost:8080/courses/1";
@Autowired
private RestTemplate restTemplate;