Move Token XML serialization out of XmlHelper
This commit is contained in:
Родитель
60baf623da
Коммит
2689479c46
|
@ -3,9 +3,7 @@
|
|||
|
||||
package com.microsoft.alm.helpers;
|
||||
|
||||
import com.microsoft.alm.secret.Token;
|
||||
import com.microsoft.alm.secret.TokenPair;
|
||||
import com.microsoft.alm.secret.TokenType;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
|
@ -19,7 +17,6 @@ import javax.xml.transform.TransformerFactory;
|
|||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.UUID;
|
||||
|
||||
public class XmlHelper {
|
||||
// Adapted from http://docs.oracle.com/javase/tutorial/jaxp/dom/readingXML.html
|
||||
|
@ -63,52 +60,6 @@ public class XmlHelper {
|
|||
}
|
||||
}
|
||||
|
||||
public static Token fromXmlToToken(final Node tokenNode) {
|
||||
Token value;
|
||||
|
||||
String tokenValue = null;
|
||||
TokenType tokenType = null;
|
||||
UUID targetIdentity = Guid.Empty;
|
||||
|
||||
final NodeList propertyNodes = tokenNode.getChildNodes();
|
||||
for (int v = 0; v < propertyNodes.getLength(); v++) {
|
||||
final Node propertyNode = propertyNodes.item(v);
|
||||
final String propertyName = propertyNode.getNodeName();
|
||||
if ("Type".equals(propertyName)) {
|
||||
tokenType = TokenType.valueOf(TokenType.class, XmlHelper.getText(propertyNode));
|
||||
} else if ("Value".equals(propertyName)) {
|
||||
tokenValue = XmlHelper.getText(propertyNode);
|
||||
} else if ("targetIdentity".equals(propertyName)) {
|
||||
targetIdentity = UUID.fromString(XmlHelper.getText(propertyNode));
|
||||
}
|
||||
}
|
||||
value = new Token(tokenValue, tokenType);
|
||||
value.setTargetIdentity(targetIdentity);
|
||||
return value;
|
||||
}
|
||||
|
||||
public static Element toXml(final Document document, final Token token) {
|
||||
final Element valueNode = document.createElement("value");
|
||||
|
||||
final Element typeNode = document.createElement("Type");
|
||||
final Text typeValue = document.createTextNode(token.Type.toString());
|
||||
typeNode.appendChild(typeValue);
|
||||
valueNode.appendChild(typeNode);
|
||||
|
||||
final Element tokenValueNode = document.createElement("Value");
|
||||
final Text valueValue = document.createTextNode(token.Value);
|
||||
tokenValueNode.appendChild(valueValue);
|
||||
valueNode.appendChild(tokenValueNode);
|
||||
|
||||
if (!Guid.Empty.equals(token.getTargetIdentity())) {
|
||||
final Element targetIdentityNode = document.createElement("targetIdentity");
|
||||
final Text targetIdentityValue = document.createTextNode(token.getTargetIdentity().toString());
|
||||
targetIdentityNode.appendChild(targetIdentityValue);
|
||||
valueNode.appendChild(targetIdentityNode);
|
||||
}
|
||||
return valueNode;
|
||||
}
|
||||
|
||||
public static TokenPair fromXmlToTokenPair(final Node tokenPairNode) {
|
||||
TokenPair value;
|
||||
|
||||
|
|
|
@ -7,8 +7,14 @@ import com.microsoft.alm.helpers.Debug;
|
|||
import com.microsoft.alm.helpers.Guid;
|
||||
import com.microsoft.alm.helpers.NotImplementedException;
|
||||
import com.microsoft.alm.helpers.StringHelper;
|
||||
import com.microsoft.alm.helpers.XmlHelper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.w3c.dom.Text;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.EnumSet;
|
||||
|
@ -89,6 +95,52 @@ public class Token extends Secret {
|
|||
|
||||
UUID targetIdentity = Guid.Empty;
|
||||
|
||||
public static Token fromXml(final Node tokenNode) {
|
||||
Token value;
|
||||
|
||||
String tokenValue = null;
|
||||
TokenType tokenType = null;
|
||||
UUID targetIdentity = Guid.Empty;
|
||||
|
||||
final NodeList propertyNodes = tokenNode.getChildNodes();
|
||||
for (int v = 0; v < propertyNodes.getLength(); v++) {
|
||||
final Node propertyNode = propertyNodes.item(v);
|
||||
final String propertyName = propertyNode.getNodeName();
|
||||
if ("Type".equals(propertyName)) {
|
||||
tokenType = TokenType.valueOf(TokenType.class, XmlHelper.getText(propertyNode));
|
||||
} else if ("Value".equals(propertyName)) {
|
||||
tokenValue = XmlHelper.getText(propertyNode);
|
||||
} else if ("targetIdentity".equals(propertyName)) {
|
||||
targetIdentity = UUID.fromString(XmlHelper.getText(propertyNode));
|
||||
}
|
||||
}
|
||||
value = new Token(tokenValue, tokenType);
|
||||
value.setTargetIdentity(targetIdentity);
|
||||
return value;
|
||||
}
|
||||
|
||||
public Element toXml(final Document document) {
|
||||
final Element valueNode = document.createElement("value");
|
||||
|
||||
final Element typeNode = document.createElement("Type");
|
||||
final Text typeValue = document.createTextNode(this.Type.toString());
|
||||
typeNode.appendChild(typeValue);
|
||||
valueNode.appendChild(typeNode);
|
||||
|
||||
final Element tokenValueNode = document.createElement("Value");
|
||||
final Text valueValue = document.createTextNode(this.Value);
|
||||
tokenValueNode.appendChild(valueValue);
|
||||
valueNode.appendChild(tokenValueNode);
|
||||
|
||||
if (!Guid.Empty.equals(this.getTargetIdentity())) {
|
||||
final Element targetIdentityNode = document.createElement("targetIdentity");
|
||||
final Text targetIdentityValue = document.createTextNode(this.getTargetIdentity().toString());
|
||||
targetIdentityNode.appendChild(targetIdentityValue);
|
||||
valueNode.appendChild(targetIdentityNode);
|
||||
}
|
||||
return valueNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The guid form Identity of the target
|
||||
*/
|
||||
|
|
|
@ -179,7 +179,7 @@ class InsecureFileBackend {
|
|||
if ("key".equals(keyOrValueName)) {
|
||||
key = XmlHelper.getText(keyOrValueNode);
|
||||
} else if ("value".equals(keyOrValueName)) {
|
||||
value = XmlHelper.fromXmlToToken(keyOrValueNode);
|
||||
value = Token.fromXml(keyOrValueNode);
|
||||
}
|
||||
}
|
||||
result.Tokens.put(key, value);
|
||||
|
@ -220,7 +220,7 @@ class InsecureFileBackend {
|
|||
|
||||
final Token value = entry.getValue();
|
||||
if (value != null) {
|
||||
final Element valueNode = XmlHelper.toXml(document, value);
|
||||
final Element valueNode = value.toXml(document);
|
||||
|
||||
entryNode.appendChild(valueNode);
|
||||
}
|
||||
|
|
|
@ -3,9 +3,7 @@
|
|||
|
||||
package com.microsoft.alm.helpers;
|
||||
|
||||
import com.microsoft.alm.secret.Token;
|
||||
import com.microsoft.alm.secret.TokenPair;
|
||||
import com.microsoft.alm.secret.TokenType;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
|
@ -14,7 +12,6 @@ import org.w3c.dom.Element;
|
|||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
@ -27,38 +24,6 @@ public class XmlHelperTest {
|
|||
underTest = new XmlHelper();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void xmlTokenSerialization_roundTrip() throws Exception {
|
||||
final Token token = new Token("1", TokenType.Access);
|
||||
token.setTargetIdentity(UUID.fromString("ffffffff-ffff-ffff-ffff-ffffffffffff"));
|
||||
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
final DocumentBuilder builder = dbf.newDocumentBuilder();
|
||||
final Document serializationDoc = builder.newDocument();
|
||||
|
||||
final Element element = underTest.toXml(serializationDoc, token);
|
||||
|
||||
serializationDoc.appendChild(element);
|
||||
final String actualXmlString = XmlHelper.toString(serializationDoc);
|
||||
final String expectedXmlString =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
|
||||
"<value>\n" +
|
||||
" <Type>Access</Type>\n" +
|
||||
" <Value>1</Value>\n" +
|
||||
" <targetIdentity>ffffffff-ffff-ffff-ffff-ffffffffffff</targetIdentity>\n" +
|
||||
"</value>";
|
||||
StringHelperTest.assertLinesEqual(expectedXmlString, actualXmlString);
|
||||
|
||||
final ByteArrayInputStream bais = new ByteArrayInputStream(actualXmlString.getBytes());
|
||||
final Document deserializationDoc = builder.parse(bais);
|
||||
final Element rootNode = deserializationDoc.getDocumentElement();
|
||||
|
||||
final Token actualToken = underTest.fromXmlToToken(rootNode);
|
||||
|
||||
assertEquals(token.Value, actualToken.Value);
|
||||
assertEquals(token.Type, actualToken.Type);
|
||||
assertEquals(token.getTargetIdentity(), actualToken.getTargetIdentity());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void xmlTokenPairSerialization_roundTrip() throws Exception {
|
||||
final TokenPair tokenPair =
|
||||
|
|
|
@ -5,9 +5,16 @@ package com.microsoft.alm.secret;
|
|||
|
||||
import com.microsoft.alm.helpers.BitConverter;
|
||||
import com.microsoft.alm.helpers.Guid;
|
||||
import com.microsoft.alm.helpers.StringHelperTest;
|
||||
import com.microsoft.alm.helpers.XmlHelper;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
|
@ -85,6 +92,38 @@ public class TokenTest {
|
|||
Assert.assertEquals(expectedHex, actualHex);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void xmlSerialization_roundTrip() throws Exception {
|
||||
final Token token = new Token("1", TokenType.Access);
|
||||
token.targetIdentity = UUID.fromString("ffffffff-ffff-ffff-ffff-ffffffffffff");
|
||||
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
final DocumentBuilder builder = dbf.newDocumentBuilder();
|
||||
final Document serializationDoc = builder.newDocument();
|
||||
|
||||
final Element element = token.toXml(serializationDoc);
|
||||
|
||||
serializationDoc.appendChild(element);
|
||||
final String actualXmlString = XmlHelper.toString(serializationDoc);
|
||||
final String expectedXmlString =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
|
||||
"<value>\n" +
|
||||
" <Type>Access</Type>\n" +
|
||||
" <Value>1</Value>\n" +
|
||||
" <targetIdentity>ffffffff-ffff-ffff-ffff-ffffffffffff</targetIdentity>\n" +
|
||||
"</value>";
|
||||
StringHelperTest.assertLinesEqual(expectedXmlString, actualXmlString);
|
||||
|
||||
final ByteArrayInputStream bais = new ByteArrayInputStream(actualXmlString.getBytes());
|
||||
final Document deserializationDoc = builder.parse(bais);
|
||||
final Element rootNode = deserializationDoc.getDocumentElement();
|
||||
|
||||
final Token actualToken = Token.fromXml(rootNode);
|
||||
|
||||
Assert.assertEquals(token.Value, actualToken.Value);
|
||||
Assert.assertEquals(token.Type, actualToken.Type);
|
||||
Assert.assertEquals(token.targetIdentity, actualToken.targetIdentity);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void validate_tooLong() {
|
||||
final int numberOfCharacters = 2048;
|
||||
|
|
Загрузка…
Ссылка в новой задаче