Refactor spring-protobuf
This commit is contained in:
parent
85ad446ccf
commit
c77a992a52
1
pom.xml
1
pom.xml
@ -50,6 +50,7 @@
|
|||||||
<module>spring-mvc-no-xml</module>
|
<module>spring-mvc-no-xml</module>
|
||||||
<module>spring-mvc-xml</module>
|
<module>spring-mvc-xml</module>
|
||||||
<module>spring-openid</module>
|
<module>spring-openid</module>
|
||||||
|
<module>spring-protobuf</module>
|
||||||
<module>spring-quartz</module>
|
<module>spring-quartz</module>
|
||||||
<module>spring-rest</module>
|
<module>spring-rest</module>
|
||||||
|
|
||||||
|
@ -40,4 +40,19 @@
|
|||||||
<version>4.5.2</version>
|
<version>4.5.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</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>
|
</project>
|
||||||
|
@ -1,23 +1,23 @@
|
|||||||
package com.baeldung.protobuf;
|
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.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
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
|
@SpringBootApplication
|
||||||
public class Application {
|
public class Application {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
RestTemplate restTemplate(ProtobufHttpMessageConverter hmc) {
|
RestTemplate restTemplate(ProtobufHttpMessageConverter hmc) {
|
||||||
return new RestTemplate(Arrays.asList(hmc));
|
return new RestTemplate(Arrays.asList(hmc));
|
||||||
@ -29,21 +29,28 @@ public class Application {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public CourseRepository createDummyCourses() {
|
public CourseRepository createStubCourses() {
|
||||||
Map<Integer, Course> dummy = new HashMap<>();
|
Map<Integer, Course> courses = new HashMap<>();
|
||||||
|
|
||||||
Course course1 = Course.newBuilder().setId(1).setCourseName("REST with Spring").addAllStudent(createDummyStudents()).build();
|
Course course1 = Course.newBuilder()
|
||||||
Course course2 = Course.newBuilder().setId(2).setCourseName("Learn Spring Security").addAllStudent(new ArrayList<Student>()).build();
|
.setId(1)
|
||||||
|
.setCourseName("REST with Spring")
|
||||||
dummy.put(course1.getId(), course1);
|
.addAllStudent(createStubStudents())
|
||||||
dummy.put(course2.getId(), course2);
|
.build();
|
||||||
|
|
||||||
return new CourseRepository(dummy);
|
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() {
|
private List<Student> createStubStudents() {
|
||||||
List<Student> studentList = new ArrayList<>();
|
|
||||||
|
|
||||||
PhoneNumber phone1 = createPhone("123456", PhoneType.MOBILE);
|
PhoneNumber phone1 = createPhone("123456", PhoneType.MOBILE);
|
||||||
Student student1 = createStudent(1, "John", "Doe", "john.doe@baeldung.com", Arrays.asList(phone1));
|
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);
|
PhoneNumber phone3_2 = createPhone("456789", PhoneType.LANDLINE);
|
||||||
Student student3 = createStudent(3, "Jane", "Doe", "jane.doe@baeldung.com", Arrays.asList(phone3_1, phone3_2));
|
Student student3 = createStudent(3, "Jane", "Doe", "jane.doe@baeldung.com", Arrays.asList(phone3_1, phone3_2));
|
||||||
|
|
||||||
studentList.addAll(Arrays.asList(student1, student2, student3));
|
return Arrays.asList(student1, student2, student3);
|
||||||
return studentList;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Student createStudent(int id, String firstName, String lastName, String email, List<PhoneNumber> phones) {
|
private Student createStudent(int id, String firstName, String lastName, String email, List<PhoneNumber> phones) {
|
||||||
|
@ -9,6 +9,7 @@ import com.baeldung.protobuf.BaeldungTraining.Course;
|
|||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class CourseController {
|
public class CourseController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
CourseRepository courseRepo;
|
CourseRepository courseRepo;
|
||||||
|
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
package com.baeldung.protobuf;
|
package com.baeldung.protobuf;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import com.baeldung.protobuf.BaeldungTraining.Course;
|
import com.baeldung.protobuf.BaeldungTraining.Course;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class CourseRepository {
|
public class CourseRepository {
|
||||||
Map<Integer, Course> courses;
|
|
||||||
|
private final Map<Integer, Course> courses;
|
||||||
|
|
||||||
public CourseRepository (Map<Integer, Course> courses) {
|
public CourseRepository (Map<Integer, Course> courses) {
|
||||||
this.courses = courses;
|
this.courses = courses;
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
package com.baeldung.protobuf;
|
package com.baeldung.protobuf;
|
||||||
|
|
||||||
import java.io.IOException;
|
import com.baeldung.protobuf.BaeldungTraining.Course;
|
||||||
import java.io.InputStream;
|
import com.googlecode.protobuf.format.JsonFormat;
|
||||||
|
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClients;
|
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.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||||
import org.springframework.boot.test.WebIntegrationTest;
|
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.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import org.junit.Test;
|
import java.io.IOException;
|
||||||
import org.junit.runner.RunWith;
|
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.hamcrest.CoreMatchers.containsString;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringApplicationConfiguration(classes = Application.class)
|
@SpringApplicationConfiguration(classes = Application.class)
|
||||||
@WebIntegrationTest
|
@WebIntegrationTest
|
||||||
public class ApplicationTest {
|
public class ApplicationTest {
|
||||||
|
|
||||||
private static final String COURSE1_URL = "http://localhost:8080/courses/1";
|
private static final String COURSE1_URL = "http://localhost:8080/courses/1";
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private RestTemplate restTemplate;
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user