2 * Copyright (c) 2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package io.fd.honeycomb.nat;
19 import static org.hamcrest.CoreMatchers.is;
20 import static org.hamcrest.CoreMatchers.not;
21 import static org.hamcrest.Matchers.empty;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertThat;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.MockitoAnnotations.initMocks;
27 import com.google.inject.Guice;
28 import com.google.inject.Inject;
29 import com.google.inject.Provider;
30 import com.google.inject.multibindings.Multibinder;
31 import com.google.inject.name.Named;
32 import com.google.inject.testing.fieldbinder.Bind;
33 import com.google.inject.testing.fieldbinder.BoundFieldModule;
34 import io.fd.honeycomb.translate.read.ReaderFactory;
35 import io.fd.honeycomb.translate.impl.read.registry.CompositeReaderRegistryBuilder;
36 import io.fd.honeycomb.translate.impl.write.registry.FlatWriterRegistryBuilder;
37 import io.fd.honeycomb.translate.vpp.util.NamingContext;
38 import io.fd.honeycomb.translate.write.WriterFactory;
39 import io.fd.vpp.jvpp.snat.future.FutureJVppSnatFacade;
40 import java.util.HashSet;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.mockito.Mock;
45 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesStateBuilder;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 public class NatModuleTest {
54 @Named("honeycomb-context")
57 private DataBroker honeycombContext;
60 private ReaderFactory ietfIfcReaderFactory;
62 @Named("honeycomb-initializer")
65 private DataBroker honeycombInitializer;
67 @Named("interface-context")
69 private NamingContext ifcContext;
72 private Set<ReaderFactory> readerFactories = new HashSet<>();
75 private Set<WriterFactory> writerFactories = new HashSet<>();
78 public void setUp() throws Exception {
79 ietfIfcReaderFactory = registry -> {
80 registry.addStructuralReader(InstanceIdentifier.create(InterfacesState.class), InterfacesStateBuilder.class);
81 registry.addStructuralReader(InstanceIdentifier.create(InterfacesState.class).child(Interface.class), InterfaceBuilder.class);
84 ifcContext = new NamingContext("interface-", "interface-context");
85 // Nat Module adds readers under InterfacesState/Interface and since readers for parents that do nothing need to
86 // be present, add structural readers (or add V3poModule here, but adding the full Module is not the best solution)
87 Guice.createInjector(binder -> Multibinder.newSetBinder(binder, ReaderFactory.class)
88 .addBinding().toInstance(registry -> {
89 registry.addStructuralReader(InstanceIdentifier.create(InterfacesState.class),
90 InterfacesStateBuilder.class);
91 registry.addStructuralReader(InstanceIdentifier.create(InterfacesState.class).child(Interface.class),
92 InterfaceBuilder.class);
93 }), new NatModule(MockJVppSnatProvider.class), BoundFieldModule.of(this)).injectMembers(this);
97 public void testReaderFactories() throws Exception {
98 assertThat(readerFactories, is(not(empty())));
100 // Test registration process (all dependencies present, topological order of readers does exist, etc.)
101 final CompositeReaderRegistryBuilder registryBuilder = new CompositeReaderRegistryBuilder();
102 readerFactories.forEach(factory -> factory.init(registryBuilder));
103 assertNotNull(registryBuilder.build());
107 public void testWriterFactories() throws Exception {
108 assertThat(writerFactories, is(not(empty())));
110 // Test registration process (all dependencies present, topological order of writers does exist, etc.)
111 final FlatWriterRegistryBuilder registryBuilder = new FlatWriterRegistryBuilder();
112 writerFactories.forEach(factory -> factory.init(registryBuilder));
113 assertNotNull(registryBuilder.build());
116 private static final class MockJVppSnatProvider implements Provider<FutureJVppSnatFacade> {
119 public FutureJVppSnatFacade get() {
120 return mock(FutureJVppSnatFacade.class);