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