BUG-6858: adapt to ise api, wire harvestAll to template-provider
[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.IOException;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.concurrent.TimeUnit;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.InOrder;
29 import org.mockito.Matchers;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.opendaylight.groupbasedpolicy.sxp_ise_adapter.impl.util.RestClientFactory;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
35 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;
36 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;
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.ise.source.config.ConnectionConfigBuilder;
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.ise.source.config.connection.config.HeaderBuilder;
39 import org.powermock.core.classloader.annotations.PrepareForTest;
40 import org.powermock.modules.junit4.PowerMockRunner;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * Test for {@link GbpIseSgtHarvesterImpl}.
46  */
47 @RunWith(PowerMockRunner.class)
48 @PrepareForTest({RestClientFactory.class})
49 public class GbpIseSgtHarvesterImplTest {
50
51     private static final Logger LOG = LoggerFactory.getLogger(GbpIseSgtHarvesterImplTest.class);
52
53     public static final TenantId TENANT_ID = new TenantId("unit-tenant-id-1");
54     public static final Uri ISE_REST_URL = new Uri("https://example.org:9060");
55     public final String iseReplyAllSgts;
56     public final String iseReplySgtDetail;
57
58     @Mock
59     private SgtInfoProcessor processor;
60     @Mock
61     private Client client;
62     @Mock
63     private WebResource webResource;
64     @Mock
65     private WebResource.Builder builder;
66     @Mock
67     private ClientResponse response;
68
69     private IseSourceConfig config;
70     private IseContext iseContext;
71
72     private GbpIseSgtHarvesterImpl harvester;
73
74     public GbpIseSgtHarvesterImplTest() throws IOException {
75         iseReplyAllSgts = IseResourceTestHelper.readLocalResource("./rawIse-allSgts1.xml");
76         iseReplySgtDetail = IseResourceTestHelper.readLocalResource("./rawIse-sgtDetail.xml");
77     }
78
79     @Before
80     public void setUp() throws Exception {
81         config = new IseSourceConfigBuilder()
82                 .setTenant(TENANT_ID)
83                 .setConnectionConfig(new ConnectionConfigBuilder()
84                         .setConnectionTimeout(10)
85                         .setReadTimeout(20)
86                         .setHeader(Collections.singletonList(new HeaderBuilder()
87                                 .setName("hName")
88                                 .setValue("hValue")
89                                 .build()))
90                         .setIseRestUrl(ISE_REST_URL)
91                         .build())
92                 .build();
93
94         iseContext = new IseContext(config);
95         harvester = new GbpIseSgtHarvesterImpl(processor);
96     }
97
98     @Test
99     public void testHarvest() throws Exception {
100         Mockito.when(response.getEntity(String.class)).thenReturn(iseReplyAllSgts, iseReplySgtDetail);
101         Mockito.when(builder.get(Matchers.<Class<ClientResponse>>any())).thenReturn(response);
102         Mockito.when(webResource.getRequestBuilder()).thenReturn(builder);
103         Mockito.when(webResource.path(Matchers.anyString())).thenReturn(webResource);
104         Mockito.when(client.resource(Matchers.<String>any())).thenReturn(webResource);
105         stub(method(RestClientFactory.class, "createIseClient")).toReturn(client);
106
107         Mockito.when(processor.processSgtInfo(Matchers.eq(TENANT_ID), Matchers.<List<SgtInfo>>any())).thenReturn(
108                 Futures.immediateCheckedFuture(null));
109
110         final ListenableFuture<Collection<SgtInfo>> harvestResult = harvester.harvestAll(iseContext);
111         final Collection<SgtInfo> addedSgts = harvestResult.get(2, TimeUnit.SECONDS);
112
113         final InOrder inOrder = Mockito.inOrder(client, webResource, builder);
114         inOrder.verify(client).resource(ISE_REST_URL.getValue());
115         // all sgts
116         inOrder.verify(webResource).path("/ers/config/sgt");
117         inOrder.verify(webResource).getRequestBuilder();
118         inOrder.verify(builder).header(Matchers.anyString(),Matchers.anyString());
119         inOrder.verify(builder).get(ClientResponse.class);
120         // sgt detail
121         inOrder.verify(webResource).path("/ers/config/sgt/abc123");
122         inOrder.verify(webResource).getRequestBuilder();
123         inOrder.verify(builder).header(Matchers.anyString(),Matchers.anyString());
124         inOrder.verify(builder).get(ClientResponse.class);
125         inOrder.verifyNoMoreInteractions();
126
127         Assert.assertEquals(1, addedSgts.size());
128     }
129 }