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.mock;
12 import static org.mockito.Mockito.when;
14 import com.fasterxml.jackson.databind.ObjectMapper;
15 import com.fasterxml.jackson.databind.SerializationFeature;
17 import java.io.FileNotFoundException;
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;
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;
34 public class DocGenTestHelper {
36 private Set<Module> modules;
37 private ObjectMapper mapper;
38 private SchemaContext schemaContext;
40 public Set<Module> loadModules(final String resourceDirectory) throws URISyntaxException, FileNotFoundException {
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());
48 final List<File> files = new ArrayList<>();
49 for (final String fileName : fileList) {
50 files.add(new File(testDir, fileName));
53 this.schemaContext = YangParserTestUtils.parseYangFiles(files);
54 return this.schemaContext.getModules();
57 public Collection<Module> getModules() {
61 public void setUp() throws Exception {
62 this.modules = loadModules("/yang");
63 this.mapper = new ObjectMapper();
64 this.mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
67 public SchemaContext getSchemaContext() {
68 return this.schemaContext;
71 public DOMSchemaService createMockSchemaService(SchemaContext mockContext) {
72 if (mockContext == null) {
73 mockContext = createMockSchemaContext();
76 final DOMSchemaService mockSchemaService = mock(DOMSchemaService.class);
77 when(mockSchemaService.getGlobalContext()).thenReturn(mockContext);
78 return mockSchemaService;
81 public SchemaContext createMockSchemaContext() {
82 final SchemaContext mockContext = mock(SchemaContext.class);
83 when(mockContext.getModules()).thenReturn(this.modules);
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(
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);
97 return Optional.empty();
99 when(mockContext.findModule(namespaceCapture.capture(), dateCapture.capture())).then(
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);
108 return Optional.empty();
113 public UriInfo createMockUriInfo(final String urlPrefix) throws URISyntaxException {
114 final URI uri = new URI(urlPrefix);
116 final UriBuilder mockBuilder = mock(UriBuilder.class);
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()));
122 final UriInfo info = mock(UriInfo.class);
124 when(info.getRequestUriBuilder()).thenReturn(mockBuilder);
125 when(mockBuilder.replaceQuery(any())).thenReturn(mockBuilder);
126 when(info.getBaseUri()).thenReturn(uri);