Move TokenPair XML serialization out of XmlHelper
This commit is contained in:
Родитель
2689479c46
Коммит
2b1dd5afb5
|
@ -3,12 +3,9 @@
|
|||
|
||||
package com.microsoft.alm.helpers;
|
||||
|
||||
import com.microsoft.alm.secret.TokenPair;
|
||||
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 javax.xml.transform.OutputKeys;
|
||||
import javax.xml.transform.Transformer;
|
||||
|
@ -60,40 +57,4 @@ public class XmlHelper {
|
|||
}
|
||||
}
|
||||
|
||||
public static TokenPair fromXmlToTokenPair(final Node tokenPairNode) {
|
||||
TokenPair value;
|
||||
|
||||
String accessToken = null;
|
||||
String refreshToken = null;
|
||||
|
||||
final NodeList propertyNodes = tokenPairNode.getChildNodes();
|
||||
for (int v = 0; v < propertyNodes.getLength(); v++) {
|
||||
final Node propertyNode = propertyNodes.item(v);
|
||||
final String propertyName = propertyNode.getNodeName();
|
||||
if ("accessToken".equals(propertyName)) {
|
||||
accessToken = XmlHelper.getText(propertyNode);
|
||||
} else if ("refreshToken".equals(propertyName)) {
|
||||
refreshToken = XmlHelper.getText(propertyNode);
|
||||
}
|
||||
}
|
||||
|
||||
value = new TokenPair(accessToken, refreshToken);
|
||||
return value;
|
||||
}
|
||||
|
||||
public static Element toXml(final Document document, final TokenPair tokenPair) {
|
||||
final Element valueNode = document.createElement("value");
|
||||
|
||||
final Element accessTokenNode = document.createElement("accessToken");
|
||||
final Text accessTokenValue = document.createTextNode(tokenPair.AccessToken.Value);
|
||||
accessTokenNode.appendChild(accessTokenValue);
|
||||
valueNode.appendChild(accessTokenNode);
|
||||
|
||||
final Element refreshTokenNode = document.createElement("refreshToken");
|
||||
final Text refreshTokenValue = document.createTextNode(tokenPair.RefreshToken.Value);
|
||||
refreshTokenNode.appendChild(refreshTokenValue);
|
||||
valueNode.appendChild(refreshTokenNode);
|
||||
|
||||
return valueNode;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,12 @@ package com.microsoft.alm.secret;
|
|||
import com.microsoft.alm.helpers.Debug;
|
||||
import com.microsoft.alm.helpers.PropertyBag;
|
||||
import com.microsoft.alm.helpers.StringHelper;
|
||||
import com.microsoft.alm.helpers.XmlHelper;
|
||||
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.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
|
@ -68,6 +74,43 @@ public class TokenPair extends Secret {
|
|||
public final Token RefreshToken;
|
||||
public final Map<String, String> Parameters;
|
||||
|
||||
public static TokenPair fromXml(final Node tokenPairNode) {
|
||||
TokenPair value;
|
||||
|
||||
String accessToken = null;
|
||||
String refreshToken = null;
|
||||
|
||||
final NodeList propertyNodes = tokenPairNode.getChildNodes();
|
||||
for (int v = 0; v < propertyNodes.getLength(); v++) {
|
||||
final Node propertyNode = propertyNodes.item(v);
|
||||
final String propertyName = propertyNode.getNodeName();
|
||||
if ("accessToken".equals(propertyName)) {
|
||||
accessToken = XmlHelper.getText(propertyNode);
|
||||
} else if ("refreshToken".equals(propertyName)) {
|
||||
refreshToken = XmlHelper.getText(propertyNode);
|
||||
}
|
||||
}
|
||||
|
||||
value = new TokenPair(accessToken, refreshToken);
|
||||
return value;
|
||||
}
|
||||
|
||||
public Element toXml(final Document document) {
|
||||
final Element valueNode = document.createElement("value");
|
||||
|
||||
final Element accessTokenNode = document.createElement("accessToken");
|
||||
final Text accessTokenValue = document.createTextNode(AccessToken.Value);
|
||||
accessTokenNode.appendChild(accessTokenValue);
|
||||
valueNode.appendChild(accessTokenNode);
|
||||
|
||||
final Element refreshTokenNode = document.createElement("refreshToken");
|
||||
final Text refreshTokenValue = document.createTextNode(RefreshToken.Value);
|
||||
refreshTokenNode.appendChild(refreshTokenValue);
|
||||
valueNode.appendChild(refreshTokenNode);
|
||||
|
||||
return valueNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares an object to this.
|
||||
*
|
||||
|
|
|
@ -3,17 +3,7 @@
|
|||
|
||||
package com.microsoft.alm.helpers;
|
||||
|
||||
import com.microsoft.alm.secret.TokenPair;
|
||||
import org.junit.Before;
|
||||
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 static org.junit.Assert.assertEquals;
|
||||
|
||||
public class XmlHelperTest {
|
||||
|
||||
|
@ -24,34 +14,4 @@ public class XmlHelperTest {
|
|||
underTest = new XmlHelper();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void xmlTokenPairSerialization_roundTrip() throws Exception {
|
||||
final TokenPair tokenPair =
|
||||
new TokenPair("9297fb18-46d0-4846-97ca-ab8dd3b55729", "d15281b1-03f1-4581-90d3-4527d9cf4147");
|
||||
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
final DocumentBuilder builder = dbf.newDocumentBuilder();
|
||||
final Document serializationDoc = builder.newDocument();
|
||||
|
||||
final Element element = underTest.toXml(serializationDoc, tokenPair);
|
||||
|
||||
serializationDoc.appendChild(element);
|
||||
final String actualXmlString = XmlHelper.toString(serializationDoc);
|
||||
final String expectedXmlString =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
|
||||
"<value>\n" +
|
||||
" <accessToken>9297fb18-46d0-4846-97ca-ab8dd3b55729</accessToken>\n" +
|
||||
" <refreshToken>d15281b1-03f1-4581-90d3-4527d9cf4147</refreshToken>\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 TokenPair actualTokenPair = underTest.fromXmlToTokenPair(rootNode);
|
||||
|
||||
assertEquals(tokenPair.AccessToken.Value, actualTokenPair.AccessToken.Value);
|
||||
assertEquals(tokenPair.RefreshToken.Value, actualTokenPair.RefreshToken.Value);
|
||||
}
|
||||
|
||||
}
|
|
@ -3,11 +3,51 @@
|
|||
|
||||
package com.microsoft.alm.secret;
|
||||
|
||||
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 static org.junit.Assert.assertEquals;
|
||||
|
||||
public class TokenPairTest {
|
||||
|
||||
@Test
|
||||
public void xmlSerialization_roundTrip() throws Exception {
|
||||
final TokenPair tokenPair =
|
||||
new TokenPair("9297fb18-46d0-4846-97ca-ab8dd3b55729", "d15281b1-03f1-4581-90d3-4527d9cf4147");
|
||||
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
final DocumentBuilder builder = dbf.newDocumentBuilder();
|
||||
final Document serializationDoc = builder.newDocument();
|
||||
|
||||
final Element element = tokenPair.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" +
|
||||
" <accessToken>9297fb18-46d0-4846-97ca-ab8dd3b55729</accessToken>\n" +
|
||||
" <refreshToken>d15281b1-03f1-4581-90d3-4527d9cf4147</refreshToken>\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 TokenPair actualTokenPair = TokenPair.fromXml(rootNode);
|
||||
|
||||
assertEquals(tokenPair.AccessToken.Value, actualTokenPair.AccessToken.Value);
|
||||
assertEquals(tokenPair.RefreshToken.Value, actualTokenPair.RefreshToken.Value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void accessTokenResponse_RFC6749() {
|
||||
final String input =
|
||||
|
|
|
@ -37,7 +37,7 @@ public class GnomeKeyringBackedTokenPairStore extends GnomeKeyringBackedSecureSt
|
|||
final DocumentBuilder builder = dbf.newDocumentBuilder();
|
||||
final Document document = builder.newDocument();
|
||||
|
||||
final Element element = XmlHelper.toXml(document, tokenPair);
|
||||
final Element element = tokenPair.toXml(document);
|
||||
document.appendChild(element);
|
||||
|
||||
final String result = XmlHelper.toString(document);
|
||||
|
@ -74,7 +74,7 @@ public class GnomeKeyringBackedTokenPairStore extends GnomeKeyringBackedSecureSt
|
|||
final Document document = builder.parse(source);
|
||||
final Element rootElement = document.getDocumentElement();
|
||||
|
||||
final TokenPair result = XmlHelper.fromXmlToTokenPair(rootElement);
|
||||
final TokenPair result = TokenPair.fromXml(rootElement);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче