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.Date;
23 import java.util.List;
25 import javax.ws.rs.core.UriBuilder;
26 import javax.ws.rs.core.UriInfo;
27 import org.mockito.ArgumentCaptor;
28 import org.mockito.invocation.InvocationOnMock;
29 import org.mockito.stubbing.Answer;
30 import org.opendaylight.controller.sal.core.api.model.SchemaService;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
34 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
36 public class DocGenTestHelper {
38 private Set<Module> modules;
39 private ObjectMapper mapper;
40 private SchemaContext schemaContext;
42 public Set<Module> loadModules(final String resourceDirectory)
43 throws URISyntaxException, FileNotFoundException, ReactorException {
45 final URI resourceDirUri = getClass().getResource(resourceDirectory).toURI();
46 final File testDir = new File(resourceDirUri);
47 final String[] fileList = testDir.list();
48 if (fileList == null) {
49 throw new FileNotFoundException(resourceDirectory.toString());
51 final List<File> files = new ArrayList<>();
52 for (final String fileName : fileList) {
53 files.add(new File(testDir, fileName));
56 this.schemaContext = YangParserTestUtils.parseYangSources(files);
57 return this.schemaContext.getModules();
60 public Collection<Module> getModules() {
64 public void setUp() throws Exception {
65 this.modules = loadModules("/yang");
66 this.mapper = new ObjectMapper();
67 this.mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
70 public SchemaContext getSchemaContext() {
71 return this.schemaContext;
74 public SchemaService createMockSchemaService() {
75 return createMockSchemaService(null);
78 public SchemaService createMockSchemaService(SchemaContext mockContext) {
79 if (mockContext == null) {
80 mockContext = createMockSchemaContext();
83 final SchemaService mockSchemaService = mock(SchemaService.class);
84 when(mockSchemaService.getGlobalContext()).thenReturn(mockContext);
85 return mockSchemaService;
88 public SchemaContext createMockSchemaContext() {
89 final SchemaContext mockContext = mock(SchemaContext.class);
90 when(mockContext.getModules()).thenReturn(this.modules);
92 final ArgumentCaptor<String> moduleCapture = ArgumentCaptor.forClass(String.class);
93 final ArgumentCaptor<Date> dateCapture = ArgumentCaptor.forClass(Date.class);
94 final ArgumentCaptor<URI> namespaceCapture = ArgumentCaptor.forClass(URI.class);
95 when(mockContext.findModuleByName(moduleCapture.capture(), dateCapture.capture())).then(
96 new Answer<Module>() {
98 public Module answer(final InvocationOnMock invocation) throws Throwable {
99 final String module = moduleCapture.getValue();
100 final Date date = dateCapture.getValue();
101 for (final Module m : Collections.unmodifiableSet(DocGenTestHelper.this.modules)) {
102 if (m.getName().equals(module) && m.getRevision().equals(date)) {
109 when(mockContext.findModuleByNamespaceAndRevision(namespaceCapture.capture(), dateCapture.capture())).then(
110 new Answer<Module>() {
112 public Module answer(final InvocationOnMock invocation) throws Throwable {
113 final URI namespace = namespaceCapture.getValue();
114 final Date date = dateCapture.getValue();
115 for (final Module m : Collections.unmodifiableSet(DocGenTestHelper.this.modules)) {
116 if (m.getNamespace().equals(namespace) && m.getRevision().equals(date)) {
126 public UriInfo createMockUriInfo(final String urlPrefix) throws URISyntaxException {
127 final URI uri = new URI(urlPrefix);
129 final UriBuilder mockBuilder = mock(UriBuilder.class);
131 final ArgumentCaptor<String> subStringCapture = ArgumentCaptor.forClass(String.class);
132 when(mockBuilder.path(subStringCapture.capture())).thenReturn(mockBuilder);
133 when(mockBuilder.build()).then(new Answer<URI>() {
135 public URI answer(final InvocationOnMock invocation) throws Throwable {
136 return URI.create(uri + "/" + subStringCapture.getValue());
140 final UriInfo info = mock(UriInfo.class);
142 when(info.getRequestUriBuilder()).thenReturn(mockBuilder);
143 when(info.getBaseUri()).thenReturn(uri);