diff --git a/fhir-odoo/src/main/java/com/ozonehis/fhir/FhirOdooConfig.java b/fhir-odoo/src/main/java/com/ozonehis/fhir/FhirOdooConfig.java index 5a19fd7..a7c100d 100644 --- a/fhir-odoo/src/main/java/com/ozonehis/fhir/FhirOdooConfig.java +++ b/fhir-odoo/src/main/java/com/ozonehis/fhir/FhirOdooConfig.java @@ -26,9 +26,6 @@ public class FhirOdooConfig { @Value("${fhir.odoo.port}") private String OdooPort; - @Value("${fhir.odoo.protocol}") - private String OdooProtocol; - public void validateOdooProperties() { if (StringUtils.isEmpty(OdooHost) || StringUtils.isBlank(OdooHost)) { throw new IllegalArgumentException("OdooHost is required"); @@ -39,18 +36,18 @@ public void validateOdooProperties() { if (StringUtils.isEmpty(OdooPort) || StringUtils.isBlank(OdooPort)) { throw new IllegalArgumentException("OdooPort is required"); } - if (StringUtils.isEmpty(OdooProtocol) || StringUtils.isBlank(OdooProtocol)) { - throw new IllegalArgumentException("OdooProtocol is required"); - } } public OdooXmlRpcProxy.RPCProtocol getRPCProtocol() { - if (OdooProtocol.equalsIgnoreCase("http")) { - return OdooXmlRpcProxy.RPCProtocol.RPC_HTTP; - } else if (OdooProtocol.equalsIgnoreCase("https")) { - return OdooXmlRpcProxy.RPCProtocol.RPC_HTTPS; - } else { - throw new IllegalArgumentException("Invalid OdooProtocol"); - } + // OdooHost is expected to be in the format: http(s):// + return OdooHost.startsWith("https") + ? OdooXmlRpcProxy.RPCProtocol.RPC_HTTPS + : OdooXmlRpcProxy.RPCProtocol.RPC_HTTP; + } + + public String getOdooHostName() { + // OdooHost is expected to be in the format: http(s):// + var hostname = OdooHost.substring(OdooHost.indexOf("://") + 3); + return hostname.endsWith("/") ? hostname.substring(0, hostname.length() - 1) : hostname; } } diff --git a/fhir-odoo/src/main/java/com/ozonehis/fhir/odoo/security/BasicAuthenticationInterceptor.java b/fhir-odoo/src/main/java/com/ozonehis/fhir/odoo/security/BasicAuthenticationInterceptor.java index 66f0480..d597605 100644 --- a/fhir-odoo/src/main/java/com/ozonehis/fhir/odoo/security/BasicAuthenticationInterceptor.java +++ b/fhir-odoo/src/main/java/com/ozonehis/fhir/odoo/security/BasicAuthenticationInterceptor.java @@ -17,9 +17,11 @@ import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Base64; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +@Slf4j @SuppressWarnings("unused") @Component @Interceptor @@ -64,12 +66,11 @@ public boolean authenticate(HttpServletRequest request, HttpServletResponse resp private boolean authenticateWithUsernameAndPassword(String username, String password) { Session odooSession = new Session( fhirOdooConfig.getRPCProtocol(), - fhirOdooConfig.getOdooHost(), + fhirOdooConfig.getOdooHostName(), Integer.parseInt(fhirOdooConfig.getOdooPort()), fhirOdooConfig.getOdooDatabase(), username, password); - try { odooSession.startSession(); if (odooSession.getUserID() != 0) { diff --git a/fhir-odoo/src/main/resources/fhir-odoo.properties b/fhir-odoo/src/main/resources/fhir-odoo.properties index e9d02a1..357dc50 100644 --- a/fhir-odoo/src/main/resources/fhir-odoo.properties +++ b/fhir-odoo/src/main/resources/fhir-odoo.properties @@ -6,14 +6,12 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. # -# The URL of the Odoo server -fhir.odoo.host=${ODOO_HOST:localhost} +# The URL of the Odoo server to connect to, including the protocol (http or https) +# For example: http://localhost or https://odoo.example.com +fhir.odoo.host=${ODOO_HOST:http://localhost} # The port of the Odoo server fhir.odoo.port=${ODOO_PORT:8069} # The name of the database to connect to on the Odoo server fhir.odoo.database=${ODOO_DATABASE:odoo} - -# The protocol to use to connect to the Odoo server -fhir.odoo.protocol=${ODOO_PROTOCOL:RPC_HTTP} diff --git a/fhir-odoo/src/test/java/com/ozonehis/fhir/FhirOdooConfigTest.java b/fhir-odoo/src/test/java/com/ozonehis/fhir/FhirOdooConfigTest.java index f0eda3e..bcd598c 100644 --- a/fhir-odoo/src/test/java/com/ozonehis/fhir/FhirOdooConfigTest.java +++ b/fhir-odoo/src/test/java/com/ozonehis/fhir/FhirOdooConfigTest.java @@ -11,6 +11,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import com.odoojava.api.OdooXmlRpcProxy; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -25,15 +26,13 @@ class FhirOdooConfigTest { @Test @DisplayName("Should validate all properties are not empty") void shouldValidateAllPropertiesAreNotEmpty() { - fhirOdooConfig.setOdooHost("localhost"); + fhirOdooConfig.setOdooHost("http://localhost"); fhirOdooConfig.setOdooDatabase("odoo_db"); fhirOdooConfig.setOdooPort("8069"); - fhirOdooConfig.setOdooProtocol("http"); - assertEquals("localhost", fhirOdooConfig.getOdooHost()); + assertEquals("http://localhost", fhirOdooConfig.getOdooHost()); assertEquals("odoo_db", fhirOdooConfig.getOdooDatabase()); assertEquals("8069", fhirOdooConfig.getOdooPort()); - assertEquals("http", fhirOdooConfig.getOdooProtocol()); assertDoesNotThrow(() -> fhirOdooConfig.validateOdooProperties()); } @@ -43,7 +42,6 @@ void shouldThrowExceptionWhenOdooHostIsEmpty() { fhirOdooConfig.setOdooHost(""); fhirOdooConfig.setOdooDatabase("odoo_db"); fhirOdooConfig.setOdooPort("8069"); - fhirOdooConfig.setOdooProtocol("http"); assertThrows( IllegalArgumentException.class, () -> fhirOdooConfig.validateOdooProperties(), "OdooHost is required"); @@ -52,10 +50,9 @@ void shouldThrowExceptionWhenOdooHostIsEmpty() { @Test @DisplayName("Should throw exception with appropriate message when OdooDatabase is empty") void shouldThrowExceptionWhenOdooDatabaseIsEmpty() { - fhirOdooConfig.setOdooHost("localhost"); + fhirOdooConfig.setOdooHost("http://localhost"); fhirOdooConfig.setOdooDatabase(""); fhirOdooConfig.setOdooPort("8069"); - fhirOdooConfig.setOdooProtocol("http"); assertThrows( IllegalArgumentException.class, @@ -69,23 +66,29 @@ void shouldThrowExceptionWhenOdooPortIsEmpty() { fhirOdooConfig.setOdooHost("localhost"); fhirOdooConfig.setOdooDatabase("odoo_db"); fhirOdooConfig.setOdooPort(""); - fhirOdooConfig.setOdooProtocol("http"); assertThrows( IllegalArgumentException.class, () -> fhirOdooConfig.validateOdooProperties(), "OdooPort is required"); } @Test - @DisplayName("Should throw exception with appropriate when OdooProtocol is empty") - void shouldThrowExceptionWhenOdooProtocolIsEmpty() { - fhirOdooConfig.setOdooHost("localhost"); - fhirOdooConfig.setOdooDatabase("odoo_db"); - fhirOdooConfig.setOdooPort("8069"); - fhirOdooConfig.setOdooProtocol(""); + @DisplayName("Should return RPC_HTTPS when OdooHost starts with https") + void shouldReturnRPC_HTTPSWhenOdooHostStartsWithHttps() { + fhirOdooConfig.setOdooHost("https://localhost"); + assertEquals(fhirOdooConfig.getRPCProtocol(), OdooXmlRpcProxy.RPCProtocol.RPC_HTTPS); + } - assertThrows( - IllegalArgumentException.class, - () -> fhirOdooConfig.validateOdooProperties(), - "OdooProtocol is required"); + @Test + @DisplayName("Should return RPC_HTTP when OdooHost starts with http") + void shouldReturnRPC_HTTPWhenOdooHostStartsWithHttp() { + fhirOdooConfig.setOdooHost("http://localhost"); + assertEquals(fhirOdooConfig.getRPCProtocol(), OdooXmlRpcProxy.RPCProtocol.RPC_HTTP); + } + + @Test + @DisplayName("Should return hostname when getOdooHostName is called") + void shouldReturnHostNameWhenGetOdooHostNameIsCalled() { + fhirOdooConfig.setOdooHost("https://localhost"); + assertEquals(fhirOdooConfig.getOdooHostName(), "localhost"); } }