Merge "Fixed Karaf Distribution Archetype add dependent bundles"
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / test / java / org / opendaylight / controller / remote / rpc / RemoteRpcProviderConfigTest.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.controller.remote.rpc;
9
10 import akka.actor.ActorSystem;
11 import akka.actor.Props;
12 import akka.actor.UntypedActor;
13 import akka.testkit.TestActorRef;
14 import com.typesafe.config.Config;
15 import com.typesafe.config.ConfigFactory;
16 import org.junit.Assert;
17 import org.junit.Test;
18 import org.opendaylight.controller.cluster.common.actor.AkkaConfigurationReader;
19 import scala.concurrent.duration.FiniteDuration;
20
21 import java.io.File;
22 import java.util.concurrent.TimeUnit;
23
24 public class RemoteRpcProviderConfigTest {
25
26     @Test
27     public void testConfigDefaults() {
28
29         Config c = ConfigFactory.parseFile(new File("application.conf"));
30         RemoteRpcProviderConfig config = new RemoteRpcProviderConfig.Builder("unit-test").build();
31
32         //Assert on configurations from common config
33         Assert.assertFalse(config.isMetricCaptureEnabled()); //should be disabled by default
34         Assert.assertNotNull(config.getMailBoxCapacity());
35         Assert.assertNotNull(config.getMailBoxName());
36         Assert.assertNotNull(config.getMailBoxPushTimeout());
37
38         //rest of the configurations should be set
39         Assert.assertNotNull(config.getActorSystemName());
40         Assert.assertNotNull(config.getRpcBrokerName());
41         Assert.assertNotNull(config.getRpcBrokerPath());
42         Assert.assertNotNull(config.getRpcManagerName());
43         Assert.assertNotNull(config.getRpcManagerPath());
44         Assert.assertNotNull(config.getRpcRegistryName());
45         Assert.assertNotNull(config.getRpcRegistryPath());
46         Assert.assertNotNull(config.getAskDuration());
47         Assert.assertNotNull(config.getGossipTickInterval());
48
49
50
51     }
52
53     @Test
54     public void testConfigCustomizations() {
55
56         AkkaConfigurationReader reader = new TestConfigReader();
57
58         final int expectedCapacity = 100;
59         String timeOutVal = "10ms";
60         FiniteDuration expectedTimeout = FiniteDuration.create(10, TimeUnit.MILLISECONDS);
61
62         RemoteRpcProviderConfig config = new RemoteRpcProviderConfig.Builder("unit-test")
63                 .metricCaptureEnabled(true)//enable metric capture
64                 .mailboxCapacity(expectedCapacity)
65                 .mailboxPushTimeout(timeOutVal)
66                 .withConfigReader(reader)
67                 .build();
68
69         Assert.assertTrue(config.isMetricCaptureEnabled());
70         Assert.assertEquals(expectedCapacity, config.getMailBoxCapacity().intValue());
71         Assert.assertEquals(expectedTimeout.toMillis(), config.getMailBoxPushTimeout().toMillis());
72
73         //Now check this config inside an actor
74         ActorSystem system = ActorSystem.create("unit-test", config.get());
75         TestActorRef<ConfigTestActor> configTestActorTestActorRef =
76                 TestActorRef.create(system, Props.create(ConfigTestActor.class));
77
78         ConfigTestActor actor = configTestActorTestActorRef.underlyingActor();
79         Config actorConfig = actor.getConfig();
80
81         config = new RemoteRpcProviderConfig(actorConfig);
82
83         Assert.assertTrue(config.isMetricCaptureEnabled());
84         Assert.assertEquals(expectedCapacity, config.getMailBoxCapacity().intValue());
85         Assert.assertEquals(expectedTimeout.toMillis(), config.getMailBoxPushTimeout().toMillis());
86     }
87
88     public static class ConfigTestActor extends UntypedActor {
89
90         private Config actorSystemConfig;
91
92         public ConfigTestActor() {
93             this.actorSystemConfig = getContext().system().settings().config();
94         }
95
96         @Override
97         public void onReceive(Object message) throws Exception {
98         }
99
100         /**
101          * Only for testing. NEVER expose actor's internal state like this.
102          *
103          * @return
104          */
105         public Config getConfig() {
106             return actorSystemConfig;
107         }
108     }
109
110     public static class TestConfigReader implements AkkaConfigurationReader {
111
112         @Override
113         public Config read() {
114             return ConfigFactory.parseResources("application.conf");
115
116         }
117     }
118 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.