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.Collections;
24 import java.util.List;
25 import java.util.concurrent.TimeUnit;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.InOrder;
31 import org.mockito.Matchers;
32 import org.mockito.Mock;
33 import org.mockito.Mockito;
34 import org.opendaylight.groupbasedpolicy.sxp_ise_adapter.impl.util.RestClientFactory;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
37 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;
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.IseSourceConfigBuilder;
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.ise.source.config.ConnectionConfigBuilder;
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.connection.config.HeaderBuilder;
41 import org.powermock.core.classloader.annotations.PrepareForTest;
42 import org.powermock.modules.junit4.PowerMockRunner;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * Test for {@link GbpIseSgtHarvesterImpl}.
49 @RunWith(PowerMockRunner.class)
50 @PrepareForTest({RestClientFactory.class})
51 public class GbpIseSgtHarvesterImplTest {
53 private static final Logger LOG = LoggerFactory.getLogger(GbpIseSgtHarvesterImplTest.class);
55 public static final TenantId TENANT_ID = new TenantId("unit-tenant-id-1");
56 public static final Uri ISE_REST_URL = new Uri("https://example.org:9060");
57 public final String iseReplyAllSgts;
58 public final String iseReplySgtDetail;
61 private SgtInfoProcessor processor;
63 private Client client;
65 private WebResource webResource;
67 private WebResource.Builder builder;
69 private ClientResponse response;
71 private IseSourceConfig config;
73 private GbpIseSgtHarvesterImpl harvester;
75 public GbpIseSgtHarvesterImplTest() throws IOException {
76 iseReplyAllSgts = readLocalResource("./rawIse-allSgts.xml");
77 iseReplySgtDetail = readLocalResource("./rawIse-sgtDetail.xml");
80 private static String readLocalResource(final String resourcePath) throws IOException {
81 final StringBuilder collector = new StringBuilder();
83 final InputStream iseReplySource = GbpIseSgtHarvesterImplTest.class.getResourceAsStream(resourcePath);
84 final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(iseReplySource))
87 while ((line = bufferedReader.readLine()) != null) {
88 collector.append(line);
91 return collector.toString();
95 public void setUp() throws Exception {
96 config = new IseSourceConfigBuilder()
98 .setConnectionConfig(new ConnectionConfigBuilder()
99 .setConnectionTimeout(10)
101 .setHeader(Collections.singletonList(new HeaderBuilder()
105 .setIseRestUrl(ISE_REST_URL)
109 harvester = new GbpIseSgtHarvesterImpl(processor);
113 public void testHarvest() throws Exception {
114 Mockito.when(response.getEntity(String.class)).thenReturn(iseReplyAllSgts, iseReplySgtDetail);
115 Mockito.when(builder.get(Matchers.<Class<ClientResponse>>any())).thenReturn(response);
116 Mockito.when(webResource.getRequestBuilder()).thenReturn(builder);
117 Mockito.when(webResource.path(Matchers.anyString())).thenReturn(webResource);
118 Mockito.when(client.resource(Matchers.<String>any())).thenReturn(webResource);
119 stub(method(RestClientFactory.class, "createIseClient")).toReturn(client);
121 Mockito.when(processor.processSgtInfo(Matchers.eq(TENANT_ID), Matchers.<List<SgtInfo>>any())).thenReturn(
122 Futures.immediateCheckedFuture(null));
124 final ListenableFuture<Integer> harvestResult = harvester.harvest(config);
126 final InOrder inOrder = Mockito.inOrder(client, webResource, builder);
127 inOrder.verify(client).resource(ISE_REST_URL.getValue());
129 inOrder.verify(webResource).path("/ers/config/sgt");
130 inOrder.verify(webResource).getRequestBuilder();
131 inOrder.verify(builder).header(Matchers.anyString(),Matchers.anyString());
132 inOrder.verify(builder).get(ClientResponse.class);
134 inOrder.verify(webResource).path("/ers/config/sgt/abc123");
135 inOrder.verify(webResource).getRequestBuilder();
136 inOrder.verify(builder).header(Matchers.anyString(),Matchers.anyString());
137 inOrder.verify(builder).get(ClientResponse.class);
138 inOrder.verifyNoMoreInteractions();
140 final Integer count = harvestResult.get(2, TimeUnit.SECONDS);
141 Assert.assertEquals(1, count.intValue());