2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.sxp_ise_adapter.impl;
11 import static org.powermock.api.support.membermodification.MemberMatcher.method;
12 import static org.powermock.api.support.membermodification.MemberModifier.stub;
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;
48 * Test for {@link GbpIseSgtHarvesterImpl}.
50 @RunWith(PowerMockRunner.class)
51 @PrepareForTest({RestClientFactory.class})
52 public class GbpIseSgtHarvesterImplTest {
54 private static final Logger LOG = LoggerFactory.getLogger(GbpIseSgtHarvesterImplTest.class);
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;
62 private SgtInfoProcessor processor;
64 private Client client;
66 private WebResource webResource;
68 private WebResource.Builder builder;
70 private ClientResponse response;
72 private IseSourceConfig config;
73 private IseContext iseContext;
75 private GbpIseSgtHarvesterImpl harvester;
77 public GbpIseSgtHarvesterImplTest() throws IOException {
78 iseReplyAllSgts = readLocalResource("./rawIse-allSgts.xml");
79 iseReplySgtDetail = readLocalResource("./rawIse-sgtDetail.xml");
82 private static String readLocalResource(final String resourcePath) throws IOException {
83 final StringBuilder collector = new StringBuilder();
85 final InputStream iseReplySource = GbpIseSgtHarvesterImplTest.class.getResourceAsStream(resourcePath);
86 final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(iseReplySource))
89 while ((line = bufferedReader.readLine()) != null) {
90 collector.append(line);
93 return collector.toString();
97 public void setUp() throws Exception {
98 config = new IseSourceConfigBuilder()
100 .setConnectionConfig(new ConnectionConfigBuilder()
101 .setConnectionTimeout(10)
103 .setHeader(Collections.singletonList(new HeaderBuilder()
107 .setIseRestUrl(ISE_REST_URL)
111 iseContext = new IseContext(config);
112 harvester = new GbpIseSgtHarvesterImpl(processor);
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);
124 Mockito.when(processor.processSgtInfo(Matchers.eq(TENANT_ID), Matchers.<List<SgtInfo>>any())).thenReturn(
125 Futures.immediateCheckedFuture(null));
127 final ListenableFuture<Collection<SgtInfo>> harvestResult = harvester.harvestAll(iseContext);
129 final InOrder inOrder = Mockito.inOrder(client, webResource, builder);
130 inOrder.verify(client).resource(ISE_REST_URL.getValue());
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);
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();
143 final Collection<SgtInfo> count = harvestResult.get(2, TimeUnit.SECONDS);
144 Assert.assertEquals(1, count.size());