Skip to content

Commit

Permalink
177 server add resttemplate call to get screenshot from microservice (#…
Browse files Browse the repository at this point in the history
…202)

* Initial service

* Added restTemplate and tests
  • Loading branch information
R-Sandor authored Sep 13, 2024
1 parent 4615b0b commit 8ac9cc5
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions docker-compose-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ services:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
- SCREENSHOT_SERVICE_URL=http://screenshot:8080/
volumes:
- pgdata:/var/lib/postgresql/data
ports:
Expand Down
1 change: 1 addition & 0 deletions docker-compose-staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ services:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
- SCREENSHOT_SERVICE_URL=http://screenshot:8080/
volumes:
- pgdata:/var/lib/postgresql/data
ports:
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ services:
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/findfirst
- SPRING_PROFILES_ACTIVE=dev
- SPRING_MAIL_HOST=mail
- SCREENSHOT_SERVICE_URL=http://screenshot:8080/
volumes:
- screenshots-data:/app/screenshots
screenshot:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public String takeScreenshot(@RequestParam String url) {
String cleanUrl = url.replace("/", "_");
Path filePath = Path.of(screenshotSaveLoc, cleanUrl + System.currentTimeMillis() + ".png");
page.screenshot(new Page.ScreenshotOptions().setPath(filePath));
return "Screenshot saved to: " + filePath;
return filePath.toString();
}
} catch (PlaywrightException e) {
// Handle Playwright specific exceptions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dev.findfirst.core.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class ScreenshotManager {

@Value("${screenshot.service.url:http://localhost:8080}")
private String screenshotServiceUrl;

@Autowired private RestTemplate rest;

public String getScreenshot(String reqUrl) {
String url = screenshotServiceUrl + "/screenshot?url=" + reqUrl;
return rest.getForObject(url, String.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package dev.findfirst.core.service;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.client.RestTemplate;

@ExtendWith(MockitoExtension.class)
public class ScreenshotManagerTest {

@Mock
private RestTemplate restTemplate;

@InjectMocks
private ScreenshotManager screenshotManager = new ScreenshotManager();


@BeforeEach
public void setUp() {
ReflectionTestUtils.setField(screenshotManager, "screenshotServiceUrl", "http://localhost:8080");
}

@Test
public void returnMockedPath() {
var expectedUrl = "https:__findfirst.dev";
Mockito
.when(restTemplate.getForObject(
"http://localhost:8080/screenshot?url=https://findfirst.dev", String.class))
.thenReturn("https:__findfirst.dev");
var pathUrl = screenshotManager.getScreenshot("https://findfirst.dev");

Assertions.assertEquals(pathUrl, expectedUrl);
}

}

0 comments on commit 8ac9cc5

Please sign in to comment.