fc593688582db8cb0b062eb511015502028f8b03
[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.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;
45
46 /**
47  * Test for {@link GbpIseSgtHarvesterImpl}.
48  */
49 @RunWith(PowerMockRunner.class)
50 @PrepareForTest({RestClientFactory.class})
51 public class GbpIseSgtHarvesterImplTest {
52
53     private static final Logger LOG = LoggerFactory.getLogger(GbpIseSgtHarvesterImplTest.class);
54
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;
59
60     @Mock
61     private SgtInfoProcessor processor;
62     @Mock
63     private Client client;
64     @Mock
65     private WebResource webResource;
66     @Mock
67     private WebResource.Builder builder;
68     @Mock
69     private ClientResponse response;
70
71     private IseSourceConfig config;
72
73     private GbpIseSgtHarvesterImpl harvester;
74
75     public GbpIseSgtHarvesterImplTest() throws IOException {
76         iseReplyAllSgts = readLocalResource("./rawIse-allSgts.xml");
77         iseReplySgtDetail = readLocalResource("./rawIse-sgtDetail.xml");
78     }
79
80     private static String readLocalResource(final String resourcePath) throws IOException {
81         final StringBuilder collector = new StringBuilder();
82         try (
83                 final InputStream iseReplySource = GbpIseSgtHarvesterImplTest.class.getResourceAsStream(resourcePath);
84                 final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(iseReplySource))
85         ) {
86             String line;
87             while ((line = bufferedReader.readLine()) != null) {
88                 collector.append(line);
89             }
90         }
91         return collector.toString();
92     }
93
94     @Before
95     public void setUp() throws Exception {
96         config = new IseSourceConfigBuilder()
97                 .setTenant(TENANT_ID)
98                 .setConnectionConfig(new ConnectionConfigBuilder()
99                         .setConnectionTimeout(10)
100                         .setReadTimeout(20)
101                         .setHeader(Collections.singletonList(new HeaderBuilder()
102                                 .setName("hName")
103                                 .setValue("hValue")
104                                 .build()))
105                         .setIseRestUrl(ISE_REST_URL)
106                         .build())
107                 .build();
108
109         harvester = new GbpIseSgtHarvesterImpl(processor);
110     }
111
112     @Test
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);
120
121         Mockito.when(processor.processSgtInfo(Matchers.eq(TENANT_ID), Matchers.<List<SgtInfo>>any())).thenReturn(
122                 Futures.immediateCheckedFuture(null));
123
124         final ListenableFuture<Integer> harvestResult = harvester.harvest(config);
125
126         final InOrder inOrder = Mockito.inOrder(client, webResource, builder);
127         inOrder.verify(client).resource(ISE_REST_URL.getValue());
128         // all sgts
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);
133         // sgt detail
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();
139
140         final Integer count = harvestResult.get(2, TimeUnit.SECONDS);
141         Assert.assertEquals(1, count.intValue());
142     }
143 }