From b6d47b00a35e9dfec74ee5e89ec34085616512b1 Mon Sep 17 00:00:00 2001 From: lor6 Date: Mon, 12 Jun 2017 11:20:42 +0300 Subject: [PATCH 1/4] junit 5 example (#2050) --- guest/junit5-example/.gitignore | 4 + guest/junit5-example/pom.xml | 68 ++++++++ .../main/java/com/stackify/daos/UserDAO.java | 141 ++++++++++++++++ .../main/java/com/stackify/models/User.java | 63 ++++++++ .../com/stackify/utils/ConnectionUtil.java | 38 +++++ .../com/stackify/utils/jdbc.properties | 4 + .../src/main/resources/log4j2.xml | 13 ++ .../stackify/test/DatabaseConnectionTest.java | 19 +++ .../java/com/stackify/test/DynamicTests.java | 38 +++++ .../java/com/stackify/test/IncrementTest.java | 25 +++ .../java/com/stackify/test/TaggedTest.java | 16 ++ .../java/com/stackify/test/UsersTest.java | 153 ++++++++++++++++++ 12 files changed, 582 insertions(+) create mode 100644 guest/junit5-example/.gitignore create mode 100644 guest/junit5-example/pom.xml create mode 100644 guest/junit5-example/src/main/java/com/stackify/daos/UserDAO.java create mode 100644 guest/junit5-example/src/main/java/com/stackify/models/User.java create mode 100644 guest/junit5-example/src/main/java/com/stackify/utils/ConnectionUtil.java create mode 100644 guest/junit5-example/src/main/resources/com/stackify/utils/jdbc.properties create mode 100644 guest/junit5-example/src/main/resources/log4j2.xml create mode 100644 guest/junit5-example/src/test/java/com/stackify/test/DatabaseConnectionTest.java create mode 100644 guest/junit5-example/src/test/java/com/stackify/test/DynamicTests.java create mode 100644 guest/junit5-example/src/test/java/com/stackify/test/IncrementTest.java create mode 100644 guest/junit5-example/src/test/java/com/stackify/test/TaggedTest.java create mode 100644 guest/junit5-example/src/test/java/com/stackify/test/UsersTest.java diff --git a/guest/junit5-example/.gitignore b/guest/junit5-example/.gitignore new file mode 100644 index 0000000000..60be5b80aa --- /dev/null +++ b/guest/junit5-example/.gitignore @@ -0,0 +1,4 @@ +/target/ +.settings/ +.classpath +.project diff --git a/guest/junit5-example/pom.xml b/guest/junit5-example/pom.xml new file mode 100644 index 0000000000..aec7f9228a --- /dev/null +++ b/guest/junit5-example/pom.xml @@ -0,0 +1,68 @@ + + 4.0.0 + junit5-example + junit5-example + 0.0.1-SNAPSHOT + + + + org.junit.jupiter + junit-jupiter-engine + 5.0.0-M4 + + + org.junit.jupiter + junit-jupiter-params + 5.0.0-M4 + + + + org.junit.vintage + junit-vintage-engine + 4.12.0-M4 + + + + com.h2database + h2 + 1.4.195 + + + + org.apache.logging.log4j + log4j-core + 2.8.2 + + + + + + + + maven-compiler-plugin + 3.5 + + 1.8 + 1.8 + + + + maven-surefire-plugin + 2.19.1 + + + org.junit.platform + junit-platform-surefire-provider + 1.0.0-M4 + + + + + math + + + + + + \ No newline at end of file diff --git a/guest/junit5-example/src/main/java/com/stackify/daos/UserDAO.java b/guest/junit5-example/src/main/java/com/stackify/daos/UserDAO.java new file mode 100644 index 0000000000..091d077ef6 --- /dev/null +++ b/guest/junit5-example/src/main/java/com/stackify/daos/UserDAO.java @@ -0,0 +1,141 @@ +package com.stackify.daos; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import com.stackify.models.User; +import com.stackify.utils.ConnectionUtil; + +public class UserDAO { + + private Logger logger = LogManager.getLogger(UserDAO.class); + + public void createTable() { + try (Connection con = ConnectionUtil.getConnection()) { + String createQuery = "CREATE TABLE users(email varchar(50) primary key, name varchar(50))"; + PreparedStatement pstmt = con.prepareStatement(createQuery); + + pstmt.execute(); + } catch (SQLException exc) { + logger.error(exc.getMessage()); + } + + } + + public void add(User user) { + try (Connection con = ConnectionUtil.getConnection()) { + + String insertQuery = "INSERT INTO users(email,name) VALUES(?,?)"; + PreparedStatement pstmt = con.prepareStatement(insertQuery); + pstmt.setString(1, user.getEmail()); + pstmt.setString(2, user.getName()); + + pstmt.executeUpdate(); + } catch (SQLException exc) { + logger.error(exc.getMessage()); + } + } + + public void update(User user) { + try (Connection con = ConnectionUtil.getConnection()) { + + String updateQuery = "UPDATE users SET name=? WHERE email=?"; + PreparedStatement pstmt = con.prepareStatement(updateQuery); + pstmt.setString(1, user.getName()); + pstmt.setString(2, user.getEmail()); + + pstmt.executeUpdate(); + } catch (SQLException exc) { + logger.error(exc.getMessage()); + } + } + + public void delete(User user) { + try (Connection con = ConnectionUtil.getConnection()) { + + String deleteQuery = "DELETE FROM users WHERE email=?"; + PreparedStatement pstmt = con.prepareStatement(deleteQuery); + pstmt.setString(1, user.getEmail()); + + pstmt.executeUpdate(); + } catch (SQLException exc) { + logger.error(exc.getMessage()); + } + } + + public void delete(String email) { + try (Connection con = ConnectionUtil.getConnection()) { + + String deleteQuery = "DELETE FROM users WHERE email=?"; + PreparedStatement pstmt = con.prepareStatement(deleteQuery); + pstmt.setString(1, email); + + pstmt.executeUpdate(); + } catch (SQLException exc) { + logger.error(exc.getMessage()); + } + } + + public User findOne(String email) { + User user = null; + + try (Connection con = ConnectionUtil.getConnection()) { + String query = "SELECT * FROM users WHERE email=?"; + PreparedStatement pstmt = con.prepareStatement(query); + pstmt.setString(1, email); + + ResultSet rs = pstmt.executeQuery(); + while (rs.next()) { + user = new User(); + user.setEmail(rs.getString("email")); + user.setName(rs.getString("name")); + } + + } catch (SQLException exc) { + logger.error(exc.getMessage()); + } + + return user; + } + + public List findAll() { + List users = new ArrayList<>(); + + try (Connection con = ConnectionUtil.getConnection()) { + String query = "SELECT * FROM users"; + PreparedStatement pstmt = con.prepareStatement(query); + + ResultSet rs = pstmt.executeQuery(); + while (rs.next()) { + User user = new User(); + user.setEmail(rs.getString("email")); + user.setName(rs.getString("name")); + users.add(user); + } + } catch (SQLException exc) { + logger.error(exc.getMessage()); + } + + return users; + } + + public void deleteAll() { + try (Connection con = ConnectionUtil.getConnection()) { + + String deleteQuery = "DELETE FROM users"; + PreparedStatement pstmt = con.prepareStatement(deleteQuery); + + pstmt.executeUpdate(); + } catch (SQLException exc) { + logger.error(exc.getMessage()); + } + } + +} diff --git a/guest/junit5-example/src/main/java/com/stackify/models/User.java b/guest/junit5-example/src/main/java/com/stackify/models/User.java new file mode 100644 index 0000000000..d6951102a7 --- /dev/null +++ b/guest/junit5-example/src/main/java/com/stackify/models/User.java @@ -0,0 +1,63 @@ +package com.stackify.models; + +public class User { + private String email; + private String name; + + public User() { + } + + public User(String email, String name) { + super(); + this.email = email; + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((email == null) ? 0 : email.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + User other = (User) obj; + if (email == null) { + if (other.email != null) + return false; + } else if (!email.equals(other.email)) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + +} diff --git a/guest/junit5-example/src/main/java/com/stackify/utils/ConnectionUtil.java b/guest/junit5-example/src/main/java/com/stackify/utils/ConnectionUtil.java new file mode 100644 index 0000000000..bcfe5058a7 --- /dev/null +++ b/guest/junit5-example/src/main/java/com/stackify/utils/ConnectionUtil.java @@ -0,0 +1,38 @@ +package com.stackify.utils; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Properties; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class ConnectionUtil { + + private static Logger logger = LogManager.getLogger(ConnectionUtil.class); + + public static Connection getConnection() { + try { + Properties props = new Properties(); + props.load(ConnectionUtil.class.getResourceAsStream("jdbc.properties")); + Class.forName(props.getProperty("jdbc.driver")); + Connection con = DriverManager.getConnection(props.getProperty("jdbc.url"), props.getProperty("jdbc.user"), props.getProperty("jdbc.password")); + return con; + } + + catch (FileNotFoundException exc) { + logger.error(exc.getMessage()); + } catch (IOException exc) { + logger.error(exc.getMessage()); + } catch (ClassNotFoundException exc) { + logger.error(exc.getMessage()); + } catch (SQLException exc) { + logger.error(exc.getMessage()); + } + return null; + } + +} diff --git a/guest/junit5-example/src/main/resources/com/stackify/utils/jdbc.properties b/guest/junit5-example/src/main/resources/com/stackify/utils/jdbc.properties new file mode 100644 index 0000000000..2d9a39b157 --- /dev/null +++ b/guest/junit5-example/src/main/resources/com/stackify/utils/jdbc.properties @@ -0,0 +1,4 @@ +jdbc.driver=org.h2.Driver +jdbc.url=jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1 +jdbc.user= +jdbc.password= \ No newline at end of file diff --git a/guest/junit5-example/src/main/resources/log4j2.xml b/guest/junit5-example/src/main/resources/log4j2.xml new file mode 100644 index 0000000000..602b5ab490 --- /dev/null +++ b/guest/junit5-example/src/main/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/guest/junit5-example/src/test/java/com/stackify/test/DatabaseConnectionTest.java b/guest/junit5-example/src/test/java/com/stackify/test/DatabaseConnectionTest.java new file mode 100644 index 0000000000..d8f13b5715 --- /dev/null +++ b/guest/junit5-example/src/test/java/com/stackify/test/DatabaseConnectionTest.java @@ -0,0 +1,19 @@ +package com.stackify.test; + +import java.sql.Connection; + +import org.junit.jupiter.api.Test; + +import com.stackify.utils.ConnectionUtil; + +import static org.junit.jupiter.api.Assertions.*; + +public interface DatabaseConnectionTest { + + @Test + default void testDatabaseConnection() { + Connection con = ConnectionUtil.getConnection(); + assertNotNull(con); + } + +} diff --git a/guest/junit5-example/src/test/java/com/stackify/test/DynamicTests.java b/guest/junit5-example/src/test/java/com/stackify/test/DynamicTests.java new file mode 100644 index 0000000000..39b3b5aac5 --- /dev/null +++ b/guest/junit5-example/src/test/java/com/stackify/test/DynamicTests.java @@ -0,0 +1,38 @@ +package com.stackify.test; + +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.function.Function; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.TestFactory; +import org.junit.jupiter.api.function.ThrowingConsumer; + +import com.stackify.daos.UserDAO; +import com.stackify.models.User; + +public class DynamicTests { + + @TestFactory + public Collection dynamicTestCollection() { + return Arrays.asList(DynamicTest.dynamicTest("Dynamic Test", () -> assertTrue(1 == 1))); + } + + @TestFactory + public Stream dynamicUserTestCollection() { + List inputList = Arrays.asList(new User("john@yahoo.com", "John"), new User("ana@yahoo.com", "Ana")); + + Function displayNameGenerator = (input) -> "Saving user: " + input; + + UserDAO userDAO = new UserDAO(); + ThrowingConsumer testExecutor = (input) -> { + userDAO.add(input); + assertNotNull(userDAO.findOne(input.getEmail())); + }; + + return DynamicTest.stream(inputList.iterator(), displayNameGenerator, testExecutor); + } +} diff --git a/guest/junit5-example/src/test/java/com/stackify/test/IncrementTest.java b/guest/junit5-example/src/test/java/com/stackify/test/IncrementTest.java new file mode 100644 index 0000000000..a23a5bf20b --- /dev/null +++ b/guest/junit5-example/src/test/java/com/stackify/test/IncrementTest.java @@ -0,0 +1,25 @@ +package com.stackify.test; + +import static org.junit.jupiter.api.Assertions.*; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.RepeatedTest; +import org.junit.jupiter.api.RepetitionInfo; + +public class IncrementTest { + + private static Logger logger = LogManager.getLogger(IncrementTest.class); + + @BeforeEach + public void increment() { + logger.info("Before Each Test"); + } + + @RepeatedTest(value = 3, name = RepeatedTest.SHORT_DISPLAY_NAME) + public void test(RepetitionInfo info) { + assertTrue(1 == 1); + logger.info("Repetition #" + info.getCurrentRepetition()); + } +} diff --git a/guest/junit5-example/src/test/java/com/stackify/test/TaggedTest.java b/guest/junit5-example/src/test/java/com/stackify/test/TaggedTest.java new file mode 100644 index 0000000000..db3c38ceac --- /dev/null +++ b/guest/junit5-example/src/test/java/com/stackify/test/TaggedTest.java @@ -0,0 +1,16 @@ +package com.stackify.test; + +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +@Tag("math") +public class TaggedTest { + + @Test + @Tag("arithmetic") + public void testEquals() { + assertTrue(1 == 1); + } + +} diff --git a/guest/junit5-example/src/test/java/com/stackify/test/UsersTest.java b/guest/junit5-example/src/test/java/com/stackify/test/UsersTest.java new file mode 100644 index 0000000000..fbbd56287d --- /dev/null +++ b/guest/junit5-example/src/test/java/com/stackify/test/UsersTest.java @@ -0,0 +1,153 @@ +package com.stackify.test; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assumptions.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import com.stackify.daos.UserDAO; +import com.stackify.models.User; + +public class UsersTest implements DatabaseConnectionTest { + + private static UserDAO userDAO; + + private static Logger logger = LogManager.getLogger(UsersTest.class); + + @BeforeAll + public static void addData() { + userDAO = new UserDAO(); + userDAO.createTable(); + + User user1 = new User("john@gmail.com", "John"); + User user2 = new User("ana@gmail.com", "Ana"); + userDAO.add(user1); + userDAO.add(user2); + } + + @Test + @DisplayName("Test Get Users") + public void testGetUsersNumber() { + assertEquals(2, userDAO.findAll().size()); + + } + + @Test + @DisplayName("Test Get Users") + public void testGetUsersNumberWithInfo(TestInfo testInfo) { + assertEquals(2, userDAO.findAll().size()); + assertEquals("Test Get Users", testInfo.getDisplayName()); + assertEquals(UsersTest.class, testInfo.getTestClass().get()); + logger.info("Running test method:" + testInfo.getTestMethod().get().getName()); + } + + @Test + public void testGetUser() { + User user = userDAO.findOne("john@gmail.com"); + assertEquals("John", user.getName(), "User name:" + user.getName() + " incorrect"); + } + + @Test + public void testClassicAssertions() { + User user1 = userDAO.findOne("john@gmail.com"); + User user2 = userDAO.findOne("john@yahoo.com"); + + assertNotNull(user1); + assertNull(user2); + + user2 = new User("john@yahoo.com", "John"); + assertEquals(user1.getName(), user2.getName(), "Names are not equal"); + assertFalse(user1.getEmail().equals(user2.getEmail()), "Emails are equal"); + assertNotSame(user1, user2); + } + + @Test + @Disabled + public void testGroupedAssertions() { + User user = userDAO.findOne("john@gmail.com"); + assertAll("user", () -> assertEquals("Johnson", user.getName()), () -> assertEquals("johnson@gmail.com", user.getEmail())); + } + + @Test + public void testIterableEquals() { + User user1 = new User("john@gmail.com", "John"); + User user2 = new User("ana@gmail.com", "Ana"); + + List users = new ArrayList<>(); + users.add(user1); + users.add(user2); + + assertIterableEquals(users, userDAO.findAll()); + } + + @Test + public void testLinesMatch() { + List expectedLines = Collections.singletonList("(.*)@(.*)"); + List emails = Arrays.asList("john@gmail.com"); + assertLinesMatch(expectedLines, emails); + } + + @Test + void testThrows() { + User user = null; + Exception exception = assertThrows(NullPointerException.class, () -> user.getName()); + logger.info(exception.getMessage()); + } + + @Test + @Disabled + void testFail() { + fail("this test fails"); + } + + @Test + void testAssumptions() { + List users = userDAO.findAll(); + assumeFalse(users == null); + assumeTrue(users.size() > 0); + + User user1 = new User("john@gmail.com", "John"); + + assumingThat(users.contains(user1), () -> assertTrue(users.size() > 1)); + } + + @ParameterizedTest + @ValueSource(strings = { "john@gmail.com", "ana@gmail.com" }) + public void testParameterized(String email) { + assertNotNull(userDAO.findOne(email)); + } + + @AfterAll + public static void removeData() { + userDAO.deleteAll(); + } + + @Nested + class DeleteUsersTest { + @Test + public void addUser() { + User user = new User("bob@gmail.com", "Bob"); + userDAO.add(user); + assertNotNull(userDAO.findOne("bob@gmail.com")); + + userDAO.delete("bob@gmail.com"); + assertNull(userDAO.findOne("bob@gmail.com")); + } + } + +} From 1028ad79fd4bf434f5c33db9728d1361c3672eb2 Mon Sep 17 00:00:00 2001 From: Jesus Boadas Date: Mon, 12 Jun 2017 11:55:12 -0400 Subject: [PATCH 2/4] OpenNLP (#2058) * Introduction to OpenNLP * Introduction to OpenNLP * OpenNLP fix code --- .../java/com/baeldung/opennlp/OpenNLP.java | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/libraries/src/main/java/com/baeldung/opennlp/OpenNLP.java b/libraries/src/main/java/com/baeldung/opennlp/OpenNLP.java index b2fa8e629b..dd44e96a3a 100644 --- a/libraries/src/main/java/com/baeldung/opennlp/OpenNLP.java +++ b/libraries/src/main/java/com/baeldung/opennlp/OpenNLP.java @@ -36,7 +36,7 @@ import opennlp.tools.util.TrainingParameters; public class OpenNLP { private final static Logger LOGGER = Logger.getLogger(OpenNLP.class.getName()); - private final static String text = "To get to the south: Go to the store. Buy a compass. Use the compass. Then walk to the south."; + private final static String text = buildString(); private final static String sentence[] = new String[] { "James", "Jordan", "live", "in", "Oklahoma", "city", "." }; private DoccatModel docCatModel; @@ -45,6 +45,16 @@ public class OpenNLP { new OpenNLP(); } + public static String buildString(){ + StringBuilder sb = new StringBuilder(); + sb.append("To get to the south:"); + sb.append(" Go to the store."); + sb.append(" Buy a compass."); + sb.append(" Use the compass."); + sb.append(" Then walk to the south."); + return sb.toString(); + } + public OpenNLP() { try { sentenceDetector(); @@ -68,7 +78,9 @@ public class OpenNLP { SentenceModel model = new SentenceModel(is); SentenceDetectorME sdetector = new SentenceDetectorME(model); String sentences[] = sdetector.sentDetect(text); - Arrays.stream(sentences).forEach(LOGGER::info); + for (String sentence : sentences) { + LOGGER.info(sentence); + } is.close(); } @@ -77,7 +89,9 @@ public class OpenNLP { TokenizerModel model = new TokenizerModel(is); Tokenizer tokenizer = new TokenizerME(model); String tokens[] = tokenizer.tokenize(text); - Arrays.stream(tokens).forEach(LOGGER::info); + for (String token : tokens) { + LOGGER.info(token); + } is.close(); } @@ -89,6 +103,9 @@ public class OpenNLP { Span nameSpans[] = nameFinder.find(sentence); String[] names = Span.spansToStrings(nameSpans, sentence); Arrays.stream(names).forEach(LOGGER::info); + for (String name : names) { + LOGGER.info(name); + } } public static void locationFinder() throws IOException { @@ -99,6 +116,9 @@ public class OpenNLP { Span locationSpans[] = nameFinder.find(sentence); String[] locations = Span.spansToStrings(locationSpans, sentence); Arrays.stream(locations).forEach(LOGGER::info); + for (String location : locations) { + LOGGER.info(location); + } } public void trainDocumentCategorizer() { @@ -160,7 +180,9 @@ public class OpenNLP { String[] taggedSentence = new String[] {"Out", "of", "the", "night", "that", "covers", "me"}; String pos[] = new String[] { "IN", "IN", "DT", "NN", "WDT", "VBZ", "PRP"}; String chunks[] = chunkerME.chunk(taggedSentence, pos); - Arrays.stream(chunks).forEach(LOGGER::info); + for (String chunk : chunks) { + LOGGER.info(chunk); + } } } From 80f633198ad2c2ea661bba1e52b4ef5baf86d09b Mon Sep 17 00:00:00 2001 From: lor6 Date: Mon, 12 Jun 2017 23:12:26 +0300 Subject: [PATCH 3/4] Junit5 (#2060) * junit 5 example * conditions example --- .../conditions/DisabledOnEnvironment.java | 15 ++++++++ .../DisabledOnEnvironmentCondition.java | 38 +++++++++++++++++++ .../src/main/resources/application.properties | 1 + .../java/com/stackify/test/UsersTest.java | 4 +- 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 guest/junit5-example/src/main/java/com/stackify/test/conditions/DisabledOnEnvironment.java create mode 100644 guest/junit5-example/src/main/java/com/stackify/test/conditions/DisabledOnEnvironmentCondition.java create mode 100644 guest/junit5-example/src/main/resources/application.properties diff --git a/guest/junit5-example/src/main/java/com/stackify/test/conditions/DisabledOnEnvironment.java b/guest/junit5-example/src/main/java/com/stackify/test/conditions/DisabledOnEnvironment.java new file mode 100644 index 0000000000..2c1fa80f2e --- /dev/null +++ b/guest/junit5-example/src/main/java/com/stackify/test/conditions/DisabledOnEnvironment.java @@ -0,0 +1,15 @@ +package com.stackify.test.conditions; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.junit.jupiter.api.extension.ExtendWith; + +@Target({ ElementType.METHOD }) +@Retention(RetentionPolicy.RUNTIME) +@ExtendWith(DisabledOnEnvironmentCondition.class) +public @interface DisabledOnEnvironment { + String[] value(); +} \ No newline at end of file diff --git a/guest/junit5-example/src/main/java/com/stackify/test/conditions/DisabledOnEnvironmentCondition.java b/guest/junit5-example/src/main/java/com/stackify/test/conditions/DisabledOnEnvironmentCondition.java new file mode 100644 index 0000000000..adb9d9c09e --- /dev/null +++ b/guest/junit5-example/src/main/java/com/stackify/test/conditions/DisabledOnEnvironmentCondition.java @@ -0,0 +1,38 @@ +package com.stackify.test.conditions; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Optional; +import java.util.Properties; + +import org.junit.jupiter.api.extension.ConditionEvaluationResult; +import org.junit.jupiter.api.extension.TestExecutionCondition; +import org.junit.jupiter.api.extension.TestExtensionContext; +import org.junit.platform.commons.support.AnnotationSupport; + +import com.stackify.utils.ConnectionUtil; + +public class DisabledOnEnvironmentCondition implements TestExecutionCondition { + + @Override + public ConditionEvaluationResult evaluate(TestExtensionContext context) { + Properties props = new Properties(); + String env = ""; + try { + props.load(ConnectionUtil.class.getResourceAsStream("/application.properties")); + env = props.getProperty("env"); + } catch (IOException e) { + e.printStackTrace(); + } + Optional disabled = AnnotationSupport.findAnnotation(context.getElement().get(), DisabledOnEnvironment.class); + if (disabled.isPresent()) { + String[] envs = disabled.get().value(); + if (Arrays.asList(envs).contains(env)) { + return ConditionEvaluationResult.disabled("Disabled on environment " + env); + } + } + + return ConditionEvaluationResult.enabled("Enabled on environment "+env); + } + +} diff --git a/guest/junit5-example/src/main/resources/application.properties b/guest/junit5-example/src/main/resources/application.properties new file mode 100644 index 0000000000..601f964ff3 --- /dev/null +++ b/guest/junit5-example/src/main/resources/application.properties @@ -0,0 +1 @@ +env=dev \ No newline at end of file diff --git a/guest/junit5-example/src/test/java/com/stackify/test/UsersTest.java b/guest/junit5-example/src/test/java/com/stackify/test/UsersTest.java index fbbd56287d..88958a8232 100644 --- a/guest/junit5-example/src/test/java/com/stackify/test/UsersTest.java +++ b/guest/junit5-example/src/test/java/com/stackify/test/UsersTest.java @@ -23,6 +23,8 @@ import java.util.List; import com.stackify.daos.UserDAO; import com.stackify.models.User; +import com.stackify.test.conditions.DisabledOnEnvironment; + public class UsersTest implements DatabaseConnectionTest { private static UserDAO userDAO; @@ -110,7 +112,7 @@ public class UsersTest implements DatabaseConnectionTest { } @Test - @Disabled + @DisabledOnEnvironment({ "dev", "prod") void testFail() { fail("this test fails"); } From f73893bbb90600c249bb9402fe190e90000ba926 Mon Sep 17 00:00:00 2001 From: Doha2012 Date: Mon, 12 Jun 2017 22:12:54 +0200 Subject: [PATCH 4/4] spring boot bootstrap (#2031) * minor logging fix * spring security sso * use basic auth * use form login * cleanup * cleanup * final cleanup * second client app for sso * spring boot bootstrap * add logic * cleanup * add simple controller * add thymeleaf and security * minor fix * minor fix * add more boot properties --- spring-boot-bootstrap/.gitignore | 24 +++ spring-boot-bootstrap/pom.xml | 75 +++++++++ .../main/java/org/baeldung/Application.java | 19 +++ .../org/baeldung/persistence/model/Book.java | 96 ++++++++++++ .../persistence/repo/BookRepository.java | 10 ++ .../java/org/baeldung/web/BookController.java | 74 +++++++++ .../baeldung/web/RestExceptionHandler.java | 32 ++++ .../org/baeldung/web/SimpleController.java | 19 +++ .../exception/BookIdMismatchException.java | 20 +++ .../web/exception/BookNotFoundException.java | 20 +++ .../src/main/resources/application.properties | 21 +++ .../src/main/resources/templates/error.html | 19 +++ .../src/main/resources/templates/home.html | 7 + .../src/test/java/org/baeldung/LiveTest.java | 142 ++++++++++++++++++ .../SpringBootBootstrapApplicationTests.java | 16 ++ 15 files changed, 594 insertions(+) create mode 100644 spring-boot-bootstrap/.gitignore create mode 100644 spring-boot-bootstrap/pom.xml create mode 100644 spring-boot-bootstrap/src/main/java/org/baeldung/Application.java create mode 100644 spring-boot-bootstrap/src/main/java/org/baeldung/persistence/model/Book.java create mode 100644 spring-boot-bootstrap/src/main/java/org/baeldung/persistence/repo/BookRepository.java create mode 100644 spring-boot-bootstrap/src/main/java/org/baeldung/web/BookController.java create mode 100644 spring-boot-bootstrap/src/main/java/org/baeldung/web/RestExceptionHandler.java create mode 100644 spring-boot-bootstrap/src/main/java/org/baeldung/web/SimpleController.java create mode 100644 spring-boot-bootstrap/src/main/java/org/baeldung/web/exception/BookIdMismatchException.java create mode 100644 spring-boot-bootstrap/src/main/java/org/baeldung/web/exception/BookNotFoundException.java create mode 100644 spring-boot-bootstrap/src/main/resources/application.properties create mode 100644 spring-boot-bootstrap/src/main/resources/templates/error.html create mode 100644 spring-boot-bootstrap/src/main/resources/templates/home.html create mode 100644 spring-boot-bootstrap/src/test/java/org/baeldung/LiveTest.java create mode 100644 spring-boot-bootstrap/src/test/java/org/baeldung/SpringBootBootstrapApplicationTests.java diff --git a/spring-boot-bootstrap/.gitignore b/spring-boot-bootstrap/.gitignore new file mode 100644 index 0000000000..2af7cefb0a --- /dev/null +++ b/spring-boot-bootstrap/.gitignore @@ -0,0 +1,24 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ \ No newline at end of file diff --git a/spring-boot-bootstrap/pom.xml b/spring-boot-bootstrap/pom.xml new file mode 100644 index 0000000000..697a139eb5 --- /dev/null +++ b/spring-boot-bootstrap/pom.xml @@ -0,0 +1,75 @@ + + + 4.0.0 + + org.baeldung + spring-boot-bootstrap + 0.0.1-SNAPSHOT + jar + + spring-boot-bootstrap + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 1.5.3.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + + + org.springframework.boot + spring-boot-starter-security + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + io.rest-assured + rest-assured + 3.0.3 + test + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/spring-boot-bootstrap/src/main/java/org/baeldung/Application.java b/spring-boot-bootstrap/src/main/java/org/baeldung/Application.java new file mode 100644 index 0000000000..f7e7bb0347 --- /dev/null +++ b/spring-boot-bootstrap/src/main/java/org/baeldung/Application.java @@ -0,0 +1,19 @@ +package org.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; + +@SpringBootApplication +@ComponentScan("org.baeldung") +@EnableJpaRepositories("org.baeldung.persistence.repo") +@EntityScan("org.baeldung.persistence.model") +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/spring-boot-bootstrap/src/main/java/org/baeldung/persistence/model/Book.java b/spring-boot-bootstrap/src/main/java/org/baeldung/persistence/model/Book.java new file mode 100644 index 0000000000..02060e17c9 --- /dev/null +++ b/spring-boot-bootstrap/src/main/java/org/baeldung/persistence/model/Book.java @@ -0,0 +1,96 @@ +package org.baeldung.persistence.model; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Book { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + + @Column(nullable = false, unique = true) + private String title; + + @Column(nullable = false) + private String author; + + // + + public Book() { + super(); + } + + public Book(String title, String author) { + super(); + this.title = title; + this.author = author; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((author == null) ? 0 : author.hashCode()); + result = prime * result + (int) (id ^ (id >>> 32)); + result = prime * result + ((title == null) ? 0 : title.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Book other = (Book) obj; + if (author == null) { + if (other.author != null) + return false; + } else if (!author.equals(other.author)) + return false; + if (id != other.id) + return false; + if (title == null) { + if (other.title != null) + return false; + } else if (!title.equals(other.title)) + return false; + return true; + } + + @Override + public String toString() { + return "Book [id=" + id + ", title=" + title + ", author=" + author + "]"; + } + +} \ No newline at end of file diff --git a/spring-boot-bootstrap/src/main/java/org/baeldung/persistence/repo/BookRepository.java b/spring-boot-bootstrap/src/main/java/org/baeldung/persistence/repo/BookRepository.java new file mode 100644 index 0000000000..ec3a3e25bc --- /dev/null +++ b/spring-boot-bootstrap/src/main/java/org/baeldung/persistence/repo/BookRepository.java @@ -0,0 +1,10 @@ +package org.baeldung.persistence.repo; + +import java.util.List; + +import org.baeldung.persistence.model.Book; +import org.springframework.data.repository.CrudRepository; + +public interface BookRepository extends CrudRepository { + List findByTitle(String title); +} diff --git a/spring-boot-bootstrap/src/main/java/org/baeldung/web/BookController.java b/spring-boot-bootstrap/src/main/java/org/baeldung/web/BookController.java new file mode 100644 index 0000000000..544ac80217 --- /dev/null +++ b/spring-boot-bootstrap/src/main/java/org/baeldung/web/BookController.java @@ -0,0 +1,74 @@ +package org.baeldung.web; + +import java.util.List; + +import org.baeldung.persistence.model.Book; +import org.baeldung.persistence.repo.BookRepository; +import org.baeldung.web.exception.BookIdMismatchException; +import org.baeldung.web.exception.BookNotFoundException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api/books") +public class BookController { + + @Autowired + private BookRepository bookRepository; + + @GetMapping + public Iterable findAll() { + return bookRepository.findAll(); + } + + @GetMapping("/title/{bookTitle}") + public List findByTitle(@PathVariable String bookTitle) { + return bookRepository.findByTitle(bookTitle); + } + + @GetMapping("/{id}") + public Book findOne(@PathVariable Long id) { + final Book book = bookRepository.findOne(id); + if (book == null) { + throw new BookNotFoundException(); + } + return book; + } + + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + public Book create(@RequestBody Book book) { + return bookRepository.save(book); + } + + @DeleteMapping("/{id}") + public void delete(@PathVariable Long id) { + final Book book = bookRepository.findOne(id); + if (book == null) { + throw new BookNotFoundException(); + } + bookRepository.delete(id); + } + + @PutMapping("/{id}") + public Book updateBook(@RequestBody Book book, @PathVariable Long id) { + if (book.getId() != id) { + throw new BookIdMismatchException(); + } + final Book old = bookRepository.findOne(id); + if (old == null) { + throw new BookNotFoundException(); + } + return bookRepository.save(book); + } + +} diff --git a/spring-boot-bootstrap/src/main/java/org/baeldung/web/RestExceptionHandler.java b/spring-boot-bootstrap/src/main/java/org/baeldung/web/RestExceptionHandler.java new file mode 100644 index 0000000000..60153fc92f --- /dev/null +++ b/spring-boot-bootstrap/src/main/java/org/baeldung/web/RestExceptionHandler.java @@ -0,0 +1,32 @@ +package org.baeldung.web; + +import org.baeldung.web.exception.BookIdMismatchException; +import org.baeldung.web.exception.BookNotFoundException; +import org.hibernate.exception.ConstraintViolationException; +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.context.request.WebRequest; +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; + +@ControllerAdvice +public class RestExceptionHandler extends ResponseEntityExceptionHandler { + + public RestExceptionHandler() { + super(); + } + + @ExceptionHandler({ BookNotFoundException.class }) + protected ResponseEntity handleNotFound(Exception ex, WebRequest request) { + return handleExceptionInternal(ex, "Book not found", new HttpHeaders(), HttpStatus.NOT_FOUND, request); + } + + @ExceptionHandler({ BookIdMismatchException.class, ConstraintViolationException.class, DataIntegrityViolationException.class }) + public ResponseEntity handleBadRequest(Exception ex, WebRequest request) { + return handleExceptionInternal(ex, ex.getLocalizedMessage(), new HttpHeaders(), HttpStatus.BAD_REQUEST, request); + } + +} \ No newline at end of file diff --git a/spring-boot-bootstrap/src/main/java/org/baeldung/web/SimpleController.java b/spring-boot-bootstrap/src/main/java/org/baeldung/web/SimpleController.java new file mode 100644 index 0000000000..0bec973a75 --- /dev/null +++ b/spring-boot-bootstrap/src/main/java/org/baeldung/web/SimpleController.java @@ -0,0 +1,19 @@ +package org.baeldung.web; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class SimpleController { + @Value("${spring.application.name}") + String appName; + + @RequestMapping("/") + public String homePage(Model model) { + model.addAttribute("appName", appName); + return "home"; + } + +} diff --git a/spring-boot-bootstrap/src/main/java/org/baeldung/web/exception/BookIdMismatchException.java b/spring-boot-bootstrap/src/main/java/org/baeldung/web/exception/BookIdMismatchException.java new file mode 100644 index 0000000000..23c55e2d38 --- /dev/null +++ b/spring-boot-bootstrap/src/main/java/org/baeldung/web/exception/BookIdMismatchException.java @@ -0,0 +1,20 @@ +package org.baeldung.web.exception; + +public class BookIdMismatchException extends RuntimeException { + + public BookIdMismatchException() { + super(); + } + + public BookIdMismatchException(final String message, final Throwable cause) { + super(message, cause); + } + + public BookIdMismatchException(final String message) { + super(message); + } + + public BookIdMismatchException(final Throwable cause) { + super(cause); + } +} diff --git a/spring-boot-bootstrap/src/main/java/org/baeldung/web/exception/BookNotFoundException.java b/spring-boot-bootstrap/src/main/java/org/baeldung/web/exception/BookNotFoundException.java new file mode 100644 index 0000000000..3ff416b3f3 --- /dev/null +++ b/spring-boot-bootstrap/src/main/java/org/baeldung/web/exception/BookNotFoundException.java @@ -0,0 +1,20 @@ +package org.baeldung.web.exception; + +public class BookNotFoundException extends RuntimeException { + + public BookNotFoundException() { + super(); + } + + public BookNotFoundException(final String message, final Throwable cause) { + super(message, cause); + } + + public BookNotFoundException(final String message) { + super(message); + } + + public BookNotFoundException(final Throwable cause) { + super(cause); + } +} \ No newline at end of file diff --git a/spring-boot-bootstrap/src/main/resources/application.properties b/spring-boot-bootstrap/src/main/resources/application.properties new file mode 100644 index 0000000000..b7e6b1e80e --- /dev/null +++ b/spring-boot-bootstrap/src/main/resources/application.properties @@ -0,0 +1,21 @@ +server.port = 8081 + +spring.application.name = Bootstrap Spring Boot + +spring.thymeleaf.cache = false +spring.thymeleaf.enabled=true +spring.thymeleaf.prefix=classpath:/templates/ +spring.thymeleaf.suffix=.html + +security.basic.enabled=true +security.user.name=john +security.user.password=123 + + +spring.datasource.driver-class-name=org.h2.Driver +spring.datasource.url=jdbc:h2:mem:bootapp;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE +spring.datasource.username=sa +spring.datasource.password= + +server.error.path=/error +server.error.whitelabel.enabled=false \ No newline at end of file diff --git a/spring-boot-bootstrap/src/main/resources/templates/error.html b/spring-boot-bootstrap/src/main/resources/templates/error.html new file mode 100644 index 0000000000..f2b51a1938 --- /dev/null +++ b/spring-boot-bootstrap/src/main/resources/templates/error.html @@ -0,0 +1,19 @@ + + +Error Occurred + + + +
+

Error Occurred!

+ +
+ + + \ No newline at end of file diff --git a/spring-boot-bootstrap/src/main/resources/templates/home.html b/spring-boot-bootstrap/src/main/resources/templates/home.html new file mode 100644 index 0000000000..5707cfb82a --- /dev/null +++ b/spring-boot-bootstrap/src/main/resources/templates/home.html @@ -0,0 +1,7 @@ + +Home Page + +

Hello !

+

Welcome to Our App

+ + \ No newline at end of file diff --git a/spring-boot-bootstrap/src/test/java/org/baeldung/LiveTest.java b/spring-boot-bootstrap/src/test/java/org/baeldung/LiveTest.java new file mode 100644 index 0000000000..5ecd4023bd --- /dev/null +++ b/spring-boot-bootstrap/src/test/java/org/baeldung/LiveTest.java @@ -0,0 +1,142 @@ +package org.baeldung; + +import static io.restassured.RestAssured.preemptive; +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; +import static org.apache.commons.lang3.RandomStringUtils.randomNumeric; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import io.restassured.RestAssured; +import io.restassured.response.Response; + +import java.util.List; + +import org.baeldung.persistence.model.Book; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = { Application.class }, webEnvironment = WebEnvironment.DEFINED_PORT) +public class LiveTest { + + @Before + public void setUp() { + RestAssured.authentication = preemptive().basic("john", "123"); + } + + private static final String API_ROOT = "http://localhost:8081/api/books"; + + @Test + public void whenGetAllBooks_thenOK() { + final Response response = RestAssured.get(API_ROOT); + assertEquals(HttpStatus.OK.value(), response.getStatusCode()); + } + + @Test + public void whenGetBooksByTitle_thenOK() { + final Book book = createRandomBook(); + createBookAsUri(book); + + final Response response = RestAssured.get(API_ROOT + "/title/" + book.getTitle()); + assertEquals(HttpStatus.OK.value(), response.getStatusCode()); + assertTrue(response.as(List.class) + .size() > 0); + } + + @Test + public void whenGetCreatedBookById_thenOK() { + final Book book = createRandomBook(); + final String location = createBookAsUri(book); + + final Response response = RestAssured.get(location); + assertEquals(HttpStatus.OK.value(), response.getStatusCode()); + assertEquals(book.getTitle(), response.jsonPath() + .get("title")); + } + + @Test + public void whenGetNotExistBookById_thenNotFound() { + final Response response = RestAssured.get(API_ROOT + "/" + randomNumeric(4)); + assertEquals(HttpStatus.NOT_FOUND.value(), response.getStatusCode()); + } + + // POST + @Test + public void whenCreateNewBook_thenCreated() { + final Book book = createRandomBook(); + + final Response response = RestAssured.given() + .contentType(MediaType.APPLICATION_JSON_VALUE) + .body(book) + .post(API_ROOT); + assertEquals(HttpStatus.CREATED.value(), response.getStatusCode()); + } + + @Test + public void whenInvalidBook_thenError() { + final Book book = createRandomBook(); + book.setAuthor(null); + + final Response response = RestAssured.given() + .contentType(MediaType.APPLICATION_JSON_VALUE) + .body(book) + .post(API_ROOT); + assertEquals(HttpStatus.BAD_REQUEST.value(), response.getStatusCode()); + } + + @Test + public void whenUpdateCreatedBook_thenUpdated() { + final Book book = createRandomBook(); + final String location = createBookAsUri(book); + + book.setId(Long.parseLong(location.split("api/books/")[1])); + book.setAuthor("newAuthor"); + Response response = RestAssured.given() + .contentType(MediaType.APPLICATION_JSON_VALUE) + .body(book) + .put(location); + assertEquals(HttpStatus.OK.value(), response.getStatusCode()); + + response = RestAssured.get(location); + assertEquals(HttpStatus.OK.value(), response.getStatusCode()); + assertEquals("newAuthor", response.jsonPath() + .get("author")); + + } + + @Test + public void whenDeleteCreatedBook_thenOk() { + final Book book = createRandomBook(); + final String location = createBookAsUri(book); + + Response response = RestAssured.delete(location); + assertEquals(HttpStatus.OK.value(), response.getStatusCode()); + + response = RestAssured.get(location); + assertEquals(HttpStatus.NOT_FOUND.value(), response.getStatusCode()); + } + + // =============================== + + private Book createRandomBook() { + final Book book = new Book(); + book.setTitle(randomAlphabetic(10)); + book.setAuthor(randomAlphabetic(15)); + return book; + } + + private String createBookAsUri(Book book) { + final Response response = RestAssured.given() + .contentType(MediaType.APPLICATION_JSON_VALUE) + .body(book) + .post(API_ROOT); + return API_ROOT + "/" + response.jsonPath() + .get("id"); + } + +} \ No newline at end of file diff --git a/spring-boot-bootstrap/src/test/java/org/baeldung/SpringBootBootstrapApplicationTests.java b/spring-boot-bootstrap/src/test/java/org/baeldung/SpringBootBootstrapApplicationTests.java new file mode 100644 index 0000000000..f32315ef03 --- /dev/null +++ b/spring-boot-bootstrap/src/test/java/org/baeldung/SpringBootBootstrapApplicationTests.java @@ -0,0 +1,16 @@ +package org.baeldung; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class SpringBootBootstrapApplicationTests { + + @Test + public void contextLoads() { + } + +}