Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.

Commit 03a95ba

Browse files
committed
Add new exercise WallStreetDbInitializer.java
1 parent 5a4d6be commit 03a95ba

File tree

9 files changed

+567
-2
lines changed

9 files changed

+567
-2
lines changed

account-db-initializer/src/test/java/com/bobocode/AccountDbInitializerTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void testTableHasPrimaryKey() throws SQLException {
5555
}
5656

5757
@Test
58-
public void testPrimaryKeyBasedOnIdField() throws SQLException {
58+
public void testPrimaryKeyHasCorrectName() throws SQLException {
5959
try (Connection connection = dataSource.getConnection()) {
6060
Statement statement = connection.createStatement();
6161
ResultSet resultSet = statement.executeQuery("SELECT * FROM INFORMATION_SCHEMA.CONSTRAINTS" +
@@ -69,7 +69,7 @@ public void testPrimaryKeyBasedOnIdField() throws SQLException {
6969
}
7070

7171
@Test
72-
public void testPrimaryKeyHasCorrectName() throws SQLException {
72+
public void testPrimaryKeyBasedOnIdField() throws SQLException {
7373
try (Connection connection = dataSource.getConnection()) {
7474
Statement statement = connection.createStatement();
7575
ResultSet resultSet = statement.executeQuery("SELECT * FROM INFORMATION_SCHEMA.CONSTRAINTS" +
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.bobocode.util;
2+
3+
import java.io.IOException;
4+
import java.net.URISyntaxException;
5+
import java.net.URL;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
9+
import java.util.Objects;
10+
import java.util.stream.Stream;
11+
12+
import static java.util.stream.Collectors.joining;
13+
14+
/**
15+
* {@link FileReader} provides an API that allow to read whole file into a {@link String} by file name.
16+
*/
17+
public class FileReader {
18+
19+
/**
20+
* Returns a {@link String} that contains whole text from the file specified by name.
21+
*
22+
* @param fileName a name of a text file
23+
* @return string that holds whole file content
24+
*/
25+
public static String readWholeFileFromResources(String fileName) {
26+
Path filePath = createPathFromFileName(fileName);
27+
try (Stream<String> fileLinesStream = openFileLinesStream(filePath)) {
28+
return fileLinesStream.collect(joining("\n"));
29+
}
30+
}
31+
32+
private static Stream<String> openFileLinesStream(Path filePath) {
33+
try {
34+
return Files.lines(filePath);
35+
} catch (IOException e) {
36+
throw new FileReaderException("Cannot create stream of file lines!", e);
37+
}
38+
}
39+
40+
private static Path createPathFromFileName(String fileName) {
41+
Objects.requireNonNull(fileName);
42+
URL fileUrl = FileReader.class.getClassLoader().getResource(fileName);
43+
try {
44+
return Paths.get(fileUrl.toURI());
45+
} catch (URISyntaxException e) {
46+
throw new FileReaderException("Invalid file URL",e);
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.bobocode.util;
2+
3+
public class FileReaderException extends RuntimeException {
4+
public FileReaderException(String message, Exception e) {
5+
super(message, e);
6+
}
7+
}

pom.xml

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<module>jdbc-account-data</module>
1515
<module>product-dao</module>
1616
<module>account-db-initializer</module>
17+
<module>wall-street-db-initializer</module>
1718
</modules>
1819

1920
<properties>
@@ -46,6 +47,13 @@
4647
<groupId>junit</groupId>
4748
<artifactId>junit</artifactId>
4849
<version>4.12</version>
50+
<scope>test</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.hamcrest</groupId>
54+
<artifactId>hamcrest-all</artifactId>
55+
<version>1.3</version>
56+
<scope>test</scope>
4957
</dependency>
5058
<dependency>
5159
<groupId>org.apache.commons</groupId>

wall-street-db-initializer/README.MD

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>Wall Street db initializer exercise :muscle:
2+
Improve your database design and SQL skills
3+
### Task
4+
`WallStreetDbInitializer` provides an API that allows to create(initialize) a database tables to store broker information,
5+
sales groups and its relations. Roughly speaking each group consists of multiple brokers, and each broker can be
6+
associated with more than one group. The job of initializer is to crate proper database tables.
7+
8+
`WallStreetDbInitializer` contains a *javadoc* that specifies database requirements. **It already contains all required Java
9+
implementation.** Your job is to **implement SQL file** `table_initialization.sql`.
10+
11+
The purpose of the task is to **design a database table following docs and naming convention and implement it using SQL**
12+
13+
To verify your implementation, run `WallStreetDbInitializerTest.java`
14+
15+
### Pre-conditions :heavy_exclamation_mark:
16+
You're supposed to be familiar with database design and naming convention, *JDBC API* and *SQL*
17+
18+
### How to start :question:
19+
* Just clone the repository and start implementing the **todo** section, verify your changes by running tests
20+
* If you don't have enough knowledge about this domain, check out the [links below](#related-materials-information_source)
21+
* Don't worry if you got stuck, checkout the **exercise/completed** branch and see the final implementation
22+
23+
### Related materials :information_source:
24+
* [JDBC API basics tutorial](https://github.com/bobocode-projects/jdbc-api-tutorial/tree/master/jdbc-basics) <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=20/>
25+
26+

wall-street-db-initializer/pom.xml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>jdbc-api-exercises</artifactId>
7+
<groupId>com.bobocode</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>wall-street-db-initializer</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.bobocode</groupId>
17+
<artifactId>jdbc-util</artifactId>
18+
<version>1.0-SNAPSHOT</version>
19+
</dependency>
20+
</dependencies>
21+
22+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.bobocode;
2+
3+
import com.bobocode.util.FileReader;
4+
5+
import javax.sql.DataSource;
6+
import java.sql.Connection;
7+
import java.sql.SQLException;
8+
import java.sql.Statement;
9+
10+
/**
11+
* {@link WallStreetDbInitializer} is an API that has only one method. It allow to create a database tables to store
12+
* information about brokers and its sales groups.
13+
*/
14+
public class WallStreetDbInitializer {
15+
private final static String TABLE_INITIALIZATION_SQL_FILE = "db/migration/table_initialization.sql"; // todo: see the file
16+
private DataSource dataSource;
17+
18+
public WallStreetDbInitializer(DataSource dataSource) {
19+
this.dataSource = dataSource;
20+
}
21+
22+
/**
23+
* Reads the SQL script form the file and executes it
24+
*
25+
* @throws SQLException
26+
*/
27+
public void init() throws SQLException {
28+
String createTablesSql = FileReader.readWholeFileFromResources(TABLE_INITIALIZATION_SQL_FILE);
29+
30+
try (Connection connection = dataSource.getConnection()) {
31+
Statement statement = connection.createStatement();
32+
statement.execute(createTablesSql);
33+
}
34+
}
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
3+
WallStreet database should store information about brokers, sales groups and its relations.
4+
5+
Each broker must have a unique username. First and last names are also mandatory.
6+
7+
A sales group is a special group that has its own restrictions. Sale groups are used to organize the work of brokers.
8+
Each group mush have a unique name, transaction type (string), and max transaction amount (a number). All field are
9+
mandatory.
10+
11+
A sales group can consists of more than one broker, while each broker can be associated with more than one sale group.
12+
13+
TECH NOTES AND NAMING CONVENTION
14+
- All tables, columns and constraints are named using "snake case" naming convention
15+
- All table names must be singular (e.g. "user", not "users")
16+
- All tables (except link tables) should have an id of type BIGINT, which is a primary key.
17+
- All primary key, foreign key, and unique constraint should be named according to the naming convention.
18+
- All link tables should have a composite key that consists of two foreign key columns
19+
20+
- All primary keys should be named according to the following rule "PK_table_name"
21+
- All foreign keys should be named according to the following rule "FK_table_name_reference_table_name"
22+
- All alternative keys (unique) should be named according to the following rule "UQ_table_name_column_name"
23+
If the key is composite (e.g. consists of two columns), the name should list all column names.
24+
E.g. "UQ_table_name_column1_name_column2_name"
25+
26+
*/
27+
28+
-- TODO: write SQL script to create a database tables according to the requirements

0 commit comments

Comments
 (0)