BUG-6858: adapt to ise api, change lookup from ise
[groupbasedpolicy.git] / sxp-integration / sxp-ise-adapter / src / test / java / org / opendaylight / groupbasedpolicy / sxp_ise_adapter / impl / GbpIseSgtHarvesterImplTest.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.groupbasedpolicy.sxp_ise_adapter.impl;
10
11 import static org.powermock.api.support.membermodification.MemberMatcher.method;
12 import static org.powermock.api.support.membermodification.MemberModifier.stub;
13
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.sun.jersey.api.client.Client;
17 import com.sun.jersey.api.client.ClientResponse;
18 import com.sun.jersey.api.client.WebResource;
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.concurrent.TimeUnit;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.InOrder;
32 import org.mockito.Matchers;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.opendaylight.groupbasedpolicy.sxp_ise_adapter.impl.util.RestClientFactory;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ise.adapter.model.rev160630.gbp.sxp.ise.adapter.IseSourceConfig;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ise.adapter.model.rev160630.gbp.sxp.ise.adapter.IseSourceConfigBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ise.adapter.model.rev160630.gbp.sxp.ise.adapter.ise.source.config.ConnectionConfigBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ise.adapter.model.rev160630.gbp.sxp.ise.adapter.ise.source.config.connection.config.HeaderBuilder;
42 import org.powermock.core.classloader.annotations.PrepareForTest;
43 import org.powermock.modules.junit4.PowerMockRunner;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  * Test for {@link GbpIseSgtHarvesterImpl}.
49  */
50 @RunWith(PowerMockRunner.class)
51 @PrepareForTest({RestClientFactory.class})
52 public class GbpIseSgtHarvesterImplTest {
53
54     private static final Logger LOG = LoggerFactory.getLogger(GbpIseSgtHarvesterImplTest.class);
55
56     public static final TenantId TENANT_ID = new TenantId("unit-tenant-id-1");
57     public static final Uri ISE_REST_URL = new Uri("https://example.org:9060");
58     public final String iseReplyAllSgts;
59     public final String iseReplySgtDetail;
60
61     @Mock
62     private SgtInfoProcessor processor;
63     @Mock
64     private Client client;
65     @Mock
66     private WebResource webResource;
67     @Mock
68     private WebResource.Builder builder;
69     @Mock
70     private ClientResponse response;
71
72     private IseSourceConfig config;
73     private IseContext iseContext;
74
75     private GbpIseSgtHarvesterImpl harvester;
76
77     public GbpIseSgtHarvesterImplTest() throws IOException {
78         iseReplyAllSgts = readLocalResource("./rawIse-allSgts.xml");
79         iseReplySgtDetail = readLocalResource("./rawIse-sgtDetail.xml");
80     }
81
82     private static String readLocalResource(final String resourcePath) throws IOException {
83         final StringBuilder collector = new StringBuilder();
84         try (
85                 final InputStream iseReplySource = GbpIseSgtHarvesterImplTest.class.getResourceAsStream(resourcePath);
86                 final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(iseReplySource))
87         ) {
88             String line;
89             while ((line = bufferedReader.readLine()) != null) {
90                 collector.append(line);
91             }
92         }
93         return collector.toString();
94     }
95
96     @Before
97     public void setUp() throws Exception {
98         config = new IseSourceConfigBuilder()
99                 .setTenant(TENANT_ID)
100                 .setConnectionConfig(new ConnectionConfigBuilder()
101                         .setConnectionTimeout(10)
102                         .setReadTimeout(20)
103                         .setHeader(Collections.singletonList(new HeaderBuilder()
104                                 .setName("hName")
105                                 .setValue("hValue")
106                                 .build()))
107                         .setIseRestUrl(ISE_REST_URL)
108                         .build())
109                 .build();
110
111         iseContext = new IseContext(config);
112         harvester = new GbpIseSgtHarvesterImpl(processor);
113     }
114
115     @Test
116     public void testHarvest() throws Exception {
117         Mockito.when(response.getEntity(String.class)).thenReturn(iseReplyAllSgts, iseReplySgtDetail);
118         Mockito.when(builder.get(Matchers.<Class<ClientResponse>>any())).thenReturn(response);
119         Mockito.when(webResource.getRequestBuilder()).thenReturn(builder);
120         Mockito.when(webResource.path(Matchers.anyString())).thenReturn(webResource);
121         Mockito.when(client.resource(Matchers.<String>any())).thenReturn(webResource);
122         stub(method(RestClientFactory.class, "createIseClient")).toReturn(client);
123
124         Mockito.when(processor.processSgtInfo(Matchers.eq(TENANT_ID), Matchers.<List<SgtInfo>>any())).thenReturn(
125                 Futures.immediateCheckedFuture(null));
126
127         final ListenableFuture<Collection<SgtInfo>> harvestResult = harvester.harvestAll(iseContext);
128
129         final InOrder inOrder = Mockito.inOrder(client, webResource, builder);
130         inOrder.verify(client).resource(ISE_REST_URL.getValue());
131         // all sgts
132         inOrder.verify(webResource).path("/ers/config/sgt");
133         inOrder.verify(webResource).getRequestBuilder();
134         inOrder.verify(builder).header(Matchers.anyString(),Matchers.anyString());
135         inOrder.verify(builder).get(ClientResponse.class);
136         // sgt detail
137         inOrder.verify(webResource).path("/ers/config/sgt/abc123");
138         inOrder.verify(webResource).getRequestBuilder();
139         inOrder.verify(builder).header(Matchers.anyString(),Matchers.anyString());
140         inOrder.verify(builder).get(ClientResponse.class);
141         inOrder.verifyNoMoreInteractions();
142
143         final Collection<SgtInfo> count = harvestResult.get(2, TimeUnit.SECONDS);
144         Assert.assertEquals(1, count.size());
145     }
146 }