From 181ac0e31e6ae5f770d9814c8e9a12a938b10b2e Mon Sep 17 00:00:00 2001 From: nimat1 Date: Tue, 3 Dec 2024 11:53:25 +0000 Subject: [PATCH 1/5] code-of-conduct integration test (#143) Create integration test for code-of-conduct --- .../tests/cms/codeofconduct.test.ts | 12 ++++++ .../utils/datafactory/codeofconduct.data.ts | 38 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 integration-test/tests/cms/codeofconduct.test.ts create mode 100644 integration-test/utils/datafactory/codeofconduct.data.ts diff --git a/integration-test/tests/cms/codeofconduct.test.ts b/integration-test/tests/cms/codeofconduct.test.ts new file mode 100644 index 00000000..81626692 --- /dev/null +++ b/integration-test/tests/cms/codeofconduct.test.ts @@ -0,0 +1,12 @@ +import {expect, test} from '@playwright/test'; +import { codeofconductExepctedInformation } from '../../utils/datafactory/codeofconduct.data'; +test('GET /api/cms/v1/code-of-conduct returns correct data', async ({request}) => { + const response = await request.get(`/api/cms/v1/code-of-conduct`); + + expect(response.status()).toBe(200); + const body = await response.json(); + + const expectedResponse = codeofconductExepctedInformation; + + expect(body).toEqual(expectedResponse); + }); diff --git a/integration-test/utils/datafactory/codeofconduct.data.ts b/integration-test/utils/datafactory/codeofconduct.data.ts new file mode 100644 index 00000000..f1f6f910 --- /dev/null +++ b/integration-test/utils/datafactory/codeofconduct.data.ts @@ -0,0 +1,38 @@ +export const codeofconductExepctedInformation = { + "page": { + "title": "Code of Conduct", + "description": "At Women Coding Community we are committed to a vibrant, supportive community where women can network, share experiences, and foster professional relationships, regardless of age, gender, visible or invisible disability, ethnicity, gender expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, sexual orientation or preferred programming language(s)." + }, + "items": [ + { + "title": "Our Standards", + "description": "To ensure a positive experience for all members, we expect participants to exhibit the following behaviors:", + "items": [ + "Respect: Treat everyone with respect, dignity, and empathy. Listen and communicate thoughtfully.", + "Inclusivity: Actively seek to acknowledge and respect the diversity of our community.", + "Collaboration: Share knowledge generously and assist others when possible, fostering a collaborative atmosphere.", + "Constructive Communication: Engage in constructive, positive communication. Provide and gracefully accept constructive criticism.", + "Professionalism: Be professional in all interactions within the community. Avoid behavior or language that may be considered inappropriate or offensive." + ] + }, + { + "title": "Unacceptable Behavior", + "description": "The following behaviors are considered harassment and are unacceptable within our community:", + "items": [ + "Violence, threats of violence, or violent language directed against another member or group.", + "Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.", + "Personal insults, particularly those related to gender, sexual orientation, race, religion, or disability.", + "Inappropriate photography or recording.", + "Advocating for, or encouraging, any of the above behavior." + ] + }, + { + "title": "Enforcement", + "description": "Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful. Instances of abusive, harassing, or otherwise unacceptable behavior can be reported by email. All complaints will be reviewed and investigated promptly and fairly." + }, + { + "title": "Consequences", + "description": "Actions determined to be contrary to these guidelines may result in consequences ranging from a warning to expulsion from the community, future events depending on the severity of the behavior." + } + ] + } From cabea01ce84e17c327108948227dd7e6c8cfa74a Mon Sep 17 00:00:00 2001 From: nimat1 Date: Tue, 3 Dec 2024 14:54:20 +0000 Subject: [PATCH 2/5] test: Events API integration test - Success Case (#144) --------- Co-authored-by: Adriana Zenck Zimmermann --- integration-test/tests/cms/events.test.ts | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 integration-test/tests/cms/events.test.ts diff --git a/integration-test/tests/cms/events.test.ts b/integration-test/tests/cms/events.test.ts new file mode 100644 index 00000000..c7e646c8 --- /dev/null +++ b/integration-test/tests/cms/events.test.ts @@ -0,0 +1,6 @@ +import {expect, test} from '@playwright/test'; + +test('GET /api/cms/v1/events returns success response code', async ({request}) => { + const response = await request.get(`/api/cms/v1/events`); + expect(response.status()).toBe(200); +}); \ No newline at end of file From d3a5a7ef7a12245d7511ddf68b514d656157535d Mon Sep 17 00:00:00 2001 From: Nevena Verbic Date: Tue, 3 Dec 2024 16:54:29 +0100 Subject: [PATCH 3/5] Update README README.md file updated: - Update Run Locally section - Update Deploy with docker compose section - Remove Deploy with docker section --- README.md | 69 ++++++++++++++++++------------------------------------- 1 file changed, 22 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index ac4f9dda..0327e807 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,6 @@ * [Perform SONAR ANALYSIS](#perform-sonar-analysis) * [Deploy application](#deploy-application) * [Deploy with docker compose](#deploy-with-docker-compose) - * [Deploy with docker](#deploy-with-docker) - * [Start docker with remote debug](#start-docker-with-remote-debug) - * [Start docker with mounted data volume](#start-docker-with-mounted-data-volume) * [Helpful commands with docker](#helpful-commands-with-docker) * [Deploy with Fly.io](#deploy-with-flyio) * [Setup Fly.io locally](#setup-flyio-locally) @@ -156,24 +153,24 @@ Once you've done that, restart the IDE. **Note**: Make sure you have docker daemon running locally to be able to run integration test, by execute ``docker ps`` -* Create Jar - -```shell -./gradlew clean bootJar -``` - * Start database ```shell -docker run --pull always --name surrealdb -p 8000:8000 -d surrealdb/surrealdb:latest start +docker run --pull always --name surrealdb -p 8000:8000 -d surrealdb/surrealdb:latest start --user root --pass root --log debug ``` -* Start Spring Boot Application: +* Start Spring Boot Application (from your IDE): ```shell ./gradlew bootRun ``` +**Note**: You can start application in a debug mode through your IDE. + +IntelliJ IDEA: +Select \src\main\java\com\wcc\platform\PlatformApplication.java and mouse right-click to +select debugging option. + * Access application on http://localhost:8080/api/cms/v1/team ## Open API Documentation @@ -273,55 +270,33 @@ Generate token to replace in the project. ### Deploy with docker compose -* build create jar: `./gradlew clean bootJar` -* Start docker compose - -```shell -docker compose -f docker/docker-compose.yml up --build -``` - -* Stop docker compose +* Build and create jar ```shell -cd docker -docker compose down -``` + ./gradlew clean bootJar` + ``` -### Deploy with docker - -* build create jar: `./gradlew clean bootJar` - -* start app via docker +* Start docker compose ```shell -docker build -t wcc-backend . -docker run -d -p 8080:8080 --name wcc-backend-container wcc-backend +docker compose -f docker/docker-compose.yml up --build ``` -#### Start docker with remote debug +**Note**: This will create two Docker instances in your Docker desktop: -```shell -docker build -t wcc-backend . -docker run -p 8080:8080 -p 5005:5005 --name wcc-backend-container wcc-backend -``` +1. surrealdb +2. springboot-app -#### Start docker with mounted data volume +* Debug application -If you are running your Spring Boot application inside a Docker container, ensure that the external -directory path is accessible from within the container. +To debug application STOP the docker container of the application, springboot-app. Do not stop the +container of the SurrealDB. Start the application from your IDE. -* Windows: - -```shell - docker run -v ${PWD}/data:/app/data -d -p 8080:8080 --name wcc-backend-container wcc-backend - -``` - -* Linux: +* Stop docker compose ```shell - docker run -v $(pwd)/data:/app/data -d -p 8080:8080 --name wcc-backend-container wcc-backend - +cd docker +docker compose down ``` #### Helpful commands with docker From cccd61363ff5758d5375f7c7d5b2427c1f7a3a2b Mon Sep 17 00:00:00 2001 From: nimat1 Date: Fri, 6 Dec 2024 21:41:40 +0000 Subject: [PATCH 4/5] Integration test mentorship overview (#155) --- .../tests/cms/mentorshipoverview.test.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 integration-test/tests/cms/mentorshipoverview.test.ts diff --git a/integration-test/tests/cms/mentorshipoverview.test.ts b/integration-test/tests/cms/mentorshipoverview.test.ts new file mode 100644 index 00000000..f1ad20d1 --- /dev/null +++ b/integration-test/tests/cms/mentorshipoverview.test.ts @@ -0,0 +1,12 @@ +import {expect, test} from '@playwright/test'; +test('GET /api/cms/v1/mentorship/overview returns correct data', async ({request}) => { +const response = await request.get('/api/cms/v1/mentorship/overview'); +expect(response.status()).toBe(200); +const data = await response.json(); +expect(data.page).toBeDefined(); +expect(data.page.title).toBe('Mentorship Programme'); +expect(data.mentorSection).toBeDefined(); +expect(data.mentorSection.title).toBe('Become a Mentor'); +expect(data.menteeSection).toBeDefined(); +expect(data.menteeSection.title).toBe('Become a Mentee'); +}); From 6f71db17cd8ba761580332508b31c91ba2279bd6 Mon Sep 17 00:00:00 2001 From: Adriana Zenck Zimmermann Date: Sat, 7 Dec 2024 17:02:55 +0100 Subject: [PATCH 5/5] feat: Adjust repository to create single table for pages (#129) Pages should have single table with content as string The content will be json string and the service should take care of serialization and de-serialization --- .../configuration/RepositoryConfig.java | 13 ++----- .../controller/platform/PageController.java | 20 ++++++++++ .../domain/cms/pages/LandingPage.java | 3 +- .../exceptions/PlatformInternalException.java | 7 +++- .../platform/repository/PageRepository.java | 2 +- .../surrealdb/SurrealDbPageRepository.java | 16 ++++---- .../com/wcc/platform/service/CmsService.java | 36 +++++++++++------- .../wcc/platform/service/PlatformService.java | 36 ++++++++++++++---- .../SurrealDbPageRepositoryTest.java | 37 +++++++++---------- .../wcc/platform/service/CmsServiceTest.java | 5 +-- 10 files changed, 108 insertions(+), 67 deletions(-) diff --git a/src/main/java/com/wcc/platform/configuration/RepositoryConfig.java b/src/main/java/com/wcc/platform/configuration/RepositoryConfig.java index e94a26c4..5bc06a1a 100644 --- a/src/main/java/com/wcc/platform/configuration/RepositoryConfig.java +++ b/src/main/java/com/wcc/platform/configuration/RepositoryConfig.java @@ -2,8 +2,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.surrealdb.driver.SyncSurrealDriver; -import com.wcc.platform.domain.cms.pages.FooterPage; -import com.wcc.platform.domain.cms.pages.LandingPage; import com.wcc.platform.repository.MemberRepository; import com.wcc.platform.repository.PageRepository; import com.wcc.platform.repository.ResourceContentRepository; @@ -24,14 +22,9 @@ public ResourceContentRepository getResourceRepository(final SyncSurrealDriver d return new SurrealDbResourceRepository(driver); } - @Bean(name = "footerRepository") - public PageRepository getFooterPageRepository(final SyncSurrealDriver driver) { - return new SurrealDbPageRepository<>(driver, FooterPage.class); - } - - @Bean(name = "landingPageRepository") - public PageRepository getLandingPageRepository(final SyncSurrealDriver driver) { - return new SurrealDbPageRepository<>(driver, LandingPage.class); + @Bean + public PageRepository getPageRepository(final SyncSurrealDriver driver) { + return new SurrealDbPageRepository(driver); } /** Create FileMemberRepository bean. */ diff --git a/src/main/java/com/wcc/platform/controller/platform/PageController.java b/src/main/java/com/wcc/platform/controller/platform/PageController.java index bd697b2f..6ebb854e 100644 --- a/src/main/java/com/wcc/platform/controller/platform/PageController.java +++ b/src/main/java/com/wcc/platform/controller/platform/PageController.java @@ -5,12 +5,16 @@ import com.wcc.platform.service.PlatformService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; +import java.util.Collection; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; @@ -45,4 +49,20 @@ public ResponseEntity savePage(@RequestBody final FooterPage footerPage) public ResponseEntity saveLandingPage(@RequestBody final LandingPage page) { return ResponseEntity.ok(service.savePage(page)); } + + /** Delete Page By ID. */ + @DeleteMapping + @Operation(summary = "Delete page by id") + public ResponseEntity deletePage(@RequestParam(name = "id") final String pageId) { + service.deletePageById(pageId); + return ResponseEntity.noContent().build(); + } + + /** Get all pages content. */ + @GetMapping + @Operation(summary = "Get all pages") + @ResponseStatus(HttpStatus.OK) + public ResponseEntity> getAllResources() { + return ResponseEntity.ok(service.getAllPages()); + } } diff --git a/src/main/java/com/wcc/platform/domain/cms/pages/LandingPage.java b/src/main/java/com/wcc/platform/domain/cms/pages/LandingPage.java index 4edc30cd..c02ea3f9 100644 --- a/src/main/java/com/wcc/platform/domain/cms/pages/LandingPage.java +++ b/src/main/java/com/wcc/platform/domain/cms/pages/LandingPage.java @@ -1,6 +1,5 @@ package com.wcc.platform.domain.cms.pages; -import com.fasterxml.jackson.annotation.JsonIgnore; import com.wcc.platform.domain.cms.pages.programme.ProgrammeItem; import com.wcc.platform.domain.platform.Event; import jakarta.validation.constraints.NotNull; @@ -19,7 +18,7 @@ @AllArgsConstructor @Builder public class LandingPage { - @JsonIgnore private String id; + @NotNull private String id; @NotNull private Page heroSection; @NotNull private Page fullBannerSection; @NotNull private Section programmes; diff --git a/src/main/java/com/wcc/platform/domain/exceptions/PlatformInternalException.java b/src/main/java/com/wcc/platform/domain/exceptions/PlatformInternalException.java index 688c69c6..901d492e 100644 --- a/src/main/java/com/wcc/platform/domain/exceptions/PlatformInternalException.java +++ b/src/main/java/com/wcc/platform/domain/exceptions/PlatformInternalException.java @@ -1,9 +1,14 @@ package com.wcc.platform.domain.exceptions; +import com.wcc.platform.domain.cms.PageType; + /** Platform generic exception. */ public class PlatformInternalException extends RuntimeException { - public PlatformInternalException(final String message, final Throwable cause) { super(message, cause); } + + public PlatformInternalException(final PageType pageType, final Throwable cause) { + super("Invalid Page type " + pageType, cause); + } } diff --git a/src/main/java/com/wcc/platform/repository/PageRepository.java b/src/main/java/com/wcc/platform/repository/PageRepository.java index e297883e..f3cebd1d 100644 --- a/src/main/java/com/wcc/platform/repository/PageRepository.java +++ b/src/main/java/com/wcc/platform/repository/PageRepository.java @@ -1,4 +1,4 @@ package com.wcc.platform.repository; /** Generic page repository interface. */ -public interface PageRepository extends CrudRepository {} +public interface PageRepository extends CrudRepository {} diff --git a/src/main/java/com/wcc/platform/repository/surrealdb/SurrealDbPageRepository.java b/src/main/java/com/wcc/platform/repository/surrealdb/SurrealDbPageRepository.java index 0df74d89..7f81c847 100644 --- a/src/main/java/com/wcc/platform/repository/surrealdb/SurrealDbPageRepository.java +++ b/src/main/java/com/wcc/platform/repository/surrealdb/SurrealDbPageRepository.java @@ -7,33 +7,31 @@ import java.util.Optional; /** SurrealDB repository implementation for page. */ -public class SurrealDbPageRepository implements PageRepository { +public class SurrealDbPageRepository implements PageRepository { /* default */ static final String TABLE = "page"; private final SyncSurrealDriver driver; - private final Class entityType; - public SurrealDbPageRepository(final SyncSurrealDriver driver, final Class entityType) { + public SurrealDbPageRepository(final SyncSurrealDriver driver) { this.driver = driver; - this.entityType = entityType; } @Override - public T save(final T entity) { + public String save(final String entity) { return driver.create(TABLE, entity); } @Override - public Collection findAll() { - return driver.select(TABLE, entityType); + public Collection findAll() { + return driver.select(TABLE, String.class); } @Override - public Optional findById(final String id) { + public Optional findById(final String id) { final var key = TABLE + ":" + id; final var query = - driver.query("SELECT * FROM " + TABLE + " WHERE id = $id", Map.of("id", key), entityType); + driver.query("SELECT * FROM " + TABLE + " WHERE id = $id", Map.of("id", key), String.class); if (query.isEmpty()) { return Optional.empty(); diff --git a/src/main/java/com/wcc/platform/service/CmsService.java b/src/main/java/com/wcc/platform/service/CmsService.java index 0b278ee7..536f79a6 100644 --- a/src/main/java/com/wcc/platform/service/CmsService.java +++ b/src/main/java/com/wcc/platform/service/CmsService.java @@ -13,25 +13,19 @@ import com.wcc.platform.repository.PageRepository; import com.wcc.platform.utils.FileUtil; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; /** CMS service responsible for simple pages. */ @Service public class CmsService { private final ObjectMapper objectMapper; - private final PageRepository footerRepository; - private final PageRepository lpRepository; + private final PageRepository pageRepository; /** Init repositories with respective qualifiers. */ @Autowired - public CmsService( - final ObjectMapper objectMapper, - @Qualifier("footerRepository") final PageRepository footerRepository, - @Qualifier("landingPageRepository") final PageRepository lpRepository) { + public CmsService(final ObjectMapper objectMapper, final PageRepository pageRepository) { this.objectMapper = objectMapper; - this.footerRepository = footerRepository; - this.lpRepository = lpRepository; + this.pageRepository = pageRepository; } /** @@ -54,8 +48,17 @@ public TeamPage getTeam() { * @return Footer page */ public FooterPage getFooter() { - final var page = footerRepository.findById(PageType.FOOTER.name()); - return page.orElseGet(this::getFooterFallback); + final var page = pageRepository.findById(PageType.FOOTER.name()); + + if (page.isPresent()) { + try { + return objectMapper.readValue(page.get(), FooterPage.class); + } catch (JsonProcessingException e) { + throw new PlatformInternalException(e.getMessage(), e); + } + } + + return getFooterFallback(); } private FooterPage getFooterFallback() { @@ -73,9 +76,16 @@ private FooterPage getFooterFallback() { * @return Landing page of the community. */ public LandingPage getLandingPage() { - final var page = lpRepository.findById(PageType.LANDING_PAGE.name()); + final var page = pageRepository.findById(PageType.LANDING_PAGE.name()); + if (page.isPresent()) { + try { + return objectMapper.readValue(page.get(), LandingPage.class); + } catch (JsonProcessingException e) { + throw new PlatformInternalException(e.getMessage(), e); + } + } - return page.orElseGet(this::getLandingPageFallback); + return getLandingPageFallback(); } private LandingPage getLandingPageFallback() { diff --git a/src/main/java/com/wcc/platform/service/PlatformService.java b/src/main/java/com/wcc/platform/service/PlatformService.java index 40bff3af..ec8cc995 100644 --- a/src/main/java/com/wcc/platform/service/PlatformService.java +++ b/src/main/java/com/wcc/platform/service/PlatformService.java @@ -1,10 +1,14 @@ package com.wcc.platform.service; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.wcc.platform.domain.cms.PageType; import com.wcc.platform.domain.cms.pages.FooterPage; import com.wcc.platform.domain.cms.pages.LandingPage; import com.wcc.platform.domain.exceptions.ContentNotFoundException; import com.wcc.platform.domain.exceptions.DuplicatedMemberException; import com.wcc.platform.domain.exceptions.MemberNotFoundException; +import com.wcc.platform.domain.exceptions.PlatformInternalException; import com.wcc.platform.domain.platform.Member; import com.wcc.platform.domain.platform.MemberDto; import com.wcc.platform.domain.platform.ResourceContent; @@ -23,21 +27,21 @@ public class PlatformService { private final ResourceContentRepository resource; - private final PageRepository footerRepository; - private final PageRepository lpRepository; + private final PageRepository pageRepository; private final MemberRepository memberRepository; + private final ObjectMapper objectMapper; /** Constructor . */ @Autowired public PlatformService( @Qualifier("getResourceRepository") final ResourceContentRepository resource, final MemberRepository memberRepository, - @Qualifier("footerRepository") final PageRepository footerRepository, - @Qualifier("landingPageRepository") final PageRepository lpRepository) { + final PageRepository pageRepository, + final ObjectMapper objectMapper) { this.resource = resource; this.memberRepository = memberRepository; - this.footerRepository = footerRepository; - this.lpRepository = lpRepository; + this.pageRepository = pageRepository; + this.objectMapper = objectMapper; } public ResourceContent saveResourceContent(final ResourceContent resourceContent) { @@ -46,12 +50,24 @@ public ResourceContent saveResourceContent(final ResourceContent resourceContent /** Save any type of page based on page Type. */ public Object savePage(final LandingPage page) { - return lpRepository.save(page); + try { + return pageRepository.save(objectMapper.writeValueAsString(page)); + } catch (JsonProcessingException e) { + throw new PlatformInternalException(PageType.LANDING_PAGE, e); + } } /** Save any type of page based on page Type. */ public Object savePage(final FooterPage page) { - return footerRepository.save(page); + try { + return pageRepository.save(objectMapper.writeValueAsString(page)); + } catch (JsonProcessingException e) { + throw new PlatformInternalException(PageType.FOOTER, e); + } + } + + public Collection getAllPages() { + return pageRepository.findAll(); } public Collection getAllResources() { @@ -81,6 +97,10 @@ public void deleteById(final String id) { resource.deleteById(result.getId()); } + public void deletePageById(final String id) { + pageRepository.findById(id).ifPresent(pageRepository::deleteById); + } + /** Save Member into storage. */ public Member createMember(final Member member) { final Optional memberOptional = emailExists(member.getEmail()); diff --git a/src/test/java/com/wcc/platform/repository/surrealdb/SurrealDbPageRepositoryTest.java b/src/test/java/com/wcc/platform/repository/surrealdb/SurrealDbPageRepositoryTest.java index fbcdcf16..de6c0658 100644 --- a/src/test/java/com/wcc/platform/repository/surrealdb/SurrealDbPageRepositoryTest.java +++ b/src/test/java/com/wcc/platform/repository/surrealdb/SurrealDbPageRepositoryTest.java @@ -1,6 +1,5 @@ package com.wcc.platform.repository.surrealdb; -import static com.wcc.platform.factories.SetupFactories.createFooterPageTest; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.anyMap; @@ -12,10 +11,9 @@ import com.surrealdb.driver.SyncSurrealDriver; import com.wcc.platform.domain.cms.PageType; -import com.wcc.platform.domain.cms.pages.FooterPage; import java.util.Collection; +import java.util.Collections; import java.util.List; -import java.util.Optional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mock; @@ -24,56 +22,55 @@ class SurrealDbPageRepositoryTest { private static final String TABLE = SurrealDbPageRepository.TABLE; + private static final String PAGE = "{ 'page': { 'title': 'title 1' } }"; @Mock private SyncSurrealDriver mockDriver; - private SurrealDbPageRepository repository; + private SurrealDbPageRepository repository; @BeforeEach void setUp() { MockitoAnnotations.openMocks(this); - repository = new SurrealDbPageRepository<>(mockDriver, FooterPage.class); + repository = new SurrealDbPageRepository(mockDriver); } @Test void testSave() { - FooterPage entity = createFooterPageTest(); - when(mockDriver.create(TABLE, entity)).thenReturn(entity); + when(mockDriver.create(TABLE, PAGE)).thenReturn(PAGE); - var savedEntity = repository.save(entity); + var savedEntity = repository.save(PAGE); - verify(mockDriver, times(1)).create(TABLE, entity); - assertEquals(entity, savedEntity); + verify(mockDriver, times(1)).create(TABLE, PAGE); + assertEquals(PAGE, savedEntity); } @Test void testFindAll() { - List mockResult = List.of(createFooterPageTest()); + List mockResult = Collections.singletonList(PAGE); + when(mockDriver.select(TABLE, String.class)).thenReturn(mockResult); - when(mockDriver.select(TABLE, FooterPage.class)).thenReturn(mockResult); + Collection result = repository.findAll(); - Collection result = repository.findAll(); - - verify(mockDriver, times(1)).select(TABLE, FooterPage.class); + verify(mockDriver, times(1)).select(TABLE, String.class); assertEquals(mockResult, result); } @Test void testFindByIdNotFoundCase1() { - Optional result = repository.findById(PageType.FOOTER.name()); + var result = repository.findById(PageType.FOOTER.name()); - verify(mockDriver, times(1)).query(anyString(), anyMap(), eq(FooterPage.class)); + verify(mockDriver, times(1)).query(anyString(), anyMap(), eq(String.class)); assertTrue(result.isEmpty()); } @Test void testFindByIdNotFoundCase2() { - when(mockDriver.query(anyString(), anyMap(), eq(FooterPage.class))).thenReturn(List.of()); + when(mockDriver.query(anyString(), anyMap(), eq(String.class))).thenReturn(List.of()); - Optional result = repository.findById("test-id-2"); + var result = repository.findById("test-id-2"); - verify(mockDriver, times(1)).query(anyString(), anyMap(), eq(FooterPage.class)); + verify(mockDriver, times(1)).query(anyString(), anyMap(), eq(String.class)); assertTrue(result.isEmpty()); } diff --git a/src/test/java/com/wcc/platform/service/CmsServiceTest.java b/src/test/java/com/wcc/platform/service/CmsServiceTest.java index 557df21f..a9e7079a 100644 --- a/src/test/java/com/wcc/platform/service/CmsServiceTest.java +++ b/src/test/java/com/wcc/platform/service/CmsServiceTest.java @@ -23,8 +23,7 @@ import org.mockito.MockitoAnnotations; class CmsServiceTest { - @Mock private PageRepository footerRepository; - @Mock private PageRepository landingPageRepository; + @Mock private PageRepository pageRepository; @Mock private ObjectMapper objectMapper; private CmsService service; @@ -33,7 +32,7 @@ class CmsServiceTest { void setUp() { MockitoAnnotations.openMocks(this); - service = new CmsService(objectMapper, footerRepository, landingPageRepository); + service = new CmsService(objectMapper, pageRepository); } @Test