Skip to content
Snippets Groups Projects
Commit d9166599 authored by Eduardo Santos's avatar Eduardo Santos
Browse files

Fixed LCM escape method

parent 7ca7331d
No related branches found
No related tags found
2 merge requests!24Added new textEscape method,!23Added new textEscape method
Pipeline #11285 passed
package org.etsi.osl.osom.lcm; package org.etsi.osl.osom.lcm;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import java.time.ZoneOffset; import java.time.ZoneOffset;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -11,15 +14,20 @@ import java.util.function.Consumer; ...@@ -11,15 +14,20 @@ import java.util.function.Consumer;
import javax.net.ssl.SSLException; import javax.net.ssl.SSLException;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException; 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.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.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ArrayNode;
import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option; import com.jayway.jsonpath.Option;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.commons.text.StringEscapeUtils;
import org.etsi.osl.osom.partnerservices.GenericClient; import org.etsi.osl.osom.partnerservices.GenericClient;
import org.etsi.osl.tmf.common.model.Any; import org.etsi.osl.tmf.common.model.Any;
import org.etsi.osl.tmf.common.model.EValueType; import org.etsi.osl.tmf.common.model.EValueType;
...@@ -126,7 +134,7 @@ public abstract class LcmBaseExecutor { ...@@ -126,7 +134,7 @@ public abstract class LcmBaseExecutor {
} }
private Optional<Characteristic> getCharacteristicByName(String charName) { protected Optional<Characteristic> getCharacteristicByName(String charName) {
List<Characteristic> serviceCharacteristic; List<Characteristic> serviceCharacteristic;
if (lcmspec.getLcmrulephase().equals("PRE_PROVISION") || this.vars.getService() == null) { if (lcmspec.getLcmrulephase().equals("PRE_PROVISION") || this.vars.getService() == null) {
...@@ -225,19 +233,31 @@ public abstract class LcmBaseExecutor { ...@@ -225,19 +233,31 @@ public abstract class LcmBaseExecutor {
return -1; return -1;
} }
public String getCharValAsString(String charName) { public String getCharValAsString(String charName) {
logger.debug("getCharValAsString " + charName); Optional<Characteristic> characteristic = getCharacteristicByName(charName);
Optional<Characteristic> c = getCharacteristicByName(charName);
if (c.isPresent()) { logger.info("Getting characteristic value as a string...");
logger.debug("getCharValAsString " + c.get().getValue().getValue());
return c.get().getValue().getValue(); if (characteristic.isPresent()) {
return characteristic.get().getValue().getValue();
} }
logger.debug("getCharValAsString NULL ");
return 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) { public Boolean getCharValFromBooleanType(String charName) {
logger.debug("getCharValFromBooleanType " + charName); logger.debug("getCharValFromBooleanType " + charName);
......
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);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment