Skip to content

Commit

Permalink
Merge pull request #16 from ADPRO-C11/sonar
Browse files Browse the repository at this point in the history
[REFACTOR] Rename variables and improve code readability
  • Loading branch information
asteriskzie authored May 25, 2024
2 parents c4e50d3 + 0bcb63d commit a0f92f4
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 225 deletions.
9 changes: 2 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ name: Java CI Pipeline
on:
push:
branches:
- main
- dev
pull_request:
branches:
- main
- dev
- "**"
workflow_dispatch:

jobs:
Expand Down Expand Up @@ -89,4 +84,4 @@ jobs:
./gradlew jacocoTestReport
env:
PRODUCTION: test
# (Optional) Add steps for generating coverage report and other post-test tasks
# (Optional) Add steps for generating coverage report and other post-test tasks
68 changes: 0 additions & 68 deletions .github/workflows/sonarcloud.yml

This file was deleted.

3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ tasks.jacocoTestReport {
}))
dependsOn(tasks.test)
reports {
xml.required.set(false)
xml.required.set(true)
html.required.set(true)
csv.required.set(false)
html.outputLocation.set(layout.buildDirectory.dir("jacocoHtml"))
}
Expand Down
6 changes: 0 additions & 6 deletions sonar-project.properties

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
public class ReviewController {
private ReviewService reviewService;

public static final String BODY_AUTHOR = "author";
public static final String BODY_CONTENT = "content";
public static final String BODY_RATING = "rating";

public ReviewController(ReviewService reviewService) {
this.reviewService = reviewService;
}
Expand All @@ -27,9 +31,9 @@ public ResponseEntity<String> reviewPage() {
@PostMapping("/subscription-boxes/{subsbox}")
public ResponseEntity<Review> createSubsboxReview(@RequestBody Map<String,String> body, @PathVariable String subsbox) {
try {
String author = body.get("author");
int rating = Integer.parseInt(body.get("rating"));
String content = body.get("content");
String author = body.get(BODY_AUTHOR);
int rating = Integer.parseInt(body.get(BODY_RATING));
String content = body.get(BODY_CONTENT);

Review review = reviewService.createReview(rating, content, subsbox, author);
return new ResponseEntity<>(review, HttpStatus.CREATED);
Expand All @@ -51,7 +55,7 @@ public ResponseEntity<List<Review>> getPublicSubsboxReview(@PathVariable String
@GetMapping("/subscription-boxes/{subsbox}/users/{user}")
public ResponseEntity<Review> getSelfSubsboxReview(@RequestBody Map<String,String> body, @PathVariable String subsbox, @PathVariable String user) {
try {
String sender = body.get("author"); // TODO: nanti pakai JWT token untuk ambil sendernya
String sender = body.get(BODY_AUTHOR);
if (!authenticate(sender, user)) {
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
Expand All @@ -65,13 +69,13 @@ public ResponseEntity<Review> getSelfSubsboxReview(@RequestBody Map<String,Strin
@PutMapping("/subscription-boxes/{subsbox}/users/{user}")
public ResponseEntity<Review> editReview(@RequestBody Map<String,String> body, @PathVariable String subsbox, @PathVariable String user) {
try {
String sender = body.get("author"); // TODO: nanti pakai JWT token untuk ambil sendernya
String sender = body.get(BODY_AUTHOR);
if (!authenticate(sender, user)) {
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}

int rating = Integer.parseInt(body.get("rating"));
String content = body.get("content");
int rating = Integer.parseInt(body.get(BODY_RATING));
String content = body.get(BODY_CONTENT);

Review review = reviewService.editReview(rating, content, subsbox, user);
return new ResponseEntity<>(review, HttpStatus.OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,44 +146,6 @@ public void readSelfSubscriptionBoxReview() throws Exception {
verify(reviewService).getReview(subsbox, author);
}

// @Test
// public void testEditSelfSubscriptionBoxReview() throws Exception {
// Review review = reviews.getFirst();
// String subsboxId = review.getSubsbox();
// String userId = review.getAuthor();
//
// int newRating = 4;
// String newContent = "Awikwok";
// when(reviewService.editReview(newRating, newContent, subsboxId, userId)).thenReturn(new Review(newRating, newContent, userId, subsboxId));
//
// ResultActions result = mockMvc.perform(put("/reviews/subscription-boxes/{subscriptionBoxId}/users/self", subsboxId)
// .contentType(MediaType.APPLICATION_JSON)
// .content("{\"rating\": 4, \"content\": \"Awikwok\", \"userId\": \"user_123\"}"))
// .andExpect(status().isOk())
// .andExpect(jsonPath("$.rating", is(newRating)))
// .andExpect(jsonPath("$.content", is(newContent)))
// .andExpect(jsonPath("$.userId", is(review.getAuthor())))
// .andExpect(jsonPath("$.subscriptionBoxId", is(review.getSubsbox())));
//
// verify(reviewService).editReview(newRating, newContent, subsboxId, userId);
// }
//
// @Test
// public void testDeleteSelfSubscriptionBoxReview() throws Exception {
// Review review = reviews.getFirst();
// String subsboxId = review.getSubsbox();
// String userId = review.getAuthor();
//
// doNothing().when(reviewService).deleteReview(subsboxId, userId);
//
// ResultActions result = mockMvc.perform(delete("/subscription-boxes/{subscriptionBoxId}/users/self", subsboxId)
// .contentType(MediaType.APPLICATION_JSON)
// .content("{\"userId\": \"user_123\"}"))
// .andExpect(status().isNoContent());
//
// verify(reviewService).deleteReview(subsboxId, userId);
// }

@Test
public void testDeleteUserSubscriptionBoxReview() throws Exception {
Review review = reviews.getFirst();
Expand Down Expand Up @@ -238,100 +200,4 @@ public void testRejectReview() throws Exception {

verify(reviewService).approveReview(review.getSubsbox(), review.getAuthor());
}

// @Test
// public void testGetAllSubscriptionBoxReview() {
// String subsboxId = "subsboxId";

// ArrayList<Review> reviews = new ArrayList<>();
// reviews.add(new Review(5, "amazing", "user1", subsboxId));
// reviews.add(new Review(4, "good", "user2", subsboxId));

// when(reviewService.testGetAllSubscriptionBoxReview(subsboxId)).thenReturn(reviews);

// ResultActions result = mockMvc.perform(get("/api/subscription-boxes/{subsboxId}", subsboxId))
// .andExpect(status().isOk())
// .andExpect(jsonPath("$", hasSize(2)))
// .andExpect(jsonPath("$[0].rating", is(5)))
// .andExpect(jsonPath("$[0].content", is("amazing")))
// .andExpect(jsonPath("$[0].userId", is("user1")))
// .andExpect(jsonPath("$[0].subscriptionBoxId", is(subsboxId)))
// .andExpect(jsonPath("$[1].rating", is(4)))
// .andExpect(jsonPath("$[1].content", is("good")))
// .andExpect(jsonPath("$[1].userId", is("user2")))
// .andExpect(jsonPath("$[1].subscriptionBoxId", is(subsboxId)));

// verify(reviewService).testGetAllSubscriptionBoxReview(subsboxId);
// }

// @Test
// public void testGetById() throws Exception {
// Review review = new Review(
// 5, "amazing", "user1", "subsboxId"
// );
// String reviewId = review.getId();

// when(reviewService.findById(reviewId)).thenReturn(review);

// ResultActions result = mockMvc.perform(get("/api/reviews/{reviewId}", reviewId))
// .andExpect(status().isOk())
// .andExpect(jsonPath("$.rating", is(5)))
// .andExpect(jsonPath("$.content", is("amazing")))
// .andExpect(jsonPath("$.userId", is("user1")))
// .andExpect(jsonPath("$.subscriptionBoxId", is("subsboxId")));

// verify(reviewService).findById(reviewId);
// }

// @Test
// public void testGetBySubscriptionBoxId() throws Exception {
// List<Review> curReviews = new ArrayList<>();

// String subscriptionBoxId = this.reviews.getFirst().getSubsbox();
// for (Review review : this.reviews) {
// if (review.getSubsbox().equals(subscriptionBoxId)) {
// curReviews.add(review);
// }
// }

// when(reviewService.findBySubscriptionBoxId(subscriptionBoxId)).thenReturn(curReviews);

// String result = mockMvc.perform(get("/api/subscription-boxes/{subscriptionBoxId}", subscriptionBoxId))
// .andExpect(status().isOk())
// .andExpect(jsonPath("$", hasSize(curReviews.size())))
// .andReturn()
// .getResponse()
// .getContentAsString();

// List<Review> foundReviews = new ArrayList<Review>();
// for (int i=0; i<curReviews.size(); i++) {
// String prefixMatcher = String.format("$[%d]", i);
// int rating = JsonPath.read(result, prefixMatcher + ".rating");
// String content = JsonPath.read(result, prefixMatcher + ".content");
// String userId = JsonPath.read(result, prefixMatcher + ".userId");
// String curSubscriptionBoxId = JsonPath.read(result, prefixMatcher + ".subscriptionBoxId");

// Review review = new Review(rating, content, userId, curSubscriptionBoxId);
// foundReviews.add(review);
// }

// Comparator<Review> cmp = new Comparator<Review>() {
// @Override
// public int compare(Review o1, Review o2) {
// return o1.getAuthor().compareTo(o2.getAuthor());
// }
// };

// curReviews.sort(cmp);
// foundReviews.sort(cmp);

// for (int i=0; i<curReviews.size(); i++) {
// assertEquals(curReviews.get(i).getRating(), foundReviews.get(i).getRating());
// assertEquals(curReviews.get(i).getContent(), foundReviews.get(i).getContent());
// assertEquals(curReviews.get(i).getAuthor(), foundReviews.get(i).getAuthor());
// assertEquals(curReviews.get(i).getSubsbox(), foundReviews.get(i).getSubsbox());
// }

// verify(reviewService).findBySubscriptionBoxId(subscriptionBoxId);
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ public void setUp() {

@Test
public void getReviewsBySubscriptionBoxId() throws Exception {
ReviewService reviewService = new ReviewService(reviewRepo);

List<Review> curReviews = new ArrayList<>();

String subscriptionBoxId = this.reviews.getFirst().getSubsbox();
Expand Down

0 comments on commit a0f92f4

Please sign in to comment.