Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into issues/merge-ups…
Browse files Browse the repository at this point in the history
…tream
  • Loading branch information
banterCZ committed Nov 6, 2023
2 parents 807c901 + c734bdf commit 848d3a4
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 62 deletions.
2 changes: 0 additions & 2 deletions docs-private/Developer-How-To-Start.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

### Standalone Run

- Enable maven profile `standalone`
- Use IntelliJ Idea run configuration at `../.run/EnrollmentServerApplication.run.xml`
- Open [http://localhost:8081/enrollment-server/actuator/health](http://localhost:8081/enrollment-server/actuator/health) and you should get `{"status":"UP"}`

Expand Down Expand Up @@ -59,7 +58,6 @@ docker run -p 80:8080 -e ENROLLMENT_SERVER_DATASOURCE_URL='jdbc:postgresql://hos

### Standalone Run

- Enable maven profile `standalone`
- Use IntelliJ Idea run configuration at `../.run/EnrollmentServerOnboardingApplication.run.xml`
- Open [http://localhost:8083/enrollment-server-onboarding/actuator/health](http://localhost:8083/enrollment-server-onboarding/actuator/health) and you should get `{"status":"UP"}`

Expand Down
2 changes: 1 addition & 1 deletion docs/onboarding/Deploying-Wildfly.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Enrollment Server contains the following configuration in `jboss-deployment-stru
<resources>
<!-- use WAR provided Bouncy Castle -->
<resource-root path="WEB-INF/lib/bcprov-jdk18on-1.72.jar" use-physical-code-source="true"/>
<resource-root path="WEB-INF/lib/bcprov-jdk18on-${BC_VERSION}.jar" use-physical-code-source="true"/>
</resources>
<dependencies>
Expand Down
41 changes: 25 additions & 16 deletions enrollment-server-onboarding/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,18 @@
</goals>
</execution>
</executions>
<configuration>
<excludes>
<exclude>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
</exclude>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down Expand Up @@ -244,6 +256,19 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<webResource>
<directory>src/main/webapp/WEB-INF</directory>
<targetPath>WEB-INF</targetPath>
<filtering>true</filtering>
</webResource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>

Expand All @@ -257,22 +282,6 @@
<additionalparam>-Xdoclint:none</additionalparam>
</properties>
</profile>
<profile>
<id>standalone</id>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>public-repository</id>
<activation>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<resources>
<!-- use WAR provided Bouncy Castle -->
<resource-root path="WEB-INF/lib/bcprov-jdk18on-1.72.jar" use-physical-code-source="true"/>
<resource-root path="WEB-INF/lib/bcprov-jdk18on-${bcprov-jdk18on.version}.jar" use-physical-code-source="true"/>
</resources>

<dependencies>
Expand Down
48 changes: 25 additions & 23 deletions enrollment-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,6 @@
<artifactId>postgresql</artifactId>
</dependency>

<!-- Other Dependencies -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.10.0</version>
</dependency>

<!-- Logging -->
<dependency>
<groupId>net.logstash.logback</groupId>
Expand Down Expand Up @@ -181,6 +174,18 @@
</goals>
</execution>
</executions>
<configuration>
<excludes>
<exclude>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
</exclude>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -190,6 +195,19 @@
<excludedGroups>external-service</excludedGroups>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<webResource>
<directory>src/main/webapp/WEB-INF</directory>
<targetPath>WEB-INF</targetPath>
<filtering>true</filtering>
</webResource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>

Expand All @@ -203,22 +221,6 @@
<additionalparam>-Xdoclint:none</additionalparam>
</properties>
</profile>
<profile>
<id>standalone</id>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>public-repository</id>
<activation>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,15 @@ public interface OperationTemplateRepository extends CrudRepository<OperationTem
*/
Optional<OperationTemplateEntity> findFirstByLanguageAndPlaceholder(String language, String placeholder);

/**
* Find an operation template by the given operation type.
* <p>
* Just a fallback method when no entry found by {@link #findFirstByLanguageAndPlaceholder(String, String)}.
*
* @param placeholder operation type
* @return operation template or empty
* @see #findFirstByLanguageAndPlaceholder(String, String)
*/
Optional<OperationTemplateEntity> findFirstByPlaceholder(String placeholder);

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
@Slf4j
public class OperationTemplateService {

private static final String DEFAULT_LANGUAGE = "en";

private final OperationTemplateRepository operationTemplateRepository;

@Autowired
Expand All @@ -44,17 +46,36 @@ public OperationTemplateService(OperationTemplateRepository operationTemplateRep
}

/**
* Find the operation template for the given type and language. Falling back to EN locale.
* Find the operation template for the given type and language.
* <p>
* Falling back to EN locale and later on to any found language.
*
* @param operationType Operation type.
* @param language Template language.
* @return Found operation template or empty.
*/
public Optional<OperationTemplateEntity> findTemplate(@NotNull String operationType, @NotNull String language) {
return operationTemplateRepository.findFirstByLanguageAndPlaceholder(language, operationType).or(() -> {
return operationTemplateRepository.findFirstByLanguageAndPlaceholder(language, operationType).or(() ->
findTemplateFallback(operationType, language));
}

private Optional<OperationTemplateEntity> findTemplateFallback(final String operationType, final String language) {
if (!DEFAULT_LANGUAGE.equals(language)) {
logger.debug("Trying fallback to EN locale for operationType={}", operationType);
return operationTemplateRepository.findFirstByLanguageAndPlaceholder("en", operationType);
});
return findDefaultTemplate(operationType);
} else {
return findAnyTemplate(operationType);
}
}

private Optional<OperationTemplateEntity> findDefaultTemplate(final String operationType) {
return operationTemplateRepository.findFirstByLanguageAndPlaceholder(DEFAULT_LANGUAGE, operationType).or(() ->
findAnyTemplate(operationType));
}

private Optional<OperationTemplateEntity> findAnyTemplate(final String operationType) {
logger.debug("Trying fallback to any locale for operationType={}", operationType);
return operationTemplateRepository.findFirstByPlaceholder(operationType);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<resources>
<!-- use WAR provided Bouncy Castle -->
<resource-root path="WEB-INF/lib/bcprov-jdk18on-1.72.jar" use-physical-code-source="true"/>
<resource-root path="WEB-INF/lib/bcprov-jdk18on-${bcprov-jdk18on.version}.jar" use-physical-code-source="true"/>
</resources>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* PowerAuth Enrollment Server
* Copyright (C) 2023 Wultra s.r.o.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.wultra.app.enrollmentserver.impl.service;

import com.wultra.app.enrollmentserver.database.OperationTemplateRepository;
import com.wultra.app.enrollmentserver.database.entity.OperationTemplateEntity;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
* Test for {@link OperationTemplateService}.
*
* @author Lubos Racansky, [email protected]
*/
@ExtendWith(MockitoExtension.class)
class OperationTemplateServiceTest {

@Mock
private OperationTemplateRepository dao;

@InjectMocks
private OperationTemplateService tested;

@Test
void testFindTemplate_givenLanguage() {
final OperationTemplateEntity entity = new OperationTemplateEntity();
when(dao.findFirstByLanguageAndPlaceholder("cs", "myTemplate"))
.thenReturn(Optional.of(entity));

final Optional<OperationTemplateEntity> result = tested.findTemplate("myTemplate", "cs");

assertTrue(result.isPresent());
assertEquals(entity, result.get());
}

@Test
void testFindTemplate_fallbackToEnglish() {
final OperationTemplateEntity entity = new OperationTemplateEntity();
when(dao.findFirstByLanguageAndPlaceholder("cs", "myTemplate"))
.thenReturn(Optional.empty());
when(dao.findFirstByLanguageAndPlaceholder("en", "myTemplate"))
.thenReturn(Optional.of(entity));

final Optional<OperationTemplateEntity> result = tested.findTemplate("myTemplate", "cs");

assertTrue(result.isPresent());
assertEquals(entity, result.get());
}

@Test
void testFindTemplate_fallbackToAnyLanguage() {
final OperationTemplateEntity entity = new OperationTemplateEntity();
when(dao.findFirstByLanguageAndPlaceholder("cs", "myTemplate"))
.thenReturn(Optional.empty());
when(dao.findFirstByLanguageAndPlaceholder("en", "myTemplate"))
.thenReturn(Optional.empty());
when(dao.findFirstByPlaceholder("myTemplate"))
.thenReturn(Optional.of(entity));

final Optional<OperationTemplateEntity> result = tested.findTemplate("myTemplate", "cs");

assertTrue(result.isPresent());
assertEquals(entity, result.get());
}

@Test
void testFindTemplate_fallbackToAnyLanguage_optimizationOfEnglishLocale() {
final OperationTemplateEntity entity = new OperationTemplateEntity();
when(dao.findFirstByLanguageAndPlaceholder("en", "myTemplate"))
.thenReturn(Optional.empty());
when(dao.findFirstByPlaceholder("myTemplate"))
.thenReturn(Optional.of(entity));

final Optional<OperationTemplateEntity> result = tested.findTemplate("myTemplate", "en");

assertTrue(result.isPresent());
assertEquals(entity, result.get());

verify(dao, times(1)).findFirstByLanguageAndPlaceholder("en", "myTemplate");
}

@Test
void testFindTemplate_notFound() {
when(dao.findFirstByLanguageAndPlaceholder("cs", "myTemplate"))
.thenReturn(Optional.empty());
when(dao.findFirstByLanguageAndPlaceholder("en", "myTemplate"))
.thenReturn(Optional.empty());
when(dao.findFirstByPlaceholder("myTemplate"))
.thenReturn(Optional.empty());

final Optional<OperationTemplateEntity> result = tested.findTemplate("myTemplate", "cs");

assertFalse(result.isPresent());
}
}
Loading

0 comments on commit 848d3a4

Please sign in to comment.