View Javadoc

1   /**
2    * Copyright (c) 2002-2011 "Neo Technology,"
3    * Network Engine for Objects in Lund AB [http://neotechnology.com]
4    *
5    * This file is part of Neo4j.
6    *
7    * Neo4j is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation, either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.neo4j.server.rest.discovery;
21  
22  import static org.hamcrest.Matchers.containsString;
23  import static org.hamcrest.Matchers.greaterThan;
24  import static org.hamcrest.Matchers.is;
25  import static org.hamcrest.Matchers.not;
26  import static org.junit.Assert.assertNotNull;
27  import static org.junit.Assert.assertThat;
28  import static org.mockito.Mockito.mock;
29  import static org.mockito.Mockito.when;
30  
31  import java.net.URI;
32  
33  import javax.ws.rs.core.Response;
34  
35  import org.apache.commons.configuration.Configuration;
36  import org.junit.Ignore;
37  import org.junit.Test;
38  import org.neo4j.server.configuration.Configurator;
39  import org.neo4j.server.rest.repr.formats.JsonFormat;
40  import org.neo4j.server.rest.web.EntityOutputFormat;
41  
42  
43  public class DiscoveryServiceTest {
44      
45      @Test
46      public void shouldReturnValidJSONWithDataAndManagementUris() throws Exception {
47          Configuration mockConfig = mock(Configuration.class);
48          String managementUri = "/management";
49          when(mockConfig.getString(Configurator.MANAGEMENT_PATH_PROPERTY_KEY, Configurator.DEFAULT_MANAGEMENT_API_PATH)).thenReturn(managementUri);
50          String dataUri = "/data";
51          when(mockConfig.getString(Configurator.DATA_API_PATH_PROPERTY_KEY, Configurator.DEFAULT_DATA_API_PATH)).thenReturn(dataUri);
52          
53          String baseUri = "http://www.example.com";
54          DiscoveryService ds = new DiscoveryService(mockConfig, new EntityOutputFormat( new JsonFormat(), new URI(baseUri), null ));
55          Response response = ds.getDiscoveryDocument();
56          
57          String json = new String((byte[]) response.getEntity());
58          
59          assertNotNull(json);
60          assertThat(json.length(), is(greaterThan(0)));
61          assertThat(json, is(not("\"\"")));
62          assertThat(json, is(not("null")));
63          
64          assertThat(json, containsString("\"management\" : \"" + baseUri + managementUri + "\""));
65          assertThat(json, containsString("\"data\" : \"" + baseUri + dataUri + "\""));
66     
67      }
68      
69      @Ignore
70      @Test
71      public void shouldReturnConfiguredUrlIfConfigIsAbsolute() throws Exception {
72          Configuration mockConfig = mock(Configuration.class);
73          String managementUri = "http://absolutedomain/management";
74          when(mockConfig.getString(Configurator.MANAGEMENT_PATH_PROPERTY_KEY, Configurator.DEFAULT_MANAGEMENT_API_PATH)).thenReturn(managementUri);
75          String dataUri = "http://absolutedomain/management";
76          when(mockConfig.getString(Configurator.DATA_API_PATH_PROPERTY_KEY, Configurator.DEFAULT_DATA_API_PATH)).thenReturn(dataUri);
77          
78          String baseUri = "http://www.example.com";
79          DiscoveryService ds = new DiscoveryService(mockConfig, new EntityOutputFormat( new JsonFormat(), new URI(baseUri), null ));
80          Response response = ds.getDiscoveryDocument();
81          
82          String json = new String((byte[]) response.getEntity());
83          
84          assertNotNull(json);
85          assertThat(json.length(), is(greaterThan(0)));
86          assertThat(json, is(not("\"\"")));
87          assertThat(json, is(not("null")));
88          
89          assertThat(json, containsString("\"management\" : \"" + managementUri + "\""));
90          assertThat(json, containsString("\"data\" : \"" + dataUri + "\""));
91     
92      }
93  }