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