2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.rest.doc.impl;
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;
15 import com.fasterxml.jackson.databind.ObjectMapper;
16 import com.fasterxml.jackson.databind.SerializationFeature;
18 import java.io.FileNotFoundException;
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;
34 public class DocGenTestHelper {
36 private Collection<? extends Module> modules;
37 private ObjectMapper mapper;
38 private EffectiveModelContext schemaContext;
40 public Collection<? extends Module> loadModules(final String resourceDirectory)
41 throws URISyntaxException, FileNotFoundException {
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());
49 final List<File> files = new ArrayList<>();
50 for (final String fileName : fileList) {
51 files.add(new File(testDir, fileName));
54 this.schemaContext = YangParserTestUtils.parseYangFiles(files);
55 return this.schemaContext.getModules();
58 public Collection<? extends Module> getModules() {
62 public void setUp() throws Exception {
63 this.modules = loadModules("/yang");
64 this.mapper = new ObjectMapper();
65 this.mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
68 public EffectiveModelContext getSchemaContext() {
69 return this.schemaContext;
72 public DOMSchemaService createMockSchemaService(EffectiveModelContext mockContext) {
73 if (mockContext == null) {
74 mockContext = createMockSchemaContext();
77 final DOMSchemaService mockSchemaService = mock(DOMSchemaService.class);
78 when(mockSchemaService.getGlobalContext()).thenReturn(mockContext);
79 return mockSchemaService;
82 public EffectiveModelContext createMockSchemaContext() {
83 final EffectiveModelContext mockContext = mock(EffectiveModelContext.class);
84 doReturn(this.modules).when(mockContext).getModules();
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(
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);
98 return Optional.empty();
100 when(mockContext.findModule(namespaceCapture.capture(), dateCapture.capture())).then(
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);
109 return Optional.empty();
114 public UriInfo createMockUriInfo(final String urlPrefix) throws URISyntaxException {
115 final URI uri = new URI(urlPrefix);
117 final UriBuilder mockBuilder = mock(UriBuilder.class);
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()));
123 final UriInfo info = mock(UriInfo.class);
125 when(info.getRequestUriBuilder()).thenReturn(mockBuilder);
126 when(mockBuilder.replaceQuery(any())).thenReturn(mockBuilder);
127 when(info.getBaseUri()).thenReturn(uri);