features-test to test that features load correctly.
[yangtools.git] / common / features-test / src / main / java / org / opendaylight / yangtools / featuretest / SingleFeatureTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.featuretest;
9
10 import static org.opendaylight.yangtools.featuretest.Constants.ORG_OPENDAYLIGHT_FEATURETEST_FEATURENAME_PROP;
11 import static org.opendaylight.yangtools.featuretest.Constants.ORG_OPENDAYLIGHT_FEATURETEST_FEATUREVERSION_PROP;
12 import static org.opendaylight.yangtools.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;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.net.URI;
24 import java.net.URISyntaxException;
25
26 import javax.inject.Inject;
27
28 import org.apache.karaf.features.Feature;
29 import org.apache.karaf.features.FeaturesService;
30 import org.apache.karaf.features.Repository;
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.ops4j.pax.exam.Configuration;
36 import org.ops4j.pax.exam.CoreOptions;
37 import org.ops4j.pax.exam.Option;
38 import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42
43 @RunWith(PerRepoTestRunner.class)
44 public class SingleFeatureTest {
45     private static final String MAVEN_REPO_LOCAL = "maven.repo.local";
46     private static final String ORG_OPS4J_PAX_URL_MVN_LOCAL_REPOSITORY = "org.ops4j.pax.url.mvn.localRepository";
47     private static final String ETC_ORG_OPS4J_PAX_URL_MVN_CFG = "etc/org.ops4j.pax.url.mvn.cfg";
48     private static final String LOG4J_LOGGER_ORG_OPENDAYLIGHT_YANGTOOLS_FEATURETEST = "log4j.logger.org.opendaylight.yangtools.featuretest";
49     private static final Logger LOG = LoggerFactory.getLogger(SingleFeatureTest.class);
50
51     /*
52      * File name to add our logging config property too.
53      */
54     private static final String ORG_OPS4J_PAX_LOGGING_CFG = "etc/org.ops4j.pax.logging.cfg";
55
56     /*
57      * Default values for karaf distro version, type, groupId, and artifactId
58      */
59     private static final String KARAF_DISTRO_VERSION = "3.0.1";
60     private static final String KARAF_DISTRO_TYPE = "zip";
61     private static final String KARAF_DISTRO_ARTIFACTID = "apache-karaf";
62     private static final String KARAF_DISTRO_GROUPID = "org.apache.karaf";
63
64     /*
65      * Property names to override defaults for karaf distro artifactId, groupId, version, and type
66      */
67     private static final String KARAF_DISTRO_VERSION_PROP = "karaf.distro.version";
68     private static final String KARAF_DISTRO_TYPE_PROP = "karaf.distro.type";
69     private static final String KARAF_DISTRO_ARTIFACTID_PROP = "karaf.distro.artifactId";
70     private static final String KARAF_DISTRO_GROUPID_PROP = "karaf.distro.groupId";
71
72
73    @Inject
74    private FeaturesService featuresService;
75
76     @Configuration
77     public Option[] config() throws IOException {
78        return new Option[] {
79              getKarafDistroOption(),
80              keepRuntimeFolder(),
81              configureConsole().ignoreLocalConsole(),
82              logLevel(LogLevel.WARN),
83              mvnLocalRepoOption(),
84              editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,LOG4J_LOGGER_ORG_OPENDAYLIGHT_YANGTOOLS_FEATURETEST,LogLevel.INFO.name()),
85              CoreOptions.systemProperty(ORG_OPENDAYLIGHT_FEATURETEST_URI_PROP).value(System.getProperty(ORG_OPENDAYLIGHT_FEATURETEST_URI_PROP)),
86              CoreOptions.systemProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATURENAME_PROP).value(System.getProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATURENAME_PROP)),
87              CoreOptions.systemProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATUREVERSION_PROP).value(System.getProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATUREVERSION_PROP)),
88        };
89     }
90
91     protected Option mvnLocalRepoOption() {
92         String mvnRepoLocal = System.getProperty(MAVEN_REPO_LOCAL, "");
93         LOG.info("mvnLocalRepo \"{}\"",mvnRepoLocal);
94         Option option =
95                 editConfigurationFilePut(ETC_ORG_OPS4J_PAX_URL_MVN_CFG,ORG_OPS4J_PAX_URL_MVN_LOCAL_REPOSITORY,mvnRepoLocal);
96         return option;
97     }
98
99     protected Option getKarafDistroOption() {
100         String groupId = System.getProperty(KARAF_DISTRO_GROUPID_PROP,KARAF_DISTRO_GROUPID);
101         String artifactId = System.getProperty(KARAF_DISTRO_ARTIFACTID_PROP,KARAF_DISTRO_ARTIFACTID);
102         String version = System.getProperty(KARAF_DISTRO_VERSION_PROP,KARAF_DISTRO_VERSION);
103         String type = System.getProperty(KARAF_DISTRO_TYPE_PROP,KARAF_DISTRO_TYPE);
104         LOG.info("Using karaf distro {} {} {} {}",groupId,artifactId,version,type);
105         return karafDistributionConfiguration()
106                 .frameworkUrl(
107                         maven()
108                                 .groupId(groupId)
109                                 .artifactId(artifactId)
110                                 .type(type)
111                                 .version(version))
112                .name("OpenDaylight")
113                .unpackDirectory(new File("target/pax"))
114                .useDeployFolder(false);
115     }
116
117     private URI getRepoURI() throws URISyntaxException {
118         return new URI(getProperty(ORG_OPENDAYLIGHT_FEATURETEST_URI_PROP));
119     }
120
121     private String getFeatureName() {
122         return getProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATURENAME_PROP);
123     }
124
125     public String getFeatureVersion() {
126         return getProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATUREVERSION_PROP);
127     }
128
129     private String getProperty(final String propName) {
130         String prop = System.getProperty(propName);
131         Assert.assertTrue("Missing property :" +propName, prop!=null);
132         return prop;
133     }
134
135     private void checkRepository(final URI repoURI) {
136         Repository repo = null;
137         for(Repository r: featuresService.listRepositories()) {
138             if(r.getURI().equals(repoURI)){
139                 repo = r;
140                 break;
141             }
142         }
143         Assert.assertNotNull("Repository not found: " + repoURI,repo);
144     }
145
146     @Before
147     public void installRepo() throws Exception {
148         LOG.info("Attempting to add repository {}", getRepoURI());
149         featuresService.addRepository(getRepoURI());
150         checkRepository(getRepoURI());
151         LOG.info("Successfully loaded repository {}", getRepoURI());
152     }
153
154     @Test
155     public void installFeature() throws Exception {
156       LOG.info("Attempting to install feature {} {}", getFeatureName(),getFeatureVersion());
157       featuresService.installFeature(getFeatureName(), getFeatureVersion());
158       Feature f = featuresService.getFeature(getFeatureName(), getFeatureVersion());
159       Assert.assertNotNull("Attempt to get feature "+ getFeatureName() + " " + getFeatureVersion() + "resulted in null" , f);
160       Assert.assertTrue("Failed to install Feature: " + getFeatureName() + " " + getFeatureVersion(),featuresService.isInstalled(f));
161       LOG.info("Successfull installed feature {} {}", getFeatureName(),getFeatureVersion());
162     }
163 }