Skip to content

Commit

Permalink
OZ-708: Infer RPC_PROTOCOL from host url
Browse files Browse the repository at this point in the history
  • Loading branch information
corneliouzbett committed Oct 1, 2024
1 parent 462e080 commit 57c9cd7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 38 deletions.
23 changes: 10 additions & 13 deletions fhir-odoo/src/main/java/com/ozonehis/fhir/FhirOdooConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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)://<host>
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)://<host>
var hostname = OdooHost.substring(OdooHost.indexOf("://") + 3);
return hostname.endsWith("/") ? hostname.substring(0, hostname.length() - 1) : hostname;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 3 additions & 5 deletions fhir-odoo/src/main/resources/fhir-odoo.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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}
39 changes: 21 additions & 18 deletions fhir-odoo/src/test/java/com/ozonehis/fhir/FhirOdooConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}

Expand All @@ -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");
Expand All @@ -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,
Expand All @@ -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");
}
}

0 comments on commit 57c9cd7

Please sign in to comment.