Skip to main content
summaryrefslogtreecommitdiffstats
blob: f726dc577f986c2b4570030ef25eb893353648ba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*******************************************************************************
 * Copyright (c) 2014 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.jaxrs.client;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.net.URI;
import java.net.URISyntaxException;
import javax.ws.rs.client.WebTarget;
import org.eclipse.osee.jaxrs.client.JaxRsClient.JaxRsClientFactory;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

/**
 * Test Case for {@link JaxRsClient}
 * 
 * @author Roberto E. Escobar
 */
public class JaxRsClientTest {

   private static final String URI_STRING = "hello";
   private static final String URI = "http://hello.com";

   //@formatter:off
   @Mock private JaxRsClientFactory factory;
   @Mock private JaxRsClientConfig config;
   @Mock private WebTarget target;
   //@formatter:on

   private JaxRsClient client;

   @Before
   public void setup() {
      MockitoAnnotations.initMocks(this);

      client = new JaxRsClient(factory, config);
   }

   @Test
   public void testTargetEmpty() {
      when(factory.newTarget(config, null)).thenReturn(target);

      WebTarget actual = client.target();
      assertEquals(target, actual);
      verify(factory).newTarget(config, null);
   }

   @Test
   public void testTargetString() {
      when(factory.newTarget(config, URI_STRING)).thenReturn(target);

      WebTarget actual = client.target(URI_STRING);
      assertEquals(target, actual);
      verify(factory).newTarget(config, URI_STRING);
   }

   @Test
   public void testTargetURI() throws URISyntaxException {
      URI expectedUri = new URI(URI);

      when(factory.newTarget(config, URI)).thenReturn(target);

      WebTarget actual = client.target(expectedUri);
      assertEquals(target, actual);
      verify(factory).newTarget(config, URI);
   }

   @Test
   public void testTargetProxyString() {
      String instance = URI_STRING;
      Class<?> clazz = String.class;

      when(factory.newClient(config, URI_STRING, clazz)).thenAnswer(answer(instance));

      Object actual = client.targetProxy(URI_STRING, clazz);
      assertEquals(instance, actual);
      verify(factory).newClient(config, URI_STRING, clazz);
   }

   @Test
   public void testTargetProxyURI() throws URISyntaxException {
      URI expectedUri = new URI(URI);
      String instance = URI_STRING;
      Class<?> clazz = String.class;

      when(factory.newClient(config, URI, clazz)).thenAnswer(answer(instance));

      Object actual = client.targetProxy(expectedUri, clazz);
      assertEquals(instance, actual);
      verify(factory).newClient(config, URI, clazz);
   }

   private static <T> Answer<T> answer(final T object) {
      return new Answer<T>() {

         @Override
         public T answer(InvocationOnMock invocation) throws Throwable {
            return object;
         }
      };
   }
}

Back to the top