Skip to content

Commit

Permalink
Fixed account.parentAccountPath serialization to use Base64 version
Browse files Browse the repository at this point in the history
Signed-off-by: Alberto Codutti <alberto.codutti@eurotech.com>
  • Loading branch information
Coduz committed Oct 22, 2021
1 parent 786ce58 commit f50d6f8
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 2 deletions.
Expand Up @@ -15,9 +15,8 @@

import com.google.common.base.Strings;
import org.apache.commons.lang.SystemUtils;
import org.eclipse.kapua.KapuaException;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.eclipse.kapua.KapuaException;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import org.eclipse.persistence.jaxb.MarshallerProperties;
import org.slf4j.Logger;
Expand Down Expand Up @@ -418,6 +417,8 @@ private static <T> T unmarshal(@NotNull Reader reader, @NotNull Class<T> type, @

if (eventCollector.hasEvents()) {
for (ValidationEvent valEvent : eventCollector.getEvents()) {
LOG.warn("Unmarshal Validation Event: {} - {}", valEvent.getSeverity(), valEvent.getMessage(), valEvent.getLinkedException());

if (valEvent.getSeverity() != ValidationEvent.WARNING) {
String msg = MessageFormat.format("Line {0}, Col: {1}.{2}\tError message: {3}{2}\tLinked exception message:{4}",
valEvent.getLocator().getLineNumber(),
Expand Down
Expand Up @@ -71,6 +71,7 @@
import org.eclipse.kapua.service.account.AccountCreator;
import org.eclipse.kapua.service.account.AccountListResult;
import org.eclipse.kapua.service.account.AccountQuery;
import org.eclipse.kapua.service.account.xml.AccountParentPathXmlAdapter;
import org.eclipse.kapua.service.account.xml.AccountXmlRegistry;
import org.eclipse.kapua.service.authentication.ApiKeyCredentials;
import org.eclipse.kapua.service.authentication.AuthenticationCredentials;
Expand Down Expand Up @@ -387,6 +388,7 @@ public JaxbContextResolver() {
AccountCreator.class,
AccountListResult.class,
AccountQuery.class,
AccountParentPathXmlAdapter.class,
AccountXmlRegistry.class,

// Data Channel Info
Expand Down
4 changes: 4 additions & 0 deletions service/account/api/pom.xml
Expand Up @@ -28,6 +28,10 @@
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-service-api</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
</dependencies>

</project>
Expand Up @@ -15,6 +15,7 @@
import org.eclipse.kapua.model.KapuaEntity;
import org.eclipse.kapua.model.KapuaNamedEntity;
import org.eclipse.kapua.model.xml.DateXmlAdapter;
import org.eclipse.kapua.service.account.xml.AccountParentPathXmlAdapter;
import org.eclipse.kapua.service.account.xml.AccountXmlRegistry;

import javax.xml.bind.annotation.XmlAccessType;
Expand Down Expand Up @@ -71,6 +72,7 @@ default String getType() {
* @since 1.0.0
*/
@XmlElement(name = "parentAccountPath")
@XmlJavaTypeAdapter(AccountParentPathXmlAdapter.class)
String getParentAccountPath();

/**
Expand Down
@@ -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);
}
}
}
}
25 changes: 25 additions & 0 deletions service/account/internal/pom.xml
Expand Up @@ -29,21 +29,46 @@
<artifactId>kapua-account-api</artifactId>
</dependency>

<!-- -->
<!-- Required service interfaces -->
<dependency>
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-security-authorization-api</artifactId>
</dependency>

<!-- -->
<!-- Internal dependencies -->
<dependency>
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-commons</artifactId>
</dependency>

<!-- -->
<!-- Test -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-locator-guice</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-qa-markers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -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());
}
}
}
}
22 changes: 22 additions & 0 deletions service/account/internal/src/test/resources/locator.xml
@@ -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>
28 changes: 28 additions & 0 deletions service/account/internal/src/test/resources/logback.xml
@@ -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>

0 comments on commit f50d6f8

Please sign in to comment.