Skip to content

Commit

Permalink
Merge pull request #15 from ADPRO-C11/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
asteriskzie authored May 25, 2024
2 parents 0589d28 + a0f92f4 commit e2c1b69
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 154 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
7 changes: 5 additions & 2 deletions .monitoring/prometheus/prometheus.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
scrape_configs:
- job_name: 'MyAppMetrics'
- job_name: 'Snackscription Metrics'
metrics_path: '/actuator/prometheus'
scrape_interval: 3s
static_configs:
- targets: ['host.docker.internal:8080']
labels:
application: 'Snackscription Review'
application: 'Snackscription Review'
- targets: ['34.124.152.90']
labels:
application: 'Snackscription Review (deployed)'
6 changes: 4 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("io.micrometer:micrometer-registry-prometheus")
implementation("io.micrometer:micrometer-core")
compileOnly("org.projectlombok:lombok")
developmentOnly("org.springframework.boot:spring-boot-devtools")
runtimeOnly("org.postgresql:postgresql")
runtimeOnly("io.micrometer:micrometer-registry-prometheus")
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
annotationProcessor("org.projectlombok:lombok")
testImplementation("org.springframework.boot:spring-boot-starter-test")
Expand All @@ -50,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
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 e2c1b69

Please sign in to comment.