Skip to content

Instantly share code, notes, and snippets.

@adamretter
Created August 17, 2018 06:55
Show Gist options
  • Save adamretter/9a0e5c3a98898267fea77cedd94fdff8 to your computer and use it in GitHub Desktop.
Save adamretter/9a0e5c3a98898267fea77cedd94fdff8 to your computer and use it in GitHub Desktop.
org.exist.management.client.JMXServletTest
/*
* eXist Open Source Native XML Database
* Copyright (C) 2001-2018 The eXist Project
* http://exist-db.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.exist.management.client;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.fluent.Executor;
import org.apache.http.client.fluent.Request;
import org.apache.http.message.BasicHeader;
import org.exist.test.ExistWebServer;
import org.junit.ClassRule;
import org.junit.Test;
import org.xmlunit.builder.Input;
import org.xmlunit.xpath.JAXPXPathEngine;
import javax.xml.transform.Source;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
public class JMXServletTest {
@ClassRule public static ExistWebServer existWebServer = new ExistWebServer(true, true, true, true, false);
private static String getServerUri() {
return "http://localhost:" + existWebServer.getPort();
}
private static String getStatusUri() {
return getServerUri() + "/exist/status";
}
@Test
public void jmxXmlTest() throws IOException {
final Executor executor = Executor.newInstance();
final HttpResponse response = executor.execute(Request
.Get(getStatusUri())
.addHeader(new BasicHeader("Accept", "application/xml"))
).returnResponse();
assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
final JAXPXPathEngine engine = new JAXPXPathEngine();
final Map<String, String> namespaces = new HashMap<>();
namespaces.put("jmx", "http://exist-db.org/jmx");
engine.setNamespaceContext(namespaces);
try (final InputStream is = response.getEntity().getContent()) {
final Source jmxXmlSource = Input.fromStream(is).build();
assertEquals("1", engine.evaluate("count(/jmx:jmx)", jmxXmlSource));
//TODO(AR/DW) further assertions about the Jmx Xml content...
// final Iterable<Node> jmx = engine.selectNodes("/jmx:some-node", jmxXmlSource);
// assertNotNull(jmx);
// //etc...
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment