refactored lispDAO
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / LispMappingServiceCliTest.java
1 /*
2  * Copyright (c) 2014 Contextream, 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
9 package org.opendaylight.lispflowmapping.implementation;
10
11 import java.util.HashMap;
12 import java.util.Map;
13 import java.util.Map.Entry;
14
15 import junitx.framework.StringAssert;
16
17 import org.jmock.api.Invocation;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
21 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
22 import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
23 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
24 import org.opendaylight.lispflowmapping.tools.junit.MockCommandInterpreter;
25
26 public class LispMappingServiceCliTest extends BaseTestCase {
27     private LispMappingService testedLispMappingService;
28     private ILispDAO dao;
29     private Map<Object, Object> visitorExecutions;
30     private MockCommandInterpreter mockCommandInterpreter;
31
32     @Override
33     @Before
34     public void before() throws Exception {
35         super.before();
36
37         testedLispMappingService = new LispMappingService();
38         dao = context.mock(ILispDAO.class);
39         inject(testedLispMappingService, "lispDao", dao);
40         mockCommandInterpreter = new MockCommandInterpreter();
41
42         visitorExecutions = new HashMap<Object, Object>();
43         allowing(dao).getAll(wany(IRowVisitor.class));
44         will(new SimpleAction() {
45             @Override
46             public Object invoke(Invocation invocation) throws Throwable {
47                 IRowVisitor visitor = (IRowVisitor) invocation.getParameter(0);
48                 for (Entry<Object, Object> entry : visitorExecutions.entrySet()) {
49                     visitor.visitRow(entry.getKey(), "IP", entry.getValue());
50                 }
51                 return null;
52             }
53         });
54     }
55
56     @Test
57     public void remove__Basic() throws Exception {
58         mockCommandInterpreter.addArgument("1.2.3.4");
59         oneOf(dao).remove(LispAFIConvertor.asIPAfiAddress("1.2.3.4"));
60         testedLispMappingService._removeEid(mockCommandInterpreter);
61     }
62
63     @Test
64     public void oneEntryInDb() throws Exception {
65         prepareVisitorResult("1.2.3.4", "9.8.7.6");
66
67         testedLispMappingService._dumpAll(mockCommandInterpreter);
68
69         String console = mockCommandInterpreter.getPrints().toString();
70         StringAssert.assertStartsWith("EID\tRLOCs\n", console);
71         StringAssert.assertContains("Ipv4", console);
72     }
73
74     @Test
75     public void twoEntriesInDb() throws Exception {
76         prepareVisitorResult("7.2.3.4", "9.8.7.6");
77         prepareVisitorResult("1.2.3.4", "9.8.7.6");
78         testedLispMappingService._dumpAll(mockCommandInterpreter);
79
80         String console = mockCommandInterpreter.getPrints().toString();
81         StringAssert.assertStartsWith("EID\tRLOCs\n", console);
82         StringAssert.assertContains("Ipv4", console);
83         StringAssert.assertContains("Ipv4", console);
84     }
85
86     private void prepareVisitorResult(final String key, final String value) {
87         visitorExecutions.put(LispAFIConvertor.asIPAfiAddress(key), LispAFIConvertor.asIPAfiAddress(value));
88     }
89 }