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

©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.