Refactor Swagger unit tests
[netconf.git] / restconf / sal-rest-docgen / src / test / java / org / opendaylight / controller / sal / rest / doc / impl / DocGenTestHelper.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.sal.rest.doc.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
15
16 import com.fasterxml.jackson.databind.JsonNode;
17 import java.net.URI;
18 import javax.ws.rs.core.UriBuilder;
19 import javax.ws.rs.core.UriInfo;
20 import org.mockito.ArgumentCaptor;
21 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23
24 final class DocGenTestHelper {
25
26     private DocGenTestHelper() {
27         // hidden on purpose
28     }
29
30     static DOMSchemaService createMockSchemaService(final EffectiveModelContext mockContext) {
31         final DOMSchemaService mockSchemaService = mock(DOMSchemaService.class);
32         when(mockSchemaService.getGlobalContext()).thenReturn(mockContext);
33         return mockSchemaService;
34     }
35
36     static UriInfo createMockUriInfo(final String urlPrefix) throws Exception {
37         final URI uri = new URI(urlPrefix);
38         final UriBuilder mockBuilder = mock(UriBuilder.class);
39
40         final ArgumentCaptor<String> subStringCapture = ArgumentCaptor.forClass(String.class);
41         when(mockBuilder.path(subStringCapture.capture())).thenReturn(mockBuilder);
42         when(mockBuilder.build()).then(invocation -> URI.create(uri + "/" + subStringCapture.getValue()));
43
44         final UriInfo info = mock(UriInfo.class);
45         when(info.getRequestUriBuilder()).thenReturn(mockBuilder);
46         when(mockBuilder.replaceQuery(any())).thenReturn(mockBuilder);
47         when(info.getBaseUri()).thenReturn(uri);
48
49         return info;
50     }
51
52     /**
53      * Checks whether object {@code mainObject} contains in properties/items key $ref with concrete value.
54      */
55     static void containsReferences(final JsonNode mainObject, final String childObject, final String expectedRef) {
56         final JsonNode properties = mainObject.get("properties");
57         assertNotNull(properties);
58
59         final JsonNode childNode = properties.get(childObject);
60         assertNotNull(childNode);
61
62         //list case
63         JsonNode refWrapper = childNode.get("items");
64         if (refWrapper == null) {
65             //container case
66             refWrapper = childNode;
67         }
68         assertEquals(expectedRef, refWrapper.get("$ref").asText());
69     }
70 }