|
| 1 | +/* |
| 2 | + * Copyright © 2017 IBM Corp. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | + * except in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 11 | + * either express or implied. See the License for the specific language governing permissions |
| 12 | + * and limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.ibm.watson.developer_cloud.spring.boot.test; |
| 16 | + |
| 17 | +import com.ibm.watson.developer_cloud.language_translator.v2.LanguageTranslator; |
| 18 | +import com.ibm.watson.developer_cloud.service.WatsonService; |
| 19 | +import com.ibm.watson.developer_cloud.spring.boot.WatsonAutoConfiguration; |
| 20 | +import okhttp3.Credentials; |
| 21 | +import org.junit.ClassRule; |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.contrib.java.lang.system.EnvironmentVariables; |
| 24 | +import org.junit.runner.RunWith; |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.context.ApplicationContext; |
| 27 | +import org.springframework.test.context.ContextConfiguration; |
| 28 | +import org.springframework.test.context.TestPropertySource; |
| 29 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 30 | +import org.springframework.test.context.support.AnnotationConfigContextLoader; |
| 31 | + |
| 32 | +import java.lang.reflect.Field; |
| 33 | + |
| 34 | +import static org.junit.Assert.assertEquals; |
| 35 | +import static org.junit.Assert.assertNotNull; |
| 36 | + |
| 37 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 38 | +@ContextConfiguration(classes = {WatsonAutoConfiguration.class}, loader = AnnotationConfigContextLoader.class) |
| 39 | +@TestPropertySource(properties = {"watson.language_translator.enabled=true"}) |
| 40 | +public class WatsonAutoConfigTest { |
| 41 | + |
| 42 | + private static final String url = "http://watson.com/language_translator"; |
| 43 | + private static final String username = "sam"; |
| 44 | + private static final String password = "secret"; |
| 45 | + |
| 46 | + @ClassRule |
| 47 | + public static final EnvironmentVariables environmentVariables = new EnvironmentVariables(); |
| 48 | + |
| 49 | + static { |
| 50 | + String vcapServices = "{\"language_translator\":[{" |
| 51 | + + "\"credentials\": {" |
| 52 | + + "\"url\":\"" + url + "\"," |
| 53 | + + "\"username\":\"" + username + "\"," |
| 54 | + + "\"password\":\"" + password + "\"" |
| 55 | + + "}," |
| 56 | + + "\"plan\": \"free\"" |
| 57 | + + "}]}"; |
| 58 | + environmentVariables.set("VCAP_SERVICES", vcapServices); |
| 59 | + } |
| 60 | + |
| 61 | + @Autowired |
| 62 | + private ApplicationContext applicationContext; |
| 63 | + |
| 64 | + @Test |
| 65 | + public void watsonBeanConfigFromEnvironment() { |
| 66 | + LanguageTranslator languageTranslator = (LanguageTranslator) applicationContext.getBean("languageTranslator"); |
| 67 | + |
| 68 | + assertNotNull(languageTranslator); |
| 69 | + // URL is not set from VCAP_SERVICES |
| 70 | + //assertEquals(url, languageTranslator.getEndPoint()); |
| 71 | + |
| 72 | + // Verify the credentials -- which are stored in a private member variable |
| 73 | + try { |
| 74 | + Field apiKeyField = WatsonService.class.getDeclaredField("apiKey"); |
| 75 | + apiKeyField.setAccessible(true); |
| 76 | + assertEquals(Credentials.basic(username, password), apiKeyField.get(languageTranslator)); |
| 77 | + } catch (NoSuchFieldException | IllegalAccessException ex) { |
| 78 | + // This shouldn't happen |
| 79 | + assert false; |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments