All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.greenpepper.runner.repository.AtlassianRepository Maven / Gradle / Ivy

There is a newer version: 4.2.4
Show newest version
package com.greenpepper.runner.repository;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.*;

import com.greenpepper.dialect.SpecificationDialect;
import com.greenpepper.repository.DocumentNode;
import com.greenpepper.shaded.org.apache.commons.lang3.StringUtils;
import com.greenpepper.shaded.org.apache.xmlrpc.XmlRpcClient;
import com.greenpepper.shaded.org.apache.xmlrpc.XmlRpcException;
import com.greenpepper.shaded.org.apache.xmlrpc.XmlRpcRequest;
import com.greenpepper.shaded.org.jsoup.Jsoup;
import com.greenpepper.shaded.org.jsoup.nodes.Element;
import com.greenpepper.shaded.org.jsoup.select.Elements;
import com.greenpepper.shaded.org.slf4j.Logger;
import com.greenpepper.shaded.org.slf4j.LoggerFactory;

import com.greenpepper.document.Document;
import com.greenpepper.html.HtmlDocumentBuilder;
import com.greenpepper.repository.DocumentNotFoundException;
import com.greenpepper.repository.DocumentRepository;
import com.greenpepper.repository.RepositoryException;
import com.greenpepper.util.CollectionUtil;
import com.greenpepper.util.IOUtil;
import com.greenpepper.util.URIUtil;

import static java.lang.String.format;
import static com.greenpepper.shaded.org.apache.commons.lang3.StringUtils.defaultString;
import static com.greenpepper.shaded.org.apache.commons.lang3.StringUtils.isAnyEmpty;
import static com.greenpepper.shaded.org.apache.commons.lang3.StringUtils.isNotBlank;

/**
 * 

AtlassianRepository class.

* * @author oaouattara * @version $Id: $Id */ public class AtlassianRepository implements DocumentRepository { /** Constant PAGE_NOT_FOUND="Page Not Found !" */ static final String PAGE_NOT_FOUND = "Page Not Found !"; /** Constant INSUFFICIENT_PRIVILEGES="INSUFFICIENT PRIVILEGES !" */ static final String INSUFFICIENT_PRIVILEGES = "INSUFFICIENT PRIVILEGES !"; /** Constant SESSION_INVALID="Session Invalid !" */ static final String SESSION_INVALID = "Session Invalid !"; /** Constant PARAMETERS_MISSING="Parameters Missing, expecting:[SpaceKey"{trunked} */ private static final String PARAMETERS_MISSING = "Parameters Missing, expecting:[SpaceKey, PageTitle, IncludeStyle] !"; private static final Logger LOGGER = LoggerFactory.getLogger(AtlassianRepository.class); private static final int SUTNAME_INDEX = 0; private static final int REPOSITORY_UID_INDEX = 1; private static final String CONFLUENCE = "Confluence-"; private final URI root; private boolean runNotImplemented; private String handler; private boolean includeStyle; private String username = ""; private String password = ""; private SpecificationDialect specificationDialect; private List selectedRepository; private XmlRpcClient xmlRpcClient; /** * Constructor for AtlassianRepository. * * The arguments are the following *
    *
  • root: the URL of the greenpepper RPC handler. eg.: http://localhost:19005/rpc/xmlrpc?handler=greenpepper1&includeStyle=true#SPACE KEY
  • *
  • (optional) username: default to empty string
  • *
  • (optional) password: default to empty string
  • *
  • (optional) runNotImplemented: If set to true, when asking for a specification, the caller will get the * last version and not the implemented version.(default to false).
  • *
* * @param args The list of parameters to set. * @throws java.lang.Exception if any. */ public AtlassianRepository(String... args) throws Exception { this.root = URI.create(URIUtil.raw(args[0])); String includeAtt = URIUtil.getAttribute(root, "includeStyle"); includeStyle = includeAtt == null ? true : Boolean.valueOf(includeAtt); handler = URIUtil.getAttribute(root, "handler"); if(handler == null) throw new IllegalArgumentException("Missing handler"); if (args.length >= 3) { username = defaultString(args[1]); password = defaultString(args[2]); } if (args.length >= 4) { runNotImplemented = Boolean.valueOf(args[3]); } } /** {@inheritDoc} */ public Document loadDocument(String location) throws Exception { String spec = retrieveSpecification(URI.create(URIUtil.raw(location))); LOGGER.trace("Page retrieved from the repository for location '{}'\n{}", location,spec); // check if there is an error in the page com.greenpepper.shaded.org.jsoup.nodes.Document jsoupDoc = Jsoup.parse(spec); jsoupDoc.outputSettings().prettyPrint(false); Elements select = jsoupDoc.select("#conf_actionError_Msg"); for (Element element : select) { if (element.hasText()) { if ( StringUtils.equals(element.text(), PAGE_NOT_FOUND)) { throw new DocumentNotFoundException(location); } else if (StringUtils.equals(element.text(), PARAMETERS_MISSING)){ throw new RepositoryException(PARAMETERS_MISSING); } else if (StringUtils.equals(element.text(), SESSION_INVALID)){ throw new RepositoryException(SESSION_INVALID); } else if (StringUtils.equals(element.text(), INSUFFICIENT_PRIVILEGES)){ throw new RepositoryException(INSUFFICIENT_PRIVILEGES); } } } select = jsoupDoc.select("#conf_actionWarn_Msg"); for (Element element : select) { if (element.hasText()) { if ( StringUtils.equals(element.text(), THIS_SPECIFICATION_WAS_NEVER_SET_AS_IMPLEMENTED)) { throw new DocumentNeverImplementedException(format("%s - %s", location, THIS_SPECIFICATION_WAS_NEVER_SET_AS_IMPLEMENTED)); } } } return loadHtmlDocument( jsoupDoc ); } /** {@inheritDoc} */ public void setDocumentAsImplemeted(String location) throws Exception { Vector args = CollectionUtil.toVector( username , password, args(URI.create(URIUtil.raw(location)))); XmlRpcClient xmlrpc = getXmlRpcClient(); String msg = (String)xmlrpc.execute( new XmlRpcRequest( handler + ".setSpecificationAsImplemented", args ) ); if(!("".equals(msg))) throw new Exception(msg); } @SuppressWarnings("unchecked") @Override public DocumentNode getSpecificationsHierarchy(String project, String systemUnderTest) throws Exception { if (isAnyEmpty(project, systemUnderTest)) { LOGGER.warn("The 'projectName' or the 'systemUnderTest' is empty. We will not be able to list the server's specifications."); return new DocumentNode("EMPTY"); } XmlRpcClient xmlrpc = getXmlRpcClient(); @SuppressWarnings("unchecked") List> sutList = (List>) xmlrpc.execute(new XmlRpcRequest(handler + ".getSystemUnderTestsOfProject", toArgs(project))); List selectedSUT = null; for (List sut : sutList) { String SutName = (String)sut.get(SUTNAME_INDEX); if (StringUtils.equals(SutName, systemUnderTest)) { selectedSUT = sut; break; } } if (selectedSUT == null) { throw new RepositoryException(format("SUT %s not found in the project %s", systemUnderTest, project)); } List list = (List) xmlrpc.execute( new XmlRpcRequest(handler + ".getSpecificationHierarchy", toArgs(getSelectedRepository(), selectedSUT))); return DocumentNode.toDocumentNode(list); } @Override public void setSpecificationDialect(SpecificationDialect specificationDialect) { this.specificationDialect = specificationDialect; } /** {@inheritDoc} */ public List listDocuments(String uri) { return new ArrayList(); } /** *

listDocumentsInHierarchy.

* * @return a {@link java.util.List} object. * @throws java.lang.Exception if any. */ @SuppressWarnings("unchecked") public List listDocumentsInHierarchy() throws Exception { Vector args = CollectionUtil.toVector( username , password, CollectionUtil.toVector(getRepositoryName())); XmlRpcClient xmlrpc = getXmlRpcClient(); return (Vector)xmlrpc.execute( new XmlRpcRequest( handler + ".getSpecificationHierarchy", args ) ); } private String retrieveSpecification(URI location) throws Exception { Vector args = CollectionUtil.toVector( username , password, args(location)); XmlRpcClient xmlrpc = getXmlRpcClient(); return (String) xmlrpc.execute( new XmlRpcRequest( handler + ".getRenderedSpecification", args ) ); } private Document loadHtmlDocument(com.greenpepper.shaded.org.jsoup.nodes.Document content ) throws IOException { Element head = content.head(); // I will put an encoding UTF8 per default if (head.select("meta[charset]").isEmpty() && head.select("meta[http-equiv=Content-Type]").isEmpty() ) { head.append(""); } // I will also remove any node from the HEAD head.select("base").remove(); Reader reader = new StringReader( content.outerHtml() ); try { return HtmlDocumentBuilder.tablesAndLists().withDialect(specificationDialect).build( reader ); } finally { IOUtil.closeQuietly( reader ); } } private Vector args(URI location) { final String[] locationArgs = location.getPath().split("/"); final String implemented = URIUtil.getAttribute(location, "implemented"); final boolean runImplemented; if (isNotBlank(implemented)) { runImplemented = Boolean.valueOf(implemented); } else { runImplemented = !runNotImplemented; } ArrayList args = new ArrayList(){{ add(getRepositoryName()); addAll(Arrays.asList(locationArgs)); add(includeStyle); add(runImplemented); }}; return toArgs(args.toArray()); } private Vector toArgs(Object... options) { Vector args = new Vector(); Collections.addAll(args, options); return args; } private XmlRpcClient getXmlRpcClient() throws MalformedURLException { if (xmlRpcClient == null) { xmlRpcClient = new XmlRpcClient( root.getScheme() + "://" + root.getAuthority() + root.getPath() ); } return xmlRpcClient; } private List getSelectedRepository() throws IOException, XmlRpcException, RepositoryException { if (selectedRepository == null) { @SuppressWarnings("unchecked") List> repoList = (List>) getXmlRpcClient() .execute(new XmlRpcRequest(handler + ".getAllSpecificationRepositories", toArgs())); for (List repo : repoList) { if (StringUtils.equals(CONFLUENCE + getRepositoryName(), (String) repo.get(REPOSITORY_UID_INDEX))){ selectedRepository = repo; break; } } if (selectedRepository == null) { throw new RepositoryException(format("SpecificationRepository %s not found", getRepositoryName())); } } return selectedRepository; } private String getRepositoryName() { return root.getFragment(); } }