Guest User

Untitled

a guest
Feb 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.17 KB | None | 0 0
  1. package com.imosadapter.srd.main;
  2.  
  3. import com.fasterxml.jackson.core.type.TypeReference;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import com.trafigura.titan.imosadapter.srd.constant.ApiRequestUtility;
  6. import com.trafigura.titan.imosadapter.srd.constant.SrdApiConstant;
  7. import com.trafigura.titan.imosadapter.srd.model.company.Company;
  8. import com.trafigura.titan.imosadapter.srd.model.legalentity.LegalEntity;
  9. import org.slf4j.Logger;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.beans.factory.annotation.Value;
  12.  
  13. import java.io.IOException;
  14. import java.util.List;
  15.  
  16. import static org.slf4j.LoggerFactory.getLogger;
  17.  
  18. public class ImosSrdSample {
  19.  
  20. @Autowired
  21. private ApiRequestUtility apiRequestUtility;
  22. @Value("${imos.srd.filepath}")
  23. private String imosFilepath;
  24.  
  25. private static final Logger LOG = getLogger(ImosSrdMapper.class);
  26.  
  27. ObjectMapper objectMapper = new ObjectMapper();
  28. String legalEntitiesOutput = null;
  29.  
  30. public void getSrdDetails() throws IOException {
  31. LOG.info("Getting Legal Entities For Company Roles");
  32. String companyRoleOutput = apiRequestUtility.srdApiCall(SrdApiConstant.COMPANIES_URL, SrdApiConstant.COMPANIES_INPUT);
  33. //LOG.info("Company Role Output " + companyRoleOutput);
  34. try {
  35. List<LegalEntity> legalEntities = objectMapper.reader().forType(new TypeReference<List<LegalEntity>>() {
  36. }).readValue(companyRoleOutput);
  37. for (LegalEntity legalEntity : legalEntities) {
  38. LOG.info("Getting Legal Entities Information");
  39. legalEntitiesOutput = apiRequestUtility.srdApiCall(SrdApiConstant.LEGALENTITIES_URL,
  40. apiRequestUtility.jsonInput(legalEntity.getIdentifier(), SrdApiConstant.GUID));
  41. //LOG.info("Legal Entities Output " + legalEntitiesOutput);
  42.  
  43. Company company = objectMapper.readValue(legalEntitiesOutput, Company.class);
  44. }
  45. } catch (IOException e) {
  46. LOG.error("No File Path found. So File not generated", e);
  47. }
  48. }
  49. }
  50.  
  51. package com.imosadapter.srd.constant;
  52. import org.springframework.stereotype.Component;
  53.  
  54. @Component
  55. public class SrdApiConstant {
  56.  
  57. private SrdApiConstant() {
  58. }
  59.  
  60. public static final String BASE_URL = "http://localhost:9100/referencedatanew/";
  61. public static final String COMPANIES_URL = "LegalEntitiesService/GetCompanies";
  62. public static final String LEGALENTITIES_URL = "LegalEntitiesService/GetCompanyByGUID";
  63.  
  64.  
  65. public static final String COMPANIES_INPUT = "{"CompanyCode":"AGSHCODE"}";
  66.  
  67. public static final String GUID = "Guid";
  68. public static final String LE_GUID = "LegalEntityGUID";
  69. }
  70.  
  71. package com.imosadapter.srd.constant;
  72.  
  73. import java.io.BufferedReader;
  74. import java.io.IOException;
  75. import java.io.InputStreamReader;
  76. import java.io.OutputStream;
  77. import java.net.HttpURLConnection;
  78. import java.net.URL;
  79.  
  80. import com.fasterxml.jackson.core.JsonProcessingException;
  81. import com.fasterxml.jackson.databind.ObjectMapper;
  82. import com.fasterxml.jackson.databind.node.ObjectNode;
  83. import org.slf4j.Logger;
  84. import org.slf4j.LoggerFactory;
  85. import org.springframework.stereotype.Component;
  86.  
  87. @Component
  88. public class ApiRequestUtility {
  89.  
  90. private static final Logger LOG = LoggerFactory.getLogger(ApiRequestUtility.class);
  91.  
  92. public String srdApiCall(String url, String jsonInputString) throws IOException {
  93. String user = SrdApiConstant.USER;
  94. String companyOutput = null;
  95. StringBuffer result = new StringBuffer();
  96. String baseUrl = SrdApiConstant.BASE_URL + url;
  97.  
  98. URL companyUrl = new URL(SrdApiConstant.BASE_URL + url);
  99. HttpURLConnection conn = (HttpURLConnection) companyUrl.openConnection();
  100. conn.setDoOutput(true);
  101. conn.setRequestMethod("POST");
  102. conn.setRequestProperty("Content-Type", "application/json");
  103. conn.setRequestProperty("User", "user");
  104. OutputStream os = conn.getOutputStream();
  105. os.write(jsonInputString.getBytes());
  106. os.flush();
  107. if (conn.getResponseCode() != 200) {
  108. throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
  109. }
  110. BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
  111. LOG.info("Output from Server .... n");
  112. while ((companyOutput = br.readLine()) != null) {
  113. result.append(companyOutput);
  114.  
  115. }
  116. br.close();
  117. conn.disconnect();
  118. LOG.info("url : " + baseUrl);
  119. return result.toString();
  120. }
  121.  
  122. public String jsonInput(String guid, String guidType) throws JsonProcessingException {
  123. String jsonString;
  124. ObjectMapper mapper = new ObjectMapper();
  125. ObjectNode srdInputJson = mapper.createObjectNode();
  126. if (SrdApiConstant.GUID.equalsIgnoreCase(guidType)) {
  127. srdInputJson.put("Guid", guid);
  128. } else if (SrdApiConstant.LE_GUID.equalsIgnoreCase(guidType)) {
  129. srdInputJson.put("LegalEntityGUID", guid);
  130. }
  131. jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(srdInputJson);
  132. LOG.info("JSON String " + jsonString);
  133. return jsonString;
  134. }
  135. }
  136.  
  137. package com.imosadapter.srd.main;
  138.  
  139. import com.fasterxml.jackson.databind.ObjectMapper;
  140. import com.trafigura.titan.imosadapter.srd.constant.ApiRequestUtility;
  141. import org.junit.Before;
  142. import org.junit.Test;
  143. import org.mockito.InjectMocks;
  144. import org.mockito.Mock;
  145. import org.springframework.test.util.ReflectionTestUtils;
  146.  
  147. import javax.xml.bind.JAXBContext;
  148.  
  149. import static org.junit.Assert.*;
  150. import static org.mockito.MockitoAnnotations.initMocks;
  151.  
  152. public class ImosSrdSampleTest {
  153.  
  154.  
  155. @InjectMocks
  156. private ImosSrdSample imosSrdSample;
  157.  
  158. @Mock
  159. private JAXBContext jaxbContext;
  160.  
  161. @Mock
  162. private ObjectMapper objectMapper;
  163.  
  164. @Mock
  165. private ApiRequestUtility apiRequestUtility;
  166.  
  167. @Before
  168. public void setUp() throws Exception {
  169. initMocks(this);
  170. ReflectionTestUtils.setField(imosSrdSample, "imosFilepath", "src/test/resources/");
  171. }
  172.  
  173. @Test
  174. public void getSrdDetails() throws Exception {
  175. imosSrdSample.getSrdDetails();
  176. }
  177.  
  178. }
  179.  
  180. java.lang.NullPointerException
  181. at com.trafigura.titan.imosadapter.srd.main.ImosSrdSample.getSrdDetails(ImosSrdSample.java:35)
  182. at com.trafigura.titan.imosadapter.srd.main.ImosSrdSampleTest.getSrdDetails(ImosSrdSampleTest.java:42)
  183. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  184. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  185. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  186. at java.lang.reflect.Method.invoke(Method.java:497)
  187. at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
  188. at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  189. at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
  190. at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  191. at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
  192. at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
  193. at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
  194. at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
  195. at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
  196. at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
  197. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
  198. at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
  199. at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
  200. at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
  201. at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
  202. at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
  203. at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
  204. at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
  205. at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Add Comment
Please, Sign In to add comment