|
| 1 | +/* |
| 2 | + Copyright 2017 Ericsson AB. |
| 3 | + For a full list of individual contributors, please see the commit history. |
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + Unless required by applicable law or agreed to in writing, software |
| 9 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + See the License for the specific language governing permissions and |
| 12 | + limitations under the License. |
| 13 | +*/ |
| 14 | +package com.ericsson.ei.erqueryservice.test; |
| 15 | + |
| 16 | +import static org.junit.Assert.assertEquals; |
| 17 | + |
| 18 | +import java.net.URI; |
| 19 | +import java.net.URISyntaxException; |
| 20 | + |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.runner.RunWith; |
| 24 | +import org.mockito.InjectMocks; |
| 25 | +import org.mockito.Mock; |
| 26 | +import org.mockito.Mockito; |
| 27 | +import org.mockito.MockitoAnnotations; |
| 28 | + |
| 29 | +import static org.mockito.BDDMockito.*; |
| 30 | +import org.mockito.invocation.Invocation; |
| 31 | +import org.mockito.invocation.InvocationOnMock; |
| 32 | +import org.mockito.stubbing.Answer; |
| 33 | +import org.springframework.beans.factory.annotation.Autowired; |
| 34 | +import org.springframework.boot.test.context.SpringBootTest; |
| 35 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 36 | +import org.springframework.http.HttpEntity; |
| 37 | +import org.springframework.http.HttpMethod; |
| 38 | +import org.springframework.http.HttpRequest; |
| 39 | +import org.springframework.http.HttpStatus; |
| 40 | +import org.springframework.http.ResponseEntity; |
| 41 | +import org.springframework.http.server.ServletServerHttpRequest; |
| 42 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 43 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 44 | +import org.springframework.util.LinkedMultiValueMap; |
| 45 | +import org.springframework.util.MultiValueMap; |
| 46 | +import org.springframework.web.client.RestOperations; |
| 47 | +import org.springframework.web.util.UriComponents; |
| 48 | +import org.springframework.web.util.UriComponentsBuilder; |
| 49 | + |
| 50 | +import com.ericsson.ei.controller.model.Subscription; |
| 51 | +import com.ericsson.ei.erqueryservice.ERQueryService; |
| 52 | +import com.fasterxml.jackson.databind.JsonNode; |
| 53 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 54 | + |
| 55 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 56 | +@SpringBootTest |
| 57 | +public class ERQueryServiceTest { |
| 58 | + |
| 59 | + @Autowired |
| 60 | + ERQueryService erQueryService; |
| 61 | + |
| 62 | + @Mock |
| 63 | + RestOperations rest; |
| 64 | + |
| 65 | + String eventId = "01"; |
| 66 | + int searchAction = 0; |
| 67 | + int limitParam = 85; |
| 68 | + int levels = 2; |
| 69 | + boolean isTree = true; |
| 70 | + |
| 71 | + @Before public void setUp() throws Exception { |
| 72 | + MockitoAnnotations.initMocks(this); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testErQueryUpstream() { |
| 77 | + erQueryService.setRest(rest); |
| 78 | + searchAction = ERQueryService.UPSTREAM; |
| 79 | + given(rest.exchange(Mockito.any(URI.class), Mockito.any(HttpMethod.class), Mockito.any(HttpEntity.class), Mockito.any(Class.class))).willAnswer(returnRestExchange(Mockito.any(URI.class), Mockito.any(HttpMethod.class), Mockito.any(HttpEntity.class), Mockito.any(Class.class))); |
| 80 | + ResponseEntity result = erQueryService.getEventStreamDataById(eventId, searchAction, limitParam, levels, isTree); |
| 81 | + } |
| 82 | + |
| 83 | + Answer<ResponseEntity> returnRestExchange(URI url, HttpMethod method, HttpEntity<?> requestEntity, |
| 84 | + Class responseType) { |
| 85 | + return new Answer<ResponseEntity>() { |
| 86 | + @Override |
| 87 | + public ResponseEntity answer(InvocationOnMock invocation) throws Throwable { |
| 88 | + URI arg0 = invocation.getArgumentAt(0, URI.class); |
| 89 | + String expectedUri = buildUri(); |
| 90 | + assertEquals(expectedUri, arg0.toString()); |
| 91 | + HttpEntity arg2 = invocation.getArgumentAt(2, HttpEntity.class); |
| 92 | + ObjectNode body = (ObjectNode) arg2.getBody(); |
| 93 | + assertBody(body); |
| 94 | + boolean firstStop = true; |
| 95 | + return new ResponseEntity(HttpStatus.OK); |
| 96 | + } |
| 97 | + }; |
| 98 | + } |
| 99 | + |
| 100 | + public String buildUri() { |
| 101 | + String uri = ""; |
| 102 | +// example uri |
| 103 | +// http://localhost:8080/search/01?limit=85&levels=2&tree=true |
| 104 | + uri += erQueryService.getUrl().trim() + eventId + "?limit=" + limitParam + "&levels=" + levels + "&tree=" + isTree; |
| 105 | + return uri; |
| 106 | + } |
| 107 | + |
| 108 | + public void assertBody(ObjectNode body) { |
| 109 | +// example body |
| 110 | +// {"ult":["ALL"]} |
| 111 | + boolean bodyNotNull = body != null; |
| 112 | + assertEquals(bodyNotNull, true); |
| 113 | + boolean searchActionIsRight = false; |
| 114 | + if (searchAction == ERQueryService.DOWNSTREAM) { |
| 115 | + searchActionIsRight = body.has("dlt"); |
| 116 | + } else if (searchAction == ERQueryService.UPSTREAM) { |
| 117 | + searchActionIsRight = body.has("ult"); |
| 118 | + } else if (searchAction == ERQueryService.DOWNANDUPSTREAM) { |
| 119 | + searchActionIsRight = body.has("dlt") && body.has("ult"); |
| 120 | + } |
| 121 | + assertEquals(searchActionIsRight, true); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void queryParamsTest() throws URISyntaxException { |
| 126 | + MultiValueMap<String, String> expectedQueryParams = new LinkedMultiValueMap(3); |
| 127 | + expectedQueryParams.add("limit", "10"); |
| 128 | + expectedQueryParams.add("levels", "5"); |
| 129 | + expectedQueryParams.add("tree", "true"); |
| 130 | + UriComponentsBuilder builder = UriComponentsBuilder.newInstance(); |
| 131 | + UriComponents result = builder.queryParams(expectedQueryParams).build(); |
| 132 | + assertEquals("limit=10&levels=5&tree=true", result.getQuery()); |
| 133 | + assertEquals(expectedQueryParams, result.getQueryParams()); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void uriParamAndHeaderTest() throws Exception { |
| 138 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 139 | + request.addHeader("Forwarded", "proto=https; host=127.0.0.1"); |
| 140 | + request.setScheme("http"); |
| 141 | + request.setServerName("localhost"); |
| 142 | + request.setRequestURI("/search/6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43"); |
| 143 | + |
| 144 | + HttpRequest httpRequest = new ServletServerHttpRequest(request); |
| 145 | + UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build(); |
| 146 | + |
| 147 | + assertEquals("https", result.getScheme()); |
| 148 | + assertEquals("127.0.0.1", result.getHost()); |
| 149 | + assertEquals("/search/6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43", result.getPath()); |
| 150 | + assertEquals("https://127.0.0.1/search/6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43", result.toUriString()); |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments