Add pagination for mounted resources of apidocs
[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.mockito.ArgumentMatchers.any;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.when;
13
14 import com.fasterxml.jackson.databind.ObjectMapper;
15 import com.fasterxml.jackson.databind.SerializationFeature;
16 import java.io.File;
17 import java.io.FileNotFoundException;
18 import java.net.URI;
19 import java.net.URISyntaxException;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Optional;
25 import java.util.Set;
26 import javax.ws.rs.core.UriBuilder;
27 import javax.ws.rs.core.UriInfo;
28 import org.mockito.ArgumentCaptor;
29 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
33
34 public class DocGenTestHelper {
35
36     private Set<Module> modules;
37     private ObjectMapper mapper;
38     private SchemaContext schemaContext;
39
40     public Set<Module> loadModules(final String resourceDirectory) throws URISyntaxException, FileNotFoundException {
41
42         final URI resourceDirUri = getClass().getResource(resourceDirectory).toURI();
43         final File testDir = new File(resourceDirUri);
44         final String[] fileList = testDir.list();
45         if (fileList == null) {
46             throw new FileNotFoundException(resourceDirectory.toString());
47         }
48         final List<File> files = new ArrayList<>();
49         for (final String fileName : fileList) {
50             files.add(new File(testDir, fileName));
51         }
52
53         this.schemaContext = YangParserTestUtils.parseYangFiles(files);
54         return this.schemaContext.getModules();
55     }
56
57     public Collection<Module> getModules() {
58         return this.modules;
59     }
60
61     public void setUp() throws Exception {
62         this.modules = loadModules("/yang");
63         this.mapper = new ObjectMapper();
64         this.mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
65     }
66
67     public SchemaContext getSchemaContext() {
68         return this.schemaContext;
69     }
70
71     public DOMSchemaService createMockSchemaService(SchemaContext mockContext) {
72         if (mockContext == null) {
73             mockContext = createMockSchemaContext();
74         }
75
76         final DOMSchemaService mockSchemaService = mock(DOMSchemaService.class);
77         when(mockSchemaService.getGlobalContext()).thenReturn(mockContext);
78         return mockSchemaService;
79     }
80
81     public SchemaContext createMockSchemaContext() {
82         final SchemaContext mockContext = mock(SchemaContext.class);
83         when(mockContext.getModules()).thenReturn(this.modules);
84
85         final ArgumentCaptor<String> moduleCapture = ArgumentCaptor.forClass(String.class);
86         final ArgumentCaptor<Optional> dateCapture = ArgumentCaptor.forClass(Optional.class);
87         final ArgumentCaptor<URI> namespaceCapture = ArgumentCaptor.forClass(URI.class);
88         when(mockContext.findModule(moduleCapture.capture(), dateCapture.capture())).then(
89             invocation -> {
90                 final String module = moduleCapture.getValue();
91                 final Optional<?> date = dateCapture.getValue();
92                 for (final Module m : Collections.unmodifiableSet(DocGenTestHelper.this.modules)) {
93                     if (m.getName().equals(module) && m.getRevision().equals(date)) {
94                         return Optional.of(m);
95                     }
96                 }
97                 return Optional.empty();
98             });
99         when(mockContext.findModule(namespaceCapture.capture(), dateCapture.capture())).then(
100             invocation -> {
101                 final URI namespace = namespaceCapture.getValue();
102                 final Optional<?> date = dateCapture.getValue();
103                 for (final Module m : Collections.unmodifiableSet(DocGenTestHelper.this.modules)) {
104                     if (m.getNamespace().equals(namespace) && m.getRevision().equals(date)) {
105                         return Optional.of(m);
106                     }
107                 }
108                 return Optional.empty();
109             });
110         return mockContext;
111     }
112
113     public UriInfo createMockUriInfo(final String urlPrefix) throws URISyntaxException {
114         final URI uri = new URI(urlPrefix);
115
116         final UriBuilder mockBuilder = mock(UriBuilder.class);
117
118         final ArgumentCaptor<String> subStringCapture = ArgumentCaptor.forClass(String.class);
119         when(mockBuilder.path(subStringCapture.capture())).thenReturn(mockBuilder);
120         when(mockBuilder.build()).then(invocation -> URI.create(uri + "/" + subStringCapture.getValue()));
121
122         final UriInfo info = mock(UriInfo.class);
123
124         when(info.getRequestUriBuilder()).thenReturn(mockBuilder);
125         when(mockBuilder.replaceQuery(any())).thenReturn(mockBuilder);
126         when(info.getBaseUri()).thenReturn(uri);
127         return info;
128     }
129
130 }