configure test profiles

This commit is contained in:
DOHA 2016-10-23 12:19:55 +02:00
parent 8cdc680e6f
commit bc9b170e89
6 changed files with 198 additions and 75 deletions

View File

@ -11,6 +11,7 @@
</parent> </parent>
<properties> <properties>
<cxf.version>3.1.6</cxf.version> <cxf.version>3.1.6</cxf.version>
<surefire.version>2.19.1</surefire.version>
</properties> </properties>
<build> <build>
<plugins> <plugins>
@ -26,7 +27,7 @@
<version>2.19.1</version> <version>2.19.1</version>
<configuration> <configuration>
<excludes> <excludes>
<exclude>**/StudentTest.java</exclude> <exclude>**/*LiveTest.java</exclude>
</excludes> </excludes>
</configuration> </configuration>
</plugin> </plugin>
@ -44,4 +45,66 @@
<version>${cxf.version}</version> <version>${cxf.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>
<profiles>
<profile>
<id>live</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.4.19</version>
<configuration>
<container>
<containerId>tomcat8x</containerId>
<type>embedded</type>
</container>
<configuration>
<properties>
<cargo.hostname>localhost</cargo.hostname>
<cargo.servlet.port>8082</cargo.servlet.port>
</properties>
</configuration>
</configuration>
<executions>
<execution>
<id>start-server</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-server</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>none</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project> </project>

View File

@ -2,20 +2,16 @@ package com.baeldung.cxf.introduction;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import java.util.Map; import java.util.Map;
import javax.xml.namespace.QName; import javax.xml.namespace.QName;
import javax.xml.ws.Service; import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding; import javax.xml.ws.soap.SOAPBinding;
import com.baeldung.cxf.introduction.Baeldung; import org.junit.Before;
import com.baeldung.cxf.introduction.Student; import org.junit.Test;
import com.baeldung.cxf.introduction.StudentImpl;
public class StudentTest { public class StudentLiveTest {
private static QName SERVICE_NAME = new QName("http://introduction.cxf.baeldung.com/", "Baeldung"); private static QName SERVICE_NAME = new QName("http://introduction.cxf.baeldung.com/", "Baeldung");
private static QName PORT_NAME = new QName("http://introduction.cxf.baeldung.com/", "BaeldungPort"); private static QName PORT_NAME = new QName("http://introduction.cxf.baeldung.com/", "BaeldungPort");
@ -25,7 +21,7 @@ public class StudentTest {
{ {
service = Service.create(SERVICE_NAME); service = Service.create(SERVICE_NAME);
String endpointAddress = "http://localhost:8080/baeldung"; final String endpointAddress = "http://localhost:8082/cxf-introduction/baeldung";
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress); service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
} }
@ -37,28 +33,28 @@ public class StudentTest {
@Test @Test
public void whenUsingHelloMethod_thenCorrect() { public void whenUsingHelloMethod_thenCorrect() {
String endpointResponse = baeldungProxy.hello("Baeldung"); final String endpointResponse = baeldungProxy.hello("Baeldung");
String localResponse = baeldungImpl.hello("Baeldung"); final String localResponse = baeldungImpl.hello("Baeldung");
assertEquals(localResponse, endpointResponse); assertEquals(localResponse, endpointResponse);
} }
@Test @Test
public void whenUsingHelloStudentMethod_thenCorrect() { public void whenUsingHelloStudentMethod_thenCorrect() {
Student student = new StudentImpl("John Doe"); final Student student = new StudentImpl("John Doe");
String endpointResponse = baeldungProxy.helloStudent(student); final String endpointResponse = baeldungProxy.helloStudent(student);
String localResponse = baeldungImpl.helloStudent(student); final String localResponse = baeldungImpl.helloStudent(student);
assertEquals(localResponse, endpointResponse); assertEquals(localResponse, endpointResponse);
} }
@Test @Test
public void usingGetStudentsMethod_thenCorrect() { public void usingGetStudentsMethod_thenCorrect() {
Student student1 = new StudentImpl("Adam"); final Student student1 = new StudentImpl("Adam");
baeldungProxy.helloStudent(student1); baeldungProxy.helloStudent(student1);
Student student2 = new StudentImpl("Eve"); final Student student2 = new StudentImpl("Eve");
baeldungProxy.helloStudent(student2); baeldungProxy.helloStudent(student2);
Map<Integer, Student> students = baeldungProxy.getStudents(); final Map<Integer, Student> students = baeldungProxy.getStudents();
assertEquals("Adam", students.get(1).getName()); assertEquals("Adam", students.get(1).getName());
assertEquals("Eve", students.get(2).getName()); assertEquals("Eve", students.get(2).getName());
} }

View File

@ -13,6 +13,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<cxf.version>3.1.7</cxf.version> <cxf.version>3.1.7</cxf.version>
<httpclient.version>4.5.2</httpclient.version> <httpclient.version>4.5.2</httpclient.version>
<surefire.version>2.19.1</surefire.version>
</properties> </properties>
<build> <build>
<plugins> <plugins>
@ -28,7 +29,7 @@
<version>2.19.1</version> <version>2.19.1</version>
<configuration> <configuration>
<excludes> <excludes>
<exclude>**/ServiceTest</exclude> <exclude>**/*LiveTest.java</exclude>
</excludes> </excludes>
</configuration> </configuration>
</plugin> </plugin>
@ -51,4 +52,66 @@
<version>${httpclient.version}</version> <version>${httpclient.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>
<profiles>
<profile>
<id>live</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.4.19</version>
<configuration>
<container>
<containerId>tomcat8x</containerId>
<type>embedded</type>
</container>
<configuration>
<properties>
<cargo.hostname>localhost</cargo.hostname>
<cargo.servlet.port>8082</cargo.servlet.port>
</properties>
</configuration>
</configuration>
<executions>
<execution>
<id>start-server</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-server</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>none</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project> </project>

View File

@ -1,5 +1,14 @@
package com.baeldung.cxf.jaxrs.implementation; package com.baeldung.cxf.jaxrs.implementation;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import javax.xml.bind.JAXB;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
@ -11,16 +20,8 @@ import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import javax.xml.bind.JAXB; public class ServiceLiveTest {
import java.io.IOException; private static final String BASE_URL = "http://localhost:8082/baeldung/courses/";
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import static org.junit.Assert.assertEquals;
public class ServiceTest {
private static final String BASE_URL = "http://localhost:8080/baeldung/courses/";
private static CloseableHttpClient client; private static CloseableHttpClient client;
@BeforeClass @BeforeClass
@ -35,95 +36,95 @@ public class ServiceTest {
@Test @Test
public void whenUpdateNonExistentCourse_thenReceiveNotFoundResponse() throws IOException { public void whenUpdateNonExistentCourse_thenReceiveNotFoundResponse() throws IOException {
HttpPut httpPut = new HttpPut(BASE_URL + "3"); final HttpPut httpPut = new HttpPut(BASE_URL + "3");
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("non_existent_course.xml"); final InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("non_existent_course.xml");
httpPut.setEntity(new InputStreamEntity(resourceStream)); httpPut.setEntity(new InputStreamEntity(resourceStream));
httpPut.setHeader("Content-Type", "text/xml"); httpPut.setHeader("Content-Type", "text/xml");
HttpResponse response = client.execute(httpPut); final HttpResponse response = client.execute(httpPut);
assertEquals(404, response.getStatusLine().getStatusCode()); assertEquals(404, response.getStatusLine().getStatusCode());
} }
@Test @Test
public void whenUpdateUnchangedCourse_thenReceiveNotModifiedResponse() throws IOException { public void whenUpdateUnchangedCourse_thenReceiveNotModifiedResponse() throws IOException {
HttpPut httpPut = new HttpPut(BASE_URL + "1"); final HttpPut httpPut = new HttpPut(BASE_URL + "1");
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("unchanged_course.xml"); final InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("unchanged_course.xml");
httpPut.setEntity(new InputStreamEntity(resourceStream)); httpPut.setEntity(new InputStreamEntity(resourceStream));
httpPut.setHeader("Content-Type", "text/xml"); httpPut.setHeader("Content-Type", "text/xml");
HttpResponse response = client.execute(httpPut); final HttpResponse response = client.execute(httpPut);
assertEquals(304, response.getStatusLine().getStatusCode()); assertEquals(304, response.getStatusLine().getStatusCode());
} }
@Test @Test
public void whenUpdateValidCourse_thenReceiveOKResponse() throws IOException { public void whenUpdateValidCourse_thenReceiveOKResponse() throws IOException {
HttpPut httpPut = new HttpPut(BASE_URL + "2"); final HttpPut httpPut = new HttpPut(BASE_URL + "2");
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("changed_course.xml"); final InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("changed_course.xml");
httpPut.setEntity(new InputStreamEntity(resourceStream)); httpPut.setEntity(new InputStreamEntity(resourceStream));
httpPut.setHeader("Content-Type", "text/xml"); httpPut.setHeader("Content-Type", "text/xml");
HttpResponse response = client.execute(httpPut); final HttpResponse response = client.execute(httpPut);
assertEquals(200, response.getStatusLine().getStatusCode()); assertEquals(200, response.getStatusLine().getStatusCode());
Course course = getCourse(2); final Course course = getCourse(2);
assertEquals(2, course.getId()); assertEquals(2, course.getId());
assertEquals("Apache CXF Support for RESTful", course.getName()); assertEquals("Apache CXF Support for RESTful", course.getName());
} }
@Test @Test
public void whenCreateConflictStudent_thenReceiveConflictResponse() throws IOException { public void whenCreateConflictStudent_thenReceiveConflictResponse() throws IOException {
HttpPost httpPost = new HttpPost(BASE_URL + "1/students"); final HttpPost httpPost = new HttpPost(BASE_URL + "1/students");
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("conflict_student.xml"); final InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("conflict_student.xml");
httpPost.setEntity(new InputStreamEntity(resourceStream)); httpPost.setEntity(new InputStreamEntity(resourceStream));
httpPost.setHeader("Content-Type", "text/xml"); httpPost.setHeader("Content-Type", "text/xml");
HttpResponse response = client.execute(httpPost); final HttpResponse response = client.execute(httpPost);
assertEquals(409, response.getStatusLine().getStatusCode()); assertEquals(409, response.getStatusLine().getStatusCode());
} }
@Test @Test
public void whenCreateValidStudent_thenReceiveOKResponse() throws IOException { public void whenCreateValidStudent_thenReceiveOKResponse() throws IOException {
HttpPost httpPost = new HttpPost(BASE_URL + "2/students"); final HttpPost httpPost = new HttpPost(BASE_URL + "2/students");
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("created_student.xml"); final InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("created_student.xml");
httpPost.setEntity(new InputStreamEntity(resourceStream)); httpPost.setEntity(new InputStreamEntity(resourceStream));
httpPost.setHeader("Content-Type", "text/xml"); httpPost.setHeader("Content-Type", "text/xml");
HttpResponse response = client.execute(httpPost); final HttpResponse response = client.execute(httpPost);
assertEquals(200, response.getStatusLine().getStatusCode()); assertEquals(200, response.getStatusLine().getStatusCode());
Student student = getStudent(2, 3); final Student student = getStudent(2, 3);
assertEquals(3, student.getId()); assertEquals(3, student.getId());
assertEquals("Student C", student.getName()); assertEquals("Student C", student.getName());
} }
@Test @Test
public void whenDeleteInvalidStudent_thenReceiveNotFoundResponse() throws IOException { public void whenDeleteInvalidStudent_thenReceiveNotFoundResponse() throws IOException {
HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/3"); final HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/3");
HttpResponse response = client.execute(httpDelete); final HttpResponse response = client.execute(httpDelete);
assertEquals(404, response.getStatusLine().getStatusCode()); assertEquals(404, response.getStatusLine().getStatusCode());
} }
@Test @Test
public void whenDeleteValidStudent_thenReceiveOKResponse() throws IOException { public void whenDeleteValidStudent_thenReceiveOKResponse() throws IOException {
HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/1"); final HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/1");
HttpResponse response = client.execute(httpDelete); final HttpResponse response = client.execute(httpDelete);
assertEquals(200, response.getStatusLine().getStatusCode()); assertEquals(200, response.getStatusLine().getStatusCode());
Course course = getCourse(1); final Course course = getCourse(1);
assertEquals(1, course.getStudents().size()); assertEquals(1, course.getStudents().size());
assertEquals(2, course.getStudents().get(0).getId()); assertEquals(2, course.getStudents().get(0).getId());
assertEquals("Student B", course.getStudents().get(0).getName()); assertEquals("Student B", course.getStudents().get(0).getName());
} }
private Course getCourse(int courseOrder) throws IOException { private Course getCourse(int courseOrder) throws IOException {
URL url = new URL(BASE_URL + courseOrder); final URL url = new URL(BASE_URL + courseOrder);
InputStream input = url.openStream(); final InputStream input = url.openStream();
return JAXB.unmarshal(new InputStreamReader(input), Course.class); return JAXB.unmarshal(new InputStreamReader(input), Course.class);
} }
private Student getStudent(int courseOrder, int studentOrder) throws IOException { private Student getStudent(int courseOrder, int studentOrder) throws IOException {
URL url = new URL(BASE_URL + courseOrder + "/students/" + studentOrder); final URL url = new URL(BASE_URL + courseOrder + "/students/" + studentOrder);
InputStream input = url.openStream(); final InputStream input = url.openStream();
return JAXB.unmarshal(new InputStreamReader(input), Student.class); return JAXB.unmarshal(new InputStreamReader(input), Student.class);
} }
} }

View File

@ -51,7 +51,7 @@
<version>${surefire.version}</version> <version>${surefire.version}</version>
<configuration> <configuration>
<excludes> <excludes>
<exclude>StudentTest.java</exclude> <exclude>**/*LiveTest.java</exclude>
</excludes> </excludes>
</configuration> </configuration>
</plugin> </plugin>
@ -60,7 +60,7 @@
<profiles> <profiles>
<profile> <profile>
<id>integration</id> <id>live</id>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>

View File

@ -6,7 +6,7 @@ import org.junit.Test;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class StudentTest { public class StudentLiveTest {
private ApplicationContext context = new AnnotationConfigApplicationContext(ClientConfiguration.class); private ApplicationContext context = new AnnotationConfigApplicationContext(ClientConfiguration.class);
private Baeldung baeldungProxy = (Baeldung) context.getBean("client"); private Baeldung baeldungProxy = (Baeldung) context.getBean("client");