diff --git a/src/main/java/org/etsi/osl/osom/lcm/LcmBaseExecutor.java b/src/main/java/org/etsi/osl/osom/lcm/LcmBaseExecutor.java index 7e7025a8e3f8afa93637d2e72bb42b33722c70b7..741676df2304f99327e3d3124c91552ad930e6a7 100644 --- a/src/main/java/org/etsi/osl/osom/lcm/LcmBaseExecutor.java +++ b/src/main/java/org/etsi/osl/osom/lcm/LcmBaseExecutor.java @@ -1,6 +1,9 @@ package org.etsi.osl.osom.lcm; +import java.io.File; import java.io.IOException; +import java.time.LocalDate; +import java.time.LocalTime; import java.time.OffsetDateTime; import java.time.ZoneOffset; import java.util.ArrayList; @@ -11,15 +14,20 @@ import java.util.function.Consumer; import javax.net.ssl.SSLException; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.exc.StreamWriteException; import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; +import com.fasterxml.jackson.databind.DatabindException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.databind.node.ArrayNode; import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.Option; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.commons.text.StringEscapeUtils; import org.etsi.osl.osom.partnerservices.GenericClient; import org.etsi.osl.tmf.common.model.Any; import org.etsi.osl.tmf.common.model.EValueType; @@ -126,7 +134,7 @@ public abstract class LcmBaseExecutor { } - private Optional<Characteristic> getCharacteristicByName(String charName) { + protected Optional<Characteristic> getCharacteristicByName(String charName) { List<Characteristic> serviceCharacteristic; if (lcmspec.getLcmrulephase().equals("PRE_PROVISION") || this.vars.getService() == null) { @@ -225,19 +233,31 @@ public abstract class LcmBaseExecutor { return -1; } + public String getCharValAsString(String charName) { - logger.debug("getCharValAsString " + charName); - Optional<Characteristic> c = getCharacteristicByName(charName); + Optional<Characteristic> characteristic = getCharacteristicByName(charName); - if (c.isPresent()) { - logger.debug("getCharValAsString " + c.get().getValue().getValue()); - return c.get().getValue().getValue(); + logger.info("Getting characteristic value as a string..."); + + if (characteristic.isPresent()) { + return characteristic.get().getValue().getValue(); } - - logger.debug("getCharValAsString NULL "); + return null; + } + + public String escapeText(String s) { + if (s == null || s.isEmpty()) { + logger.debug("escapeText NULL "); + return null; + } + + logger.info("Escaping string..."); + + return "\"" + StringEscapeUtils.escapeJava(s) + "\""; } + public Boolean getCharValFromBooleanType(String charName) { logger.debug("getCharValFromBooleanType " + charName); diff --git a/src/test/java/org/etsi/osl/osom/LcmBaseExecutorTest.java b/src/test/java/org/etsi/osl/osom/LcmBaseExecutorTest.java new file mode 100644 index 0000000000000000000000000000000000000000..1f02c9a8f33aa01d3cd15e6f24dc68dcb8decdff --- /dev/null +++ b/src/test/java/org/etsi/osl/osom/LcmBaseExecutorTest.java @@ -0,0 +1,62 @@ +package org.etsi.osl.osom; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; + +import java.util.Optional; + +import org.etsi.osl.tmf.common.model.Any; +import org.etsi.osl.tmf.common.model.service.Characteristic; +import org.etsi.osl.osom.LcmBaseExecutorTest.TestLcmBaseExecutor; +import org.etsi.osl.osom.lcm.LcmBaseExecutor; +import org.junit.Before; +import org.junit.Test; + +public class LcmBaseExecutorTest { + + private LcmBaseExecutor executor; + + @Before + public void setUp() { + executor = spy(new TestLcmBaseExecutor()); + } + + @Test + public void testGetCharValAsString() { + // Test the method + String result = executor.getCharValAsString("Parameter"); + String expected = "[{\"device\": {\"ipv4Address\": {}, \"networkAccessIdentifier\": \"123456789@domain.com\", \"phoneNumber\": \"+123456789\"}, \"provisioningId\": \"3fa85f64-5717-4562-b3fc-2c963f66afa6\", \"qosProfile\": \"QOS_A\", \"status\": \"AVAILABLE\", \"startedAt\": \"2024-12-12T09:52:26Z\"}]"; + assertEquals(expected, result); + } + + @Test + public void testEscapeText() { + String input = "[{\"device\": {\"ipv4Address\": {}, \"networkAccessIdentifier\": \"123456789@domain.com\", \"phoneNumber\": \"+123456789\"}, \"provisioningId\": \"3fa85f64-5717-4562-b3fc-2c963f66afa6\", \"qosProfile\": \"QOS_A\", \"status\": \"AVAILABLE\", \"startedAt\": \"2024-12-12T09:52:26Z\"}]"; + String expected = "\"" + input.replace("\"", "\\\"") + "\""; + String result = executor.escapeText(input); + + assertEquals(expected, result); + } + + static class TestLcmBaseExecutor extends LcmBaseExecutor { + @Override + public void exec() { + } + + @Override + protected Optional<Characteristic> getCharacteristicByName(String charName) { + Any anyValue = new Any(); + anyValue.setValue( + "[{\"device\": {\"ipv4Address\": {}, \"networkAccessIdentifier\": \"123456789@domain.com\", \"phoneNumber\": \"+123456789\"}, \"provisioningId\": \"3fa85f64-5717-4562-b3fc-2c963f66afa6\", \"qosProfile\": \"QOS_A\", \"status\": \"AVAILABLE\", \"startedAt\": \"2024-12-12T09:52:26Z\"}]" + ); + + Characteristic characteristic = new Characteristic(); + characteristic.setName("Parameter"); + characteristic.setValue(anyValue); + + return Optional.of(characteristic); + } + } + +}