From 45e403d89794720689be5c8ee71f4c7174c022c3 Mon Sep 17 00:00:00 2001 From: Prudence Au Date: Thu, 30 Aug 2018 13:24:55 -0400 Subject: Add support for using the "old" headers Change-Id: Ie4b302ca4325ee3b04fee0319efbc1a770d6c1ab Signed-off-by: Prudence Au Issue-ID: LOG-401 --- .../networkdiscovery/service/rs/RestService.java | 1 + .../service/rs/RestServiceImpl.java | 22 +++- .../NetworkDiscoveryContextBuilderTest.java | 124 ++++++++++++++------- 3 files changed, 99 insertions(+), 48 deletions(-) (limited to 'src') diff --git a/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/service/rs/RestService.java b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/service/rs/RestService.java index 8825bc6..67c65d9 100644 --- a/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/service/rs/RestService.java +++ b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/service/rs/RestService.java @@ -51,6 +51,7 @@ public interface RestService { @ApiResponse(code = 500, message = "Unexpected Runtime error") }) public Response getContext(@Context HttpServletRequest req, @HeaderParam("Authorization") String authorization, @HeaderParam("X-ONAP-PartnerName") String xpartnerName, @HeaderParam("X-ONAP-RequestID") String xRequestId, + @HeaderParam("X-FromAppId") String xFromAppId, @HeaderParam("X-TransactionId") String xTransactionId, @QueryParam("serviceInstanceId") String serviceInstanceId, @QueryParam("modelVersionId") String modelVersionId, @QueryParam("modelInvariantId") String modelInvariantId) throws DiscoveryException; diff --git a/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/service/rs/RestServiceImpl.java b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/service/rs/RestServiceImpl.java index 87ef440..7046c2d 100644 --- a/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/service/rs/RestServiceImpl.java +++ b/src/main/java/org/onap/pomba/contextbuilder/networkdiscovery/service/rs/RestServiceImpl.java @@ -46,15 +46,27 @@ public class RestServiceImpl implements RestService { private SpringService service; @Override - public Response getContext(HttpServletRequest req, String authorization, String partnerName, String requestId, + public Response getContext(HttpServletRequest req, String authorization, String partnerName, String requestId, String fromAppId, String transactionId, String serviceInstanceId, String modelVersionId, String modelInvariantId) throws DiscoveryException { // Do some validation on Http headers and URL parameters - if (requestId == null || requestId.isEmpty()) { - requestId = UUID.randomUUID().toString(); - log.debug(HEADER_REQUEST_ID + " is missing; using newly generated value: " + requestId); - } + //The request ID in the header is not yet standardized to X-ONAP-RequestID. We would still support X-TransactionId until further notice. + if(requestId == null || requestId.isEmpty()) { + if(transactionId != null) { + requestId = transactionId; + } else { + requestId = UUID.randomUUID().toString(); + log.debug(HEADER_REQUEST_ID + " is missing; using newly generated value: " + requestId); + } + } + + //The partner name in the header is not yet standardized to X-PartnerName. We would still support X-FromAppId until further notice. + if(partnerName == null || partnerName.isEmpty()) { + if(fromAppId != null) { + partnerName = fromAppId; + } + } try { ModelContext sdContext = service.getContext(req, partnerName, authorization, requestId, serviceInstanceId, diff --git a/src/test/java/org/onap/pomba/contextbuilder/networkdiscovery/unittest/service/NetworkDiscoveryContextBuilderTest.java b/src/test/java/org/onap/pomba/contextbuilder/networkdiscovery/unittest/service/NetworkDiscoveryContextBuilderTest.java index 77e37ca..5485535 100644 --- a/src/test/java/org/onap/pomba/contextbuilder/networkdiscovery/unittest/service/NetworkDiscoveryContextBuilderTest.java +++ b/src/test/java/org/onap/pomba/contextbuilder/networkdiscovery/unittest/service/NetworkDiscoveryContextBuilderTest.java @@ -1,19 +1,15 @@ /* - * ============LICENSE_START=================================================== - * Copyright (c) 2018 Amdocs - * ============================================================================ - * 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 + * ============LICENSE_START=================================================== Copyright (c) 2018 + * Amdocs ============================================================================ 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 + * 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. - * ============LICENSE_END===================================================== + * 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. ============LICENSE_END===================================================== */ package org.onap.pomba.contextbuilder.networkdiscovery.unittest.service; @@ -25,10 +21,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; -import com.github.jknack.handlebars.internal.Files; -import com.github.tomakehurst.wiremock.client.WireMock; -import com.github.tomakehurst.wiremock.junit.WireMockRule; -import com.github.tomakehurst.wiremock.matching.UrlPattern; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -47,11 +39,11 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; +import org.onap.pomba.common.datatypes.DataQuality; import org.onap.pomba.contextbuilder.networkdiscovery.model.NetworkDiscoveryRspInfo; import org.onap.pomba.contextbuilder.networkdiscovery.service.SpringServiceImpl; import org.onap.pomba.contextbuilder.networkdiscovery.service.rs.RestService; import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.Attribute; -import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.DataQuality; import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.NetworkDiscoveryNotification; import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.Resource; import org.springframework.beans.factory.annotation.Autowired; @@ -63,18 +55,23 @@ import org.springframework.core.env.Environment; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; +import com.github.jknack.handlebars.internal.Files; +import com.github.tomakehurst.wiremock.client.WireMock; +import com.github.tomakehurst.wiremock.junit.WireMockRule; +import com.github.tomakehurst.wiremock.matching.UrlPattern; + @RunWith(SpringJUnit4ClassRunner.class) -@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class }) +@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) @WebAppConfiguration @SpringBootTest -@TestPropertySource(properties = { "serviceDecomposition.host=localhost", "serviceDecomposition.port=3333", +@TestPropertySource(properties = {"serviceDecomposition.host=localhost", "serviceDecomposition.port=3333", "networkDiscoveryMicroService.host=localhost", "networkDiscoveryMicroService.port=9808", - "networkDiscoveryMicroService.responseTimeOutInMilliseconds=1000" }) + "networkDiscoveryMicroService.responseTimeOutInMilliseconds=1000"}) public class NetworkDiscoveryContextBuilderTest { - private String authorization = "Basic " - + Base64.getEncoder().encodeToString(("admin" + ":" + "admin").getBytes(StandardCharsets.UTF_8)); + private String authorization = + "Basic " + Base64.getEncoder().encodeToString(("admin" + ":" + "admin").getBytes(StandardCharsets.UTF_8)); private String partnerName = "POMBA"; private String transactionId = UUID.randomUUID().toString(); private String serviceInstanceId = "c6456519-6acf-4adb-997c-3c363dd4caaf"; @@ -97,35 +94,33 @@ public class NetworkDiscoveryContextBuilderTest { public WireMockRule networkDiscoveryMicroServiceRule = new WireMockRule(wireMockConfig().port(9808)); @Before - public void setUp() throws Exception { - } + public void setUp() throws Exception {} @After - public void tearDown() throws Exception { - } + public void tearDown() throws Exception {} @Test public void testVerifyNoAuthoriztion() throws Exception { - Response response = this.restService.getContext(httpServletRequest, null, partnerName, transactionId, - serviceInstanceId, null, null); + Response response = this.restService.getContext(httpServletRequest, null, partnerName, transactionId, null, + null, serviceInstanceId, null, null); assertTrue(response.getEntity().toString().contains("Missing Authorization: ")); assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus()); } @Test public void testVerifyBadAuthoriztion() throws Exception { - String authorization = "Basic " - + Base64.getEncoder().encodeToString(("Test" + ":" + "Fake").getBytes(StandardCharsets.UTF_8)); + String authorization = + "Basic " + Base64.getEncoder().encodeToString(("Test" + ":" + "Fake").getBytes(StandardCharsets.UTF_8)); Response response = this.restService.getContext(httpServletRequest, authorization, partnerName, transactionId, - serviceInstanceId, null, null); + null, null, serviceInstanceId, null, null); assertEquals("Authorization Failed", response.getEntity().toString()); assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus()); } @Test public void testVerifyPartnerName() throws Exception { - Response response = this.restService.getContext(httpServletRequest, authorization, null, transactionId, - serviceInstanceId, null, null); + Response response = this.restService.getContext(httpServletRequest, authorization, null, transactionId, null, + null, serviceInstanceId, null, null); assertTrue(response.getEntity().toString().contains("X-ONAP-PartnerName")); assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); } @@ -133,7 +128,7 @@ public class NetworkDiscoveryContextBuilderTest { @Test public void testVerifyServiceInstanceId() throws Exception { Response response = this.restService.getContext(httpServletRequest, authorization, partnerName, transactionId, - null, null, null); + null, null, null, null, null); assertTrue(response.getEntity().toString().contains("serviceInstanceId")); assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); } @@ -149,7 +144,7 @@ public class NetworkDiscoveryContextBuilderTest { this.serviceDecompositionRule.stubFor(get(urlStr).willReturn(okJson(sdResonse))); addResponse_any("junit/networkDiscoveryResponse-1.json", networkDiscoveryMicroServiceRule); Response response = this.restService.getContext(httpServletRequest, authorization, partnerName, transactionId, - serviceInstanceId, null, null); + null, null, serviceInstanceId, null, null); assertEquals(Status.OK.getStatusCode(), response.getStatus()); } @@ -161,7 +156,7 @@ public class NetworkDiscoveryContextBuilderTest { addResponse_any("junit/networkDiscoveryResponse-1.json", networkDiscoveryMicroServiceRule); Response response = this.restService.getContext(httpServletRequest, authorization, partnerName, transactionId, - serviceInstanceId, null, null); + null, null, serviceInstanceId, null, null); assertEquals(Status.OK.getStatusCode(), response.getStatus()); } @@ -171,10 +166,10 @@ public class NetworkDiscoveryContextBuilderTest { String networkDiscoveryCallBackUrl = "/network-discovery/service/networkDiscoveryNotification"; addResponse(networkDiscoveryCallBackUrl, "junit/networkDiscovery-1.json", networkDiscoveryMicroServiceRule); - String badAuthorization = "Basic " - + Base64.getEncoder().encodeToString(("Test" + ":" + "Fake").getBytes(StandardCharsets.UTF_8)); - Response response = this.restService.networkDiscoveryNotification(networkDiscoveryNotification, - badAuthorization); + String badAuthorization = + "Basic " + Base64.getEncoder().encodeToString(("Test" + ":" + "Fake").getBytes(StandardCharsets.UTF_8)); + Response response = + this.restService.networkDiscoveryNotification(networkDiscoveryNotification, badAuthorization); assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus()); } @@ -192,6 +187,50 @@ public class NetworkDiscoveryContextBuilderTest { assertEquals(rsp.getNetworkDiscoveryNotificationList().size(), 1); } + @Test + public void testVerifyNoPartnerNameWithFromAppId() throws Exception { + String serviceDecompUrl = "/service-decomposition/service/context?serviceInstanceId=" + serviceInstanceId; + addResponse(serviceDecompUrl, "junit/serviceDecomposition-1.json", serviceDecompositionRule); + addResponse_any("junit/networkDiscoveryResponse-1.json", networkDiscoveryMicroServiceRule); + + Response response = this.restService.getContext(httpServletRequest, authorization, null, transactionId, + partnerName, null, serviceInstanceId, null, null); + assertEquals(Status.OK.getStatusCode(), response.getStatus()); + } + + @Test + public void testVerifyNoRequestIdNoTransactionId() throws Exception { + String serviceDecompUrl = "/service-decomposition/service/context?serviceInstanceId=" + serviceInstanceId; + addResponse(serviceDecompUrl, "junit/serviceDecomposition-1.json", serviceDecompositionRule); + addResponse_any("junit/networkDiscoveryResponse-1.json", networkDiscoveryMicroServiceRule); + + Response response = this.restService.getContext(httpServletRequest, authorization, partnerName, null, null, + null, serviceInstanceId, null, null); + assertEquals(Status.OK.getStatusCode(), response.getStatus()); + } + + @Test + public void testVerifyNoPartnerNameNoFromAppId() throws Exception { + String serviceDecompUrl = "/service-decomposition/service/context?serviceInstanceId=" + serviceInstanceId; + addResponse(serviceDecompUrl, "junit/serviceDecomposition-1.json", serviceDecompositionRule); + addResponse_any("junit/networkDiscoveryResponse-1.json", networkDiscoveryMicroServiceRule); + + Response response = this.restService.getContext(httpServletRequest, authorization, null, transactionId, null, + null, serviceInstanceId, null, null); + assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + } + + @Test + public void testVerifyNoRequestIdWithTransactionId() throws Exception { + String serviceDecompUrl = "/service-decomposition/service/context?serviceInstanceId=" + serviceInstanceId; + addResponse(serviceDecompUrl, "junit/serviceDecomposition-1.json", serviceDecompositionRule); + addResponse_any("junit/networkDiscoveryResponse-1.json", networkDiscoveryMicroServiceRule); + + Response response = this.restService.getContext(httpServletRequest, authorization, partnerName, null, null, + transactionId, serviceInstanceId, null, null); + assertEquals(Status.OK.getStatusCode(), response.getStatus()); + } + private void addResponse(String path, String classpathResource, WireMockRule thisMock) throws IOException { String payload = readFully(ClassLoader.getSystemResourceAsStream(classpathResource)); thisMock.stubFor(get(path).willReturn(okJson(payload))); @@ -223,8 +262,7 @@ public class NetworkDiscoveryContextBuilderTest { DataQuality dataQuality = new DataQuality(); dataQuality.setStatus(DataQuality.Status.ok); myResource.setDataQuality(dataQuality); - List attributeList = new ArrayList<>(); - ; + List attributeList = new ArrayList<>();; Attribute attribute = new Attribute(); attribute.setName("vserver-id"); attribute.setValue("25fb07ab-0478-465e-a021-6384ac299671"); -- cgit 1.2.3-korg