Advertisement
Guest User

Untitled

a guest
Apr 10th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.47 KB | None | 0 0
  1. @WebAppConfiguration
  2. @ContextConfiguration(classes={LibraryManagerContextConfiguration.class, WebSecurityConfig.class})
  3. public class AbstractIntegrationTest {
  4.  
  5. @Autowired
  6. private WebApplicationContext webApplicationContext;
  7.  
  8. @Autowired
  9. private JwtAuthenticationFilter jwtAuthenticationFilter;
  10.  
  11. @Autowired
  12. private LoginFilter loginFilter;
  13.  
  14. private MockMvc mockMvc;
  15.  
  16. private static IDatabaseConnection databaseConnection;
  17. private static Connection connection;
  18. private static boolean isAfterFirstRun;
  19. private static Logger logger = LogManager.getLogger(AbstractIntegrationTest.class);
  20.  
  21. @BeforeClass
  22. public static void createDatabase() throws Exception {
  23. try {
  24. final Properties properties = loadProperties();
  25.  
  26. final String driver = properties.getProperty("datasource.driver");
  27. final String url = properties.getProperty("datasource.url");
  28. final String userName = properties.getProperty("datasource.username");
  29. final String password = properties.getProperty("datasource.password");
  30. final String schema = properties.getProperty("datasource.schema");
  31.  
  32. Class.forName(driver);
  33. connection = DriverManager.getConnection(url, userName, password);
  34. databaseConnection = new HsqldbConnection(connection, schema);
  35.  
  36. } catch (final SQLException exception) {
  37. throw new RuntimeException(exception.getMessage(), exception);
  38. } catch (final ClassNotFoundException exception) {
  39. throw new RuntimeException(exception.getMessage(), exception);
  40. }
  41. }
  42.  
  43. @Before
  44. public void setDatabaseUp() throws Exception {
  45. if (!isAfterFirstRun) {
  46. runSQLCommands(getDataSetupFile());
  47. }
  48. runSQLCommands(getClearDatabaseFile());
  49. runSQLCommands(getResetSequencesFile());
  50. runSQLCommands(getDataFile());
  51. isAfterFirstRun = true;
  52. }
  53.  
  54. @AfterClass
  55. public static void closeConnection() throws Exception {
  56. connection.close();
  57. databaseConnection.close();
  58. }
  59.  
  60. protected void runSQLCommands(final String file) throws Exception {
  61. if (file != null) {
  62. final InputStream stream = getSQLInputStream(file);
  63. final BufferedReader databaseReader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
  64.  
  65. int i = 1;
  66. String sqlStatement = null;
  67. while ((sqlStatement = databaseReader.readLine()) != null) {
  68. if (sqlStatement.startsWith("--")) {
  69. i++;
  70. continue;
  71. }
  72. final int index = sqlStatement.lastIndexOf(";");
  73. if (index > -1) {
  74. sqlStatement = sqlStatement.substring(0, index + 1);
  75. }
  76. if (sqlStatement.trim().length() != 0) {
  77. try {
  78. connection.createStatement().execute(sqlStatement);
  79. logger.info(sqlStatement);
  80. } catch (final Exception exception) {
  81. logger.error("Error running command on line " + i + " of file " + file + ": " + exception.getMessage());
  82. throw new RuntimeException(exception);
  83. }
  84. }
  85. i++;
  86. }
  87. }
  88. }
  89.  
  90. protected IDatabaseConnection getConnection() {
  91. return databaseConnection;
  92. }
  93.  
  94. protected static IDataSet getDataSet(final String dataset) {
  95. try {
  96. final InputSource source = new InputSource(AbstractIntegrationTest.class.getResourceAsStream(dataset));
  97. return new FlatXmlDataSetBuilder().build(source);
  98. } catch (final Exception exception) {
  99. throw new RuntimeException("Cannot read the dataset file " + dataset + "!", exception);
  100. }
  101. }
  102.  
  103. private static Properties loadProperties() throws Exception {
  104. final InputStream stream = ClassLoader.getSystemResourceAsStream("datasource.properties");
  105. if (stream == null) {
  106. throw new FileNotFoundException("File erm.properties not found. A file named erm.properties must be present "
  107. + "in the src/test/resources folder of the project whose class is being tested.");
  108. }
  109. final Properties properties = new Properties();
  110. properties.load(stream);
  111. return properties;
  112. }
  113.  
  114. private static InputStream getSQLInputStream(final String fileName) {
  115. return AbstractIntegrationTest.class.getResourceAsStream(fileName);
  116. }
  117.  
  118. protected String getClearDatabaseFile() {
  119. return "/database/clear-database.sql";
  120. }
  121.  
  122. protected String getDataSetupFile() {
  123. return "/database/database-setup.sql";
  124. }
  125.  
  126. protected String getDataFile() {
  127. return "/database/data.sql";
  128. }
  129.  
  130. protected String getResetSequencesFile() {
  131. return "/database/reset-sequences.sql";
  132. }
  133.  
  134. protected void verifyDatasetForTable(final String fileName, final String tableName, final String[] ignoredColumns) throws Exception {
  135. assertEqualsByQuery(getDataSet("/database/datasets/" + fileName), getConnection(), "select * from " + tableName, tableName, ignoredColumns);
  136. }
  137.  
  138. protected void verifyDatasetForTable(final String fileName, final String tableName, final String sqlStatement, final String[] ignoredColumns)
  139. throws Exception {
  140. printValues(tableName, sqlStatement, getConnection());
  141. assertEqualsByQuery(getDataSet("/database/datasets/" + fileName), getConnection(), sqlStatement, tableName, ignoredColumns);
  142. }
  143.  
  144. private void printValues(final String tableName, final String query, final IDatabaseConnection conn) throws Exception {
  145. final ITable table = conn.createQueryTable(tableName, query);
  146. for (int i = 0; i < table.getRowCount(); i++) {
  147. System.out.print(i + 1 + " ");
  148. for (int j = 0; j < table.getTableMetaData().getColumns().length; j++) {
  149. final String column = table.getTableMetaData().getColumns()[j].getColumnName();
  150. System.out.print(column + ": " + table.getValue(i, column) + " ");
  151. }
  152. System.out.println();
  153. }
  154. }
  155.  
  156. @RunWith(SpringJUnit4ClassRunner.class)
  157. public class AuthenticationIntegrationTest extends AbstractIntegrationTest {
  158.  
  159. @Test
  160. public void shouldGetAuthorizationJwt() throws Exception {
  161.  
  162. final String jsonCredentials = "{"
  163. + ""username" : "augusto","
  164. + ""password" : "spring""
  165. + "}";
  166.  
  167. final MvcResult result = performRESTLogin(jsonCredentials);
  168. final MockHttpServletResponse response = result.getResponse();
  169. final int status = response.getStatus();
  170. final String jwt = response.getHeader("Authorization");
  171.  
  172. assertThat(status, is(200));
  173. assertThat(jwt, notNullValue());
  174. }
  175.  
  176. }
  177.  
  178. server.contextPath=/librarymanager
  179. server.port: 8081
  180.  
  181. spring.datasource.url = jdbc:mysql://localhost:3306/librarymanager
  182. spring.datasource.username = root
  183. spring.datasource.password = root
  184. spring.jpa.show-sql = true
  185. spring.jpa.hibernate.ddl-auto = create-drop
  186. spring.jpa.hibernate.naming.strategy = org.hibernate.cfg.ImprovedNamingStrategy
  187. spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
  188.  
  189. datasource.class=org.hsqldb.jdbc.JDBCDataSource
  190. datasource.driver=org.hsqldb.jdbc.JDBCDriver
  191. datasource.url=jdbc:hsqldb:mem:librarymanager;sql.syntax_ora=true
  192. datasource.schema=sa
  193. datasource.username=sa
  194. datasource.password=
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement