Java tutorial
/* * Copyright 2013-2104 the original author or authors. * * 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 jittr.rest; import jittr.Jittr; import jittr.security.WebSecurityConfig; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import static org.junit.Assert.*; import java.util.Map; /** * Web security tests. * * @author Grehov * */ @ActiveProfiles("dev") @RunWith(SpringRunner.class) @SpringBootTest(classes = { Jittr.class, JittleFacadeRestSecurityIntTest.ExtraConfig.class, WebSecurityConfig.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class JittleFacadeRestSecurityIntTest { @Autowired TestRestTemplate testRestTemplate; @Autowired MappingJackson2HttpMessageConverter converter; @Test public void passwordGrant() { MultiValueMap<String, String> request = new LinkedMultiValueMap<String, String>(); request.set("username", "habuma"); request.set("password", "password"); request.set("grant_type", "password"); Map<String, Object> token = testRestTemplate.postForObject("/oauth/token", request, Map.class); assertNotNull("Wrong response: " + token, token); } @TestConfiguration public static class ExtraConfig { @Bean RestTemplateBuilder restTemplateBuilder() { return new RestTemplateBuilder().basicAuthorization("jittr", "jittr"); } } }