Java tutorial
/** Copyright 2014 OpenSearchServer, Inc. * http://www.opensearchserver.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. **/ package com.opensearchserver.hadse.index; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; import java.util.Arrays; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class IndexTest { @Test public void t01_deleteIndex() { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); RestTemplate template = new RestTemplate(); ResponseEntity<String> entity = template.exchange("http://localhost:8080/my_index", HttpMethod.DELETE, null, String.class); assertNotNull(entity); HttpStatus res = entity.getStatusCode(); assertNotNull(res); switch (res) { case OK: assertEquals(HttpStatus.OK, res); break; case NOT_FOUND: assertEquals(HttpStatus.NOT_FOUND, res); break; default: fail("Unexpected result: " + res); break; } } @Test public void t02_createDefaultIndex() { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); RestTemplate template = new RestTemplate(); ResponseEntity<String> entity = template.exchange("http://localhost:8080/my_index", HttpMethod.PUT, null, String.class); assertEquals(HttpStatus.CREATED, entity.getStatusCode()); } @Test public void t03_exists() { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); RestTemplate template = new RestTemplate(); ResponseEntity<String> entity = template.exchange("http://localhost:8080/my_index", HttpMethod.GET, null, String.class); assertEquals(HttpStatus.FOUND, entity.getStatusCode()); } }