Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed account.parentAccountPath serialization to use Base64 version
Signed-off-by: Alberto Codutti <alberto.codutti@eurotech.com>
- Loading branch information
Showing
9 changed files
with
234 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
.../api/src/main/java/org/eclipse/kapua/service/account/xml/AccountParentPathXmlAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Eurotech and/or its affiliates and others | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Eurotech - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.kapua.service.account.xml; | ||
|
||
import com.google.common.base.Strings; | ||
import org.eclipse.kapua.KapuaIllegalArgumentException; | ||
import org.eclipse.kapua.locator.KapuaLocator; | ||
import org.eclipse.kapua.model.id.KapuaIdFactory; | ||
import org.eclipse.kapua.service.account.Account; | ||
|
||
import javax.xml.bind.annotation.adapters.XmlAdapter; | ||
import java.math.BigInteger; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* {@link Account#getParentAccountPath()} {@link XmlAdapter}. | ||
* | ||
* @since 1.6.0 | ||
*/ | ||
public class AccountParentPathXmlAdapter extends XmlAdapter<String, String> { | ||
|
||
private static final KapuaLocator LOCATOR = KapuaLocator.getInstance(); | ||
private static final KapuaIdFactory KAPUA_ID_FACTORY = LOCATOR.getFactory(KapuaIdFactory.class); | ||
|
||
@Override | ||
public String marshal(String parentAccountPathLong) { | ||
if (Strings.isNullOrEmpty(parentAccountPathLong)) { | ||
return null; | ||
} else { | ||
String[] parentAccountPathLongTokens = parentAccountPathLong.substring(1).split("/"); | ||
|
||
List<String> parentAccountPathBase64List = | ||
Arrays.stream(parentAccountPathLongTokens) | ||
.map(p -> KAPUA_ID_FACTORY.newKapuaId(new BigInteger(p)).toCompactId()) | ||
.collect(Collectors.toList()); | ||
|
||
return "/" + String.join("/", parentAccountPathBase64List); | ||
} | ||
} | ||
|
||
@Override | ||
public String unmarshal(String parentAccountPathBase64) throws KapuaIllegalArgumentException { | ||
if (Strings.isNullOrEmpty(parentAccountPathBase64)) { | ||
return null; | ||
} else { | ||
String[] parentAccountPathBase64Tokens = parentAccountPathBase64.substring(1).split("/"); | ||
|
||
try { | ||
List<String> parentAccountPathLongList = | ||
Arrays.stream(parentAccountPathBase64Tokens) | ||
.map(p -> KAPUA_ID_FACTORY.newKapuaId(p).toStringId()) | ||
.collect(Collectors.toList()); | ||
|
||
return "/" + String.join("/", parentAccountPathLongList); | ||
} catch (IllegalArgumentException e) { | ||
throw new KapuaIllegalArgumentException("account.parentAccountPath", parentAccountPathBase64); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
.../src/test/java/org/eclipse/kapua/service/account/xml/AccountParentPathXmlAdapterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Eurotech and/or its affiliates and others | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Eurotech - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.kapua.service.account.xml; | ||
|
||
import org.eclipse.kapua.KapuaIllegalArgumentException; | ||
import org.eclipse.kapua.qa.markers.junit.JUnitTests; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@Category(JUnitTests.class) | ||
public class AccountParentPathXmlAdapterTest { | ||
|
||
private static final AccountParentPathXmlAdapter XML_ADAPTER = new AccountParentPathXmlAdapter(); | ||
|
||
@Test | ||
public void testMarshal() { | ||
String parentAccountPath = "/1/2/3"; | ||
|
||
String marshalled = XML_ADAPTER.marshal(parentAccountPath); | ||
|
||
Assert.assertNotNull(marshalled); | ||
Assert.assertEquals("/AQ/Ag/Aw", marshalled); | ||
} | ||
|
||
@Test | ||
public void testMarshalNull() { | ||
String marshalled = XML_ADAPTER.marshal(null); | ||
|
||
Assert.assertNull(marshalled); | ||
} | ||
|
||
@Test | ||
public void testUnmarshal() throws KapuaIllegalArgumentException { | ||
String parentAccountPath = "/AQ/Ag/Aw"; | ||
|
||
String unmarshalled = XML_ADAPTER.unmarshal(parentAccountPath); | ||
|
||
Assert.assertNotNull(unmarshalled); | ||
Assert.assertEquals("/1/2/3", unmarshalled); | ||
} | ||
|
||
@Test | ||
public void testUnmarshalNull() throws KapuaIllegalArgumentException { | ||
String unmarshalled = XML_ADAPTER.unmarshal(null); | ||
|
||
Assert.assertNull(unmarshalled); | ||
} | ||
|
||
@Test | ||
public void testUnmarshalInvalid() { | ||
List<String> parentAccountPaths = Arrays.asList("/AQ/Ag/Awwerty###", "///", "A//B", "!@#$%^&*()_"); | ||
|
||
for (String parentAccountPath : parentAccountPaths) { | ||
try { | ||
XML_ADAPTER.unmarshal(parentAccountPath); | ||
} catch (KapuaIllegalArgumentException kapuaIllegalArgumentException) { | ||
Assert.assertEquals("account.parentAccountPath", kapuaIllegalArgumentException.getArgumentName()); | ||
Assert.assertEquals(parentAccountPath, kapuaIllegalArgumentException.getArgumentValue()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright (c) 2016, 2021 Eurotech and/or its affiliates and others | ||
This program and the accompanying materials are made | ||
available under the terms of the Eclipse Public License 2.0 | ||
which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
SPDX-License-Identifier: EPL-2.0 | ||
Contributors: | ||
Eurotech - initial API and implementation | ||
--> | ||
<!DOCTYPE xml> | ||
<locator-config> | ||
<provided> | ||
<api>org.eclipse.kapua.model.id.KapuaIdFactory</api> | ||
</provided> | ||
<packages> | ||
<package>org.eclipse.kapua.commons</package> | ||
</packages> | ||
</locator-config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright (c) 2021 Red Hat Inc and others | ||
This program and the accompanying materials are made | ||
available under the terms of the Eclipse Public License 2.0 | ||
which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
SPDX-License-Identifier: EPL-2.0 | ||
Contributors: | ||
Red Hat - initial API and implementation | ||
--> | ||
<!DOCTYPE xml> | ||
<configuration> | ||
|
||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<logger name="liquibase" level="WARN"/> | ||
|
||
<root level="info"> | ||
<appender-ref ref="STDOUT"/> | ||
</root> | ||
</configuration> |