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.Mockito.mock;
11 import static org.mockito.Mockito.when;
13 import com.fasterxml.jackson.databind.ObjectMapper;
14 import com.fasterxml.jackson.databind.SerializationFeature;
16 import java.io.FileNotFoundException;
18 import java.net.URISyntaxException;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.List;
23 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.controller.sal.core.api.model.SchemaService;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
33 public class DocGenTestHelper {
35 private Set<Module> modules;
36 private ObjectMapper mapper;
37 private SchemaContext schemaContext;
39 public Set<Module> loadModules(final String resourceDirectory) throws URISyntaxException, FileNotFoundException {
41 final URI resourceDirUri = getClass().getResource(resourceDirectory).toURI();
42 final File testDir = new File(resourceDirUri);
43 final String[] fileList = testDir.list();
44 if (fileList == null) {
45 throw new FileNotFoundException(resourceDirectory.toString());
47 final List<File> files = new ArrayList<>();
48 for (final String fileName : fileList) {
49 files.add(new File(testDir, fileName));
52 this.schemaContext = YangParserTestUtils.parseYangFiles(files);
53 return this.schemaContext.getModules();
56 public Collection<Module> getModules() {
60 public void setUp() throws Exception {
61 this.modules = loadModules("/yang");
62 this.mapper = new ObjectMapper();
63 this.mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
66 public SchemaContext getSchemaContext() {
67 return this.schemaContext;
70 public SchemaService createMockSchemaService() {
71 return createMockSchemaService(null);
74 public SchemaService createMockSchemaService(SchemaContext mockContext) {
75 if (mockContext == null) {
76 mockContext = createMockSchemaContext();
79 final SchemaService mockSchemaService = mock(SchemaService.class);
80 when(mockSchemaService.getGlobalContext()).thenReturn(mockContext);
81 return mockSchemaService;
84 public SchemaContext createMockSchemaContext() {
85 final SchemaContext mockContext = mock(SchemaContext.class);
86 when(mockContext.getModules()).thenReturn(this.modules);
88 final ArgumentCaptor<String> moduleCapture = ArgumentCaptor.forClass(String.class);
89 final ArgumentCaptor<Optional> dateCapture = ArgumentCaptor.forClass(Optional.class);
90 final ArgumentCaptor<URI> namespaceCapture = ArgumentCaptor.forClass(URI.class);
91 when(mockContext.findModule(moduleCapture.capture(), dateCapture.capture())).then(
93 final String module = moduleCapture.getValue();
94 final Optional<?> date = dateCapture.getValue();
95 for (final Module m : Collections.unmodifiableSet(DocGenTestHelper.this.modules)) {
96 if (m.getName().equals(module) && m.getRevision().equals(date)) {
97 return Optional.of(m);
100 return Optional.empty();
102 when(mockContext.findModule(namespaceCapture.capture(), dateCapture.capture())).then(
104 final URI namespace = namespaceCapture.getValue();
105 final Optional<?> date = dateCapture.getValue();
106 for (final Module m : Collections.unmodifiableSet(DocGenTestHelper.this.modules)) {
107 if (m.getNamespace().equals(namespace) && m.getRevision().equals(date)) {
108 return Optional.of(m);
111 return Optional.empty();
116 public UriInfo createMockUriInfo(final String urlPrefix) throws URISyntaxException {
117 final URI uri = new URI(urlPrefix);
119 final UriBuilder mockBuilder = mock(UriBuilder.class);
121 final ArgumentCaptor<String> subStringCapture = ArgumentCaptor.forClass(String.class);
122 when(mockBuilder.path(subStringCapture.capture())).thenReturn(mockBuilder);
123 when(mockBuilder.build()).then(invocation -> URI.create(uri + "/" + subStringCapture.getValue()));
125 final UriInfo info = mock(UriInfo.class);
127 when(info.getRequestUriBuilder()).thenReturn(mockBuilder);
128 when(info.getBaseUri()).thenReturn(uri);