From 1c1f0dc110177a190c67e3a8b924542ce4d75ff8 Mon Sep 17 00:00:00 2001 From: Fiete Ostkamp Date: Tue, 8 Aug 2023 07:11:17 +0000 Subject: Make URIParser code more readable - Part 2 - declare variables closer to where they are used - move more validation into the validatePath method - increment the global index variable i in fewer places Issue-ID: AAI-3656 Signed-off-by: Fiete Ostkamp Change-Id: I64380b84370a362113bf64c4edff3d389ffb290e --- .../java/org/onap/aai/parsers/uri/URIParser.java | 89 ++++++++++------------ .../aai/prevalidation/ValidationServiceTest.java | 2 +- 2 files changed, 41 insertions(+), 50 deletions(-) (limited to 'aai-core/src') diff --git a/aai-core/src/main/java/org/onap/aai/parsers/uri/URIParser.java b/aai-core/src/main/java/org/onap/aai/parsers/uri/URIParser.java index 6989a725..409631f5 100644 --- a/aai-core/src/main/java/org/onap/aai/parsers/uri/URIParser.java +++ b/aai-core/src/main/java/org/onap/aai/parsers/uri/URIParser.java @@ -105,12 +105,10 @@ public class URIParser { this.loader = this.originalLoader; } String[] parts = uri.getRawPath().split("/"); - Introspector validNamespaces = loader.introspectorFromName("inventory"); Introspector previousObj = null; EdgeType type = EdgeType.TREE; for (int i = 0; i < parts.length;) { String part = parts[i]; - Introspector introspector = null; if (part.equals(RestTokens.COUSIN.toString())) { boolean isPathInvalid = i == parts.length - 1; if (isPathInvalid) { @@ -118,54 +116,41 @@ public class URIParser { uri + " not a valid path. Cannot end in " + RestTokens.COUSIN); } boolean isFinalContainer = i == parts.length - 2; - introspector = parseCousin(p, parts[i + 1], previousObj, isFinalContainer); - previousObj = introspector; + previousObj = parseCousin(p, parts[i + 1], previousObj, isFinalContainer); type = EdgeType.ALL; i += 2; continue; } - introspector = loader.introspectorFromName(part); - if (introspector != null) { - - validatePath(isRelative, validNamespaces, previousObj, part, introspector); - - Set keys = introspector.getKeys(); - if (keys.size() > 0) { - MultivaluedMap uriKeys = new MultivaluedHashMap<>(); - i++; - boolean isLastPart = i == parts.length; - if (isLastPart && queryParams != null) { - uriKeys = queryParams; - } else { - for (String key : keys) { - part = UriUtils.decode(parts[i], "UTF-8"); - introspector.setValue(key, part); - // skip this for further processing - i++; - } - } - - p.processObject(introspector, type, uriKeys); - type = EdgeType.TREE; - } else if (introspector.isContainer()) { - boolean isFinalContainer = i == parts.length - 1; - MultivaluedMap uriKeys = - isFinalContainer && queryParams != null - ? queryParams - : new MultivaluedHashMap<>(); - p.processContainer(introspector, type, uriKeys, isFinalContainer); - i++; + Introspector introspector = loader.introspectorFromName(part); + validatePath(isRelative, previousObj, part, introspector); + Set keys = introspector.getKeys(); + if (keys.size() > 0) { + MultivaluedMap uriKeys = new MultivaluedHashMap<>(); + boolean isLastPart = i+1 == parts.length; + if (isLastPart && queryParams != null) { + uriKeys = queryParams; } else { - p.processNamespace(introspector); - // namespace case - i++; + for (String key : keys) { + part = UriUtils.decode(parts[i+1], "UTF-8"); + introspector.setValue(key, part); + // skip this for further processing + i++; + } } - previousObj = introspector; + + p.processObject(introspector, type, uriKeys); + type = EdgeType.TREE; + } else if (introspector.isContainer()) { + boolean isFinalContainer = i == parts.length - 1; + MultivaluedMap uriKeys = isFinalContainer && queryParams != null + ? queryParams + : new MultivaluedHashMap<>(); + p.processContainer(introspector, type, uriKeys, isFinalContainer); } else { - // invalid item found should log - // original said bad path - throw new AAIException(aaiExceptionCode, "invalid item found in path: " + part); + p.processNamespace(introspector); } + i++; + previousObj = introspector; } } catch (AAIException e) { throw e; @@ -174,8 +159,11 @@ public class URIParser { } } - private void validatePath(boolean isRelative, Introspector validNamespaces, Introspector previousObj, + private void validatePath(boolean isRelative, Introspector previousObj, String part, Introspector introspector) throws AAIException, DoesNotStartWithValidNamespaceException { + if (introspector == null) { + throw new AAIException(aaiExceptionCode, "invalid item found in path: " + part); + } // previous has current as property boolean isPathInvalid = previousObj != null && !previousObj.hasChild(introspector) && !previousObj.getDbName().equals("nodes"); @@ -183,9 +171,10 @@ public class URIParser { throw new AAIException(aaiExceptionCode, uri + " not a valid path. " + part + " not valid"); } if (previousObj == null) { - String abstractType = introspector.getMetadata(ObjectMetadata.ABSTRACT); - // first time through, make sure it starts from a namespace + // first time through, make sure it starts from a valid namespace // ignore abstract types + String abstractType = introspector.getMetadata(ObjectMetadata.ABSTRACT); + Introspector validNamespaces = loader.introspectorFromName("inventory"); if (!isRelative && !"true".equals(abstractType) && !validNamespaces.hasChild(introspector)) { throw new DoesNotStartWithValidNamespaceException( uri + " not a valid path. It does not start from a valid namespace"); @@ -193,7 +182,8 @@ public class URIParser { } } - private Introspector parseCousin(Parsable p, String name, Introspector previousObj, boolean isFinalContainer) throws AAIException, AAIUnknownObjectException { + private Introspector parseCousin(Parsable p, String name, Introspector previousObj, boolean isFinalContainer) + throws AAIException, AAIUnknownObjectException { Introspector introspector; if (null == previousObj) { throw new AAIException(aaiExceptionCode); @@ -205,10 +195,11 @@ public class URIParser { if (introspector.isContainer()) { MultivaluedMap uriKeys = isFinalContainer && queryParams != null - ? queryParams - : new MultivaluedHashMap<>(); + ? queryParams + : new MultivaluedHashMap<>(); /* - * Related-to could be COUSIN OR TREE and in some cases BOTH. So Let EdgeRuleBuilder use all the + * Related-to could be COUSIN OR TREE and in some cases BOTH. So Let + * EdgeRuleBuilder use all the * edgeTypes */ p.processContainer(introspector, EdgeType.ALL, uriKeys, isFinalContainer); diff --git a/aai-core/src/test/java/org/onap/aai/prevalidation/ValidationServiceTest.java b/aai-core/src/test/java/org/onap/aai/prevalidation/ValidationServiceTest.java index d017408f..7f6e5610 100644 --- a/aai-core/src/test/java/org/onap/aai/prevalidation/ValidationServiceTest.java +++ b/aai-core/src/test/java/org/onap/aai/prevalidation/ValidationServiceTest.java @@ -115,7 +115,7 @@ public class ValidationServiceTest { String validationResponse = PayloadUtil.getResourcePayload("prevalidation/success-response-with-empty-violations.json"); - ResponseEntity responseEntity = Mockito.mock(ResponseEntity.class, Mockito.RETURNS_DEEP_STUBS); + ResponseEntity responseEntity = Mockito.mock(ResponseEntity.class, Mockito.RETURNS_DEEP_STUBS); Mockito.when(restClient.execute(eq(ValidationService.VALIDATION_ENDPOINT), eq(HttpMethod.POST), any(), eq(pserverRequest))).thenReturn(responseEntity); -- cgit 1.2.3-korg