I was a commuter to an open source project [check the branch "290-dbrider-dataset-2"]
- Environment configuration
- Frontend with HTML and Bootstrap
- API implementation
- Verified and resolved bug reports and issues
- Git Version Control to track changes and deal with Git Conflict
🧩 Docker (dockerfile and docker compose)
🧩 Liquibase (connected it to database)
🧩 DBRider customization
POST _api/user/question/{questionId}/answer/{id}/downVote_
In Stack Overflow forum, when a User votes for an Answer, the Reputation of the answer's author decreases by 5 points. API should include a documented feature that returns the Total Vote Count, which is the sum of upvotes and downvotes. However, this functionality is only available to authorized users.
First, I took answer id and question id from Url adding an authorized user - from SecurityContextHolder:
(path: src/main/java/com/javamentor/qa/platform/webapp/controllers/rest/ResourceAnswerController.java)
The interesting thing here is that there are multiple edge cases we have to consider to count the Total Amount of Votes. Such as:
-
User can Votedown only once. Thus, the method voteAnswerExists in ResourceAnswerController validates if the object already exists in database: further, in VoteAnswerService: (path: src/main/java/com/javamentor/qa/platform/service/impl/model/VoteAnswerServiceImpl.java) finally, in a VoteAnswerDao: (path: src/main/java/com/javamentor/qa/platform/dao/impl/model/VoteAnswerDaoImpl.java)
-
User cannot vote for own answer. Therefore, I checked if the User is not an author of the Answer in REST-Controller by implementing the getByIdAndChecked method that queries the information from database: Service layer:
Dao layer: -
The author of the Answer has to be "granted" by -5 points; ergo, his Reputation status should be updated in a few steps:
(path: src/main/java/com/javamentor/qa/platform/service/impl/model/ReputationServiceImpl.java)
__ check if the instance of the Rreputation exists:
__ update points or create a new Reputation:
__ fill in the fields of newReputation in case it was created:
-
To return the Total Amount of Votes we have to compute both down and up votes of the Answer:
-
Besides, we have to check if the Answer and the Question (related to the answer) exist. Here I use EnitityManager in Dao layer to query information from database:
Let us focus on the reputationService.updateCountByDown that takes two parameters: a User who wrote the Answer and an Answer's ID:
DBRider is a Java testing library that facilitates writing integration tests for database-related code
<dependency>
<groupId>com.github.database-rider</groupId>
<artifactId>rider-core</artifactId>
<version>1.35.0</version>
<scope>test</scope>
</dependency>
@DataSet annotation is implemented to configure a DataSet (vs. DBUnit) and is specified with the following feature:
-
strategy = SeedStrategy.INSERT meaning that DBUnit will insert data in tables present on provided data;
-
skipCleaningFor allows to skip cleaning for "db_liquibase" to preserve data that is managed by an external tool - Liquibase;
-
cleanAfter calls a cleaning process after test that will ensure any changes are rolled back to leave the database in its original state;
-
tableOrder defines the order in which tables should be cleaned after the test to ensure child tables will be cleaned before their parents.