Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 45 additions & 41 deletions .github/workflows/sampledata-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,76 @@ name: sampledata service ci

on:
push:
branches: ["main"]
branches: ['main']
paths:
- "sampledata/**"
- ".github/workflows/actions/action.yaml"
- ".github/workflows/sampledata-ci.yaml"
- "pom.xml"
- 'sampledata/**'
- '.github/workflows/actions/action.yaml'
- '.github/workflows/sampledata-ci.yaml'
- 'pom.xml'
pull_request:
branches: ["main"]
branches: ['main']
paths:
- "sampledata/**"
- ".github/workflows/actions/action.yaml"
- ".github/workflows/sampledata-ci.yaml"
- "pom.xml"
- 'sampledata/**'
- '.github/workflows/actions/action.yaml'
- '.github/workflows/sampledata-ci.yaml'
- 'pom.xml'
workflow_dispatch:

jobs:
Build:
Test:
runs-on: ubuntu-latest
env:
FROM_ORIGINAL_REPOSITORY: ${{ github.event.pull_request.head.repo.full_name == github.repository || github.ref == 'refs/heads/main' }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis

- uses: ./.github/workflows/actions
- name: Run Maven Build Command

- name: Run tests and generate coverage report
run: mvn clean install -pl sampledata -am

- name: Run Maven Checkstyle
if: ${{ env.FROM_ORIGINAL_REPOSITORY == 'true' }}
run: mvn checkstyle:checkstyle -pl sampledata -am -Dcheckstyle.output.file=sampledata-checkstyle-result.xml

- name: Upload Checkstyle Result
if: ${{ env.FROM_ORIGINAL_REPOSITORY == 'true' }}
uses: jwgmeligmeyling/checkstyle-github-action@master
with:
path: '**/sampledata-checkstyle-result.xml'

- name: Upload test results
uses: dorny/test-reporter@v1
if: ${{ env.FROM_ORIGINAL_REPOSITORY == 'true' && (success() || failure()) }}
with:
name: Sampledata-Service-Unit-Test-Results
path: 'sampledata/**/*-reports/TEST*.xml'
reporter: java-junit

- name: Add coverage report to PR
uses: madrapps/jacoco-report@v1.6.1
if: ${{ env.FROM_ORIGINAL_REPOSITORY == 'true' }}
with:
paths: ${{github.workspace}}/sampledata/target/site/jacoco/jacoco.xml
token: ${{secrets.GITHUB_TOKEN}}
min-coverage-overall: 70
min-coverage-changed-files: 70
title: 'Sampledata Coverage Report'
update-comment: true

- name: Analyze with sonar cloud
if: ${{ env.FROM_ORIGINAL_REPOSITORY == 'true' }}
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: mvn org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -f sampledata
- name: OWASP Dependency Check
if: ${{ env.FROM_ORIGINAL_REPOSITORY == 'true' }}
uses: dependency-check/Dependency-Check_Action@main
env:
JAVA_HOME: /opt/jdk
with:
project: 'yas'
path: '.'
format: 'HTML'
- name: Upload OWASP Dependency Check results
if: ${{ env.FROM_ORIGINAL_REPOSITORY == 'true' }}
uses: actions/upload-artifact@master
with:
name: OWASP Dependency Check Report
path: ${{github.workspace}}/reports
- name: Log in to the Container registry
if: ${{ github.ref == 'refs/heads/main' }}
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push Docker images
if: ${{ github.ref == 'refs/heads/main' }}
uses: docker/build-push-action@v6
with:
context: ./sampledata
push: true
tags: ghcr.io/nashtech-garage/yas-sampledata:latest

Build:
runs-on: ubuntu-latest
needs: Test
steps:
- uses: actions/checkout@v4
- uses: ./.github/workflows/actions
- name: Build artifact
run: mvn clean install -pl sampledata -am -DskipTests
4 changes: 2 additions & 2 deletions sampledata/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
<description>YAS sampledata service</description>

<properties>
<sonar.organization>nashtech-garage</sonar.organization>
<sonar.organization>longtoz</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
<sonar.projectKey>nashtech-garage_yas-sampledata</sonar.projectKey>
<sonar.projectKey>longtoz_yas-sampledata</sonar.projectKey>
</properties>
<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.yas.sampledata;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

class SampledataJacocoSmokeTest {

@Test
void jacocoSmokeTest() {
assertTrue(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.yas.sampledata.config;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.util.ReflectionTestUtils;

class DataSourceConfigTest {

@Test
void dataSources_andJdbcTemplates_areCreated() {
DataSourceConfig config = new DataSourceConfig();
ReflectionTestUtils.setField(config, "driverClassName", "org.h2.Driver");
ReflectionTestUtils.setField(config, "username", "sa");
ReflectionTestUtils.setField(config, "password", "");
ReflectionTestUtils.setField(config, "productUrl", "jdbc:h2:mem:sampledata_product;DB_CLOSE_DELAY=-1");
ReflectionTestUtils.setField(config, "mediaUrl", "jdbc:h2:mem:sampledata_media;DB_CLOSE_DELAY=-1");

DataSource product = config.productDataSource();
DataSource media = config.mediaDataSource();
JdbcTemplate productJdbc = config.jdbcProduct(product);
JdbcTemplate mediaJdbc = config.jdbcMedia(media);

assertNotNull(product);
assertNotNull(media);
assertNotNull(productJdbc);
assertNotNull(mediaJdbc);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.yas.sampledata.config;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.api.Test;

class RestClientConfigTest {

@Test
void restClient_isCreated() {
RestClientConfig config = new RestClientConfig();

assertNotNull(config.restClient());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.yas.sampledata.config;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;

class SecurityConfigTest {

@Test
void jwtAuthenticationConverterForKeycloak_mapsRolesToAuthorities() {
SecurityConfig config = new SecurityConfig();
var converter = config.jwtAuthenticationConverterForKeycloak();

Jwt jwt = Jwt.withTokenValue("token")
.header("alg", "none")
.claim("realm_access", Map.of("roles", List.of("ADMIN", "USER")))
.subject("user-1")
.build();

JwtAuthenticationToken auth = (JwtAuthenticationToken) converter.convert(jwt);

assertEquals("user-1", auth.getName());
assertTrue(auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN")));
assertTrue(auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_USER")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.yas.sampledata.config;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.security.SecurityScheme;
import org.junit.jupiter.api.Test;

class SwaggerConfigTest {

@Test
void annotations_arePresent() {
OpenAPIDefinition openApi = SwaggerConfig.class.getAnnotation(OpenAPIDefinition.class);
SecurityScheme securityScheme = SwaggerConfig.class.getAnnotation(SecurityScheme.class);

assertNotNull(openApi);
assertNotNull(securityScheme);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.yas.sampledata.controller;

import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.yas.sampledata.service.SampleDataService;
import com.yas.sampledata.viewmodel.SampleDataVm;
import org.junit.jupiter.api.Test;

class SampleDataControllerTest {

@Test
void createSampleData_delegatesToService() {
SampleDataService service = mock(SampleDataService.class);
SampleDataVm expected = new SampleDataVm("ok");
when(service.createSampleData()).thenReturn(expected);
SampleDataController controller = new SampleDataController(service);

SampleDataVm result = controller.createSampleData(new SampleDataVm("ignored"));

assertSame(expected, result);
verify(service).createSampleData();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.yas.sampledata.service;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.yas.sampledata.viewmodel.SampleDataVm;
import javax.sql.DataSource;
import org.h2.jdbcx.JdbcDataSource;
import org.junit.jupiter.api.Test;

class SampleDataServiceTest {

@Test
void createSampleData_returnsSuccessMessage() {
DataSource dataSource = createDataSource("jdbc:h2:mem:sampledata_service;DB_CLOSE_DELAY=-1");
SampleDataService service = new SampleDataService(dataSource, dataSource);

SampleDataVm result = service.createSampleData();

assertEquals("Insert Sample Data successfully!", result.message());
}

private DataSource createDataSource(String url) {
JdbcDataSource dataSource = new JdbcDataSource();
dataSource.setURL(url);
dataSource.setUser("sa");
dataSource.setPassword("");
return dataSource;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.yas.sampledata.utils;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

class MessagesUtilsTest {

@Test
void getMessage_returnsKeyWhenMissing() {
String message = MessagesUtils.getMessage("UNKNOWN_CODE");

assertEquals("UNKNOWN_CODE", message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.yas.sampledata.utils;

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.sql.Connection;
import java.sql.ResultSet;
import javax.sql.DataSource;
import org.h2.jdbcx.JdbcDataSource;
import org.junit.jupiter.api.Test;

class SqlScriptExecutorTest {

@Test
void executeScriptsForSchema_runsSqlScripts() throws Exception {
DataSource dataSource = createDataSource("jdbc:h2:mem:sampledata_utils;DB_CLOSE_DELAY=-1");
SqlScriptExecutor executor = new SqlScriptExecutor();

executor.executeScriptsForSchema(dataSource, "PUBLIC", "classpath*:db/product/*.sql");

assertTrue(tableExists(dataSource, "SAMPLE_PRODUCT"));
}

private DataSource createDataSource(String url) {
JdbcDataSource dataSource = new JdbcDataSource();
dataSource.setURL(url);
dataSource.setUser("sa");
dataSource.setPassword("");
return dataSource;
}

private boolean tableExists(DataSource dataSource, String tableName) throws Exception {
try (Connection connection = dataSource.getConnection()) {
ResultSet tables = connection.getMetaData().getTables(null, "PUBLIC", tableName, null);
return tables.next();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.yas.sampledata.viewmodel;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

class ErrorVmTest {

@Test
void constructor_defaultsFieldErrors() {
ErrorVm vm = new ErrorVm("400", "Bad", "detail");

assertNotNull(vm.fieldErrors());
assertTrue(vm.fieldErrors().isEmpty());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.yas.sampledata.viewmodel;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

class SampleDataVmTest {

@Test
void recordStoresMessage() {
SampleDataVm vm = new SampleDataVm("message");

assertEquals("message", vm.message());
}
}
1 change: 1 addition & 0 deletions sampledata/src/test/resources/db/media/sampledata_test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE IF NOT EXISTS sample_media (id INT PRIMARY KEY);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE IF NOT EXISTS sample_product (id INT PRIMARY KEY);