2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.odlparent.featuretest;
10 import static org.opendaylight.odlparent.featuretest.Constants.ORG_OPENDAYLIGHT_FEATURETEST_FEATURENAME_PROP;
11 import static org.opendaylight.odlparent.featuretest.Constants.ORG_OPENDAYLIGHT_FEATURETEST_FEATUREVERSION_PROP;
12 import static org.opendaylight.odlparent.featuretest.Constants.ORG_OPENDAYLIGHT_FEATURETEST_URI_PROP;
13 import static org.ops4j.pax.exam.CoreOptions.maven;
14 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureConsole;
15 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
16 //import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.debugConfiguration;
17 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
18 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
19 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;
22 import java.io.IOException;
24 import java.net.URISyntaxException;
25 import javax.inject.Inject;
26 import org.apache.karaf.features.Feature;
27 import org.apache.karaf.features.FeaturesService;
28 import org.apache.karaf.features.Repository;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.ops4j.pax.exam.Configuration;
34 import org.ops4j.pax.exam.CoreOptions;
35 import org.ops4j.pax.exam.Option;
36 import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 @RunWith(PerRepoTestRunner.class)
42 public class SingleFeatureTest {
43 private static final String MAVEN_REPO_LOCAL = "maven.repo.local";
44 private static final String ORG_OPS4J_PAX_URL_MVN_LOCAL_REPOSITORY = "org.ops4j.pax.url.mvn.localRepository";
45 private static final String ORG_OPS4J_PAX_URL_MVN_REPOSITORIES = "org.ops4j.pax.url.mvn.repositories";
46 private static final String ETC_ORG_OPS4J_PAX_URL_MVN_CFG = "etc/org.ops4j.pax.url.mvn.cfg";
47 private static final String LOG4J_LOGGER_ORG_OPENDAYLIGHT_YANGTOOLS_FEATURETEST = "log4j.logger.org.opendaylight.odlparent.featuretest";
48 private static final Logger LOG = LoggerFactory.getLogger(SingleFeatureTest.class);
51 * File name to add our logging config property too.
53 private static final String ORG_OPS4J_PAX_LOGGING_CFG = "etc/org.ops4j.pax.logging.cfg";
56 * Default values for karaf distro version, type, groupId, and artifactId
58 private static final String KARAF_DISTRO_VERSION = "3.0.2";
59 private static final String KARAF_DISTRO_TYPE = "zip";
60 private static final String KARAF_DISTRO_ARTIFACTID = "apache-karaf";
61 private static final String KARAF_DISTRO_GROUPID = "org.apache.karaf";
64 * Property names to override defaults for karaf distro artifactId, groupId, version, and type
66 private static final String KARAF_DISTRO_VERSION_PROP = "karaf.distro.version";
67 private static final String KARAF_DISTRO_TYPE_PROP = "karaf.distro.type";
68 private static final String KARAF_DISTRO_ARTIFACTID_PROP = "karaf.distro.artifactId";
69 private static final String KARAF_DISTRO_GROUPID_PROP = "karaf.distro.groupId";
73 * List of karaf 3.0.2 default maven repositories with snapshot repositories excluded.
75 * Unfortunately this must be hard-coded since declarative model which uses Options,
76 * does not allow us to read value, parse it (properties has allways
77 * problems with lists) and construct replacement string which does
78 * not contains snapshots.
81 private static final String EXTERNAL_DEFAULT_REPOSITORIES = "http://repo1.maven.org/maven2@id=central, " +
82 "http://repository.springsource.com/maven/bundles/release@id=spring.ebr.release, " +
83 "http://repository.springsource.com/maven/bundles/external@id=spring.ebr.external, " +
84 "http://zodiac.springsource.com/maven/bundles/release@id=gemini ";
88 private FeaturesService featuresService;
91 public Option[] config() throws IOException {
93 getKarafDistroOption(),
95 configureConsole().ignoreLocalConsole(),
96 logLevel(LogLevel.WARN),
98 editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,LOG4J_LOGGER_ORG_OPENDAYLIGHT_YANGTOOLS_FEATURETEST,LogLevel.INFO.name()),
101 * Disables external snapshot repositories.
103 * Pax URL and Karaf by default always search for new version of snapshots
104 * in all snapshots repository, even if that snapshots does not belong to that
105 * repository and maven is invoked even with -nsu (no snapshot update)
108 * This is also true for OpenDaylight snapshot artefacts - pax url tries
109 * to resolve them from third-party repositories, even if they are not present
110 * there - this increases time which takes for features to install.
112 * For more complex projects this actually means several HTTP GETs for each
113 * snapshot bundle referenced, even if the bundle is already present
114 * in local maven repository.
116 * In order to speed-up installation and remove unnecessary network traffic,
117 * which fails for obvious reasons, external snapshot repositories are
122 disableExternalSnapshotRepositories(),
123 CoreOptions.systemProperty(ORG_OPENDAYLIGHT_FEATURETEST_URI_PROP).value(System.getProperty(ORG_OPENDAYLIGHT_FEATURETEST_URI_PROP)),
124 CoreOptions.systemProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATURENAME_PROP).value(System.getProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATURENAME_PROP)),
125 CoreOptions.systemProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATUREVERSION_PROP).value(System.getProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATUREVERSION_PROP)),
131 * Disables snapshot repositories, which are enabled by default in karaf distribution.
135 * @return Edit Configuration option which removes external snapshot repositories.
137 private static Option disableExternalSnapshotRepositories() {
138 return editConfigurationFilePut(ETC_ORG_OPS4J_PAX_URL_MVN_CFG, ORG_OPS4J_PAX_URL_MVN_REPOSITORIES,EXTERNAL_DEFAULT_REPOSITORIES);
141 protected Option mvnLocalRepoOption() {
142 String mvnRepoLocal = System.getProperty(MAVEN_REPO_LOCAL, "");
143 LOG.info("mvnLocalRepo \"{}\"",mvnRepoLocal);
145 editConfigurationFilePut(ETC_ORG_OPS4J_PAX_URL_MVN_CFG,ORG_OPS4J_PAX_URL_MVN_LOCAL_REPOSITORY,mvnRepoLocal);
149 protected Option getKarafDistroOption() {
150 String groupId = System.getProperty(KARAF_DISTRO_GROUPID_PROP,KARAF_DISTRO_GROUPID);
151 String artifactId = System.getProperty(KARAF_DISTRO_ARTIFACTID_PROP,KARAF_DISTRO_ARTIFACTID);
152 String version = System.getProperty(KARAF_DISTRO_VERSION_PROP,KARAF_DISTRO_VERSION);
153 String type = System.getProperty(KARAF_DISTRO_TYPE_PROP,KARAF_DISTRO_TYPE);
154 LOG.info("Using karaf distro {} {} {} {}",groupId,artifactId,version,type);
155 return karafDistributionConfiguration()
159 .artifactId(artifactId)
162 .name("OpenDaylight")
163 .unpackDirectory(new File("target/pax"))
164 .useDeployFolder(false);
167 private URI getRepoURI() throws URISyntaxException {
168 return new URI(getProperty(ORG_OPENDAYLIGHT_FEATURETEST_URI_PROP));
171 private String getFeatureName() {
172 return getProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATURENAME_PROP);
175 public String getFeatureVersion() {
176 return getProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATUREVERSION_PROP);
179 private String getProperty(final String propName) {
180 String prop = System.getProperty(propName);
181 Assert.assertTrue("Missing property :" +propName, prop!=null);
185 private void checkRepository(final URI repoURI) {
186 Repository repo = null;
187 for(Repository r: featuresService.listRepositories()) {
188 if(r.getURI().equals(repoURI)){
193 Assert.assertNotNull("Repository not found: " + repoURI,repo);
197 public void installRepo() throws Exception {
198 LOG.info("Attempting to add repository {}", getRepoURI());
199 featuresService.addRepository(getRepoURI());
200 checkRepository(getRepoURI());
201 LOG.info("Successfully loaded repository {}", getRepoURI());
205 public void installFeature() throws Exception {
206 LOG.info("Attempting to install feature {} {}", getFeatureName(),getFeatureVersion());
207 featuresService.installFeature(getFeatureName(), getFeatureVersion());
208 Feature f = featuresService.getFeature(getFeatureName(), getFeatureVersion());
209 Assert.assertNotNull("Attempt to get feature "+ getFeatureName() + " " + getFeatureVersion() + "resulted in null" , f);
210 Assert.assertTrue("Failed to install Feature: " + getFeatureName() + " " + getFeatureVersion(),featuresService.isInstalled(f));
211 LOG.info("Successfull installed feature {} {}", getFeatureName(),getFeatureVersion());