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;
12 import com.fasterxml.jackson.databind.ObjectMapper;
13 import com.fasterxml.jackson.databind.SerializationFeature;
14 import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule;
16 import java.io.FileNotFoundException;
18 import java.net.URISyntaxException;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.Date;
23 import javax.ws.rs.core.UriBuilder;
24 import javax.ws.rs.core.UriInfo;
25 import org.mockito.ArgumentCaptor;
26 import org.mockito.invocation.InvocationOnMock;
27 import org.mockito.stubbing.Answer;
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.parser.spi.meta.ReactorException;
32 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
33 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
34 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
35 import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
37 public class DocGenTestHelper {
39 private Set<Module> modules;
40 private ObjectMapper mapper;
41 private SchemaContext schemaContext;
43 public Set<Module> loadModules(final String resourceDirectory)
44 throws FileNotFoundException,
45 URISyntaxException, ReactorException {
47 final URI resourceDirUri = getClass().getResource(resourceDirectory).toURI();
48 final File testDir = new File(resourceDirUri);
49 final String[] fileList = testDir.list();
50 if (fileList == null) {
51 throw new FileNotFoundException(resourceDirectory.toString());
53 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
54 for (final String fileName : fileList) {
55 final File file = new File(testDir, fileName);
56 reactor.addSource(new YangStatementSourceImpl(new NamedFileInputStream(file, file.getPath())));
59 this.schemaContext = reactor.buildEffective();
60 return this.schemaContext.getModules();
63 public Collection<Module> getModules() {
67 public void setUp() throws Exception {
68 this.modules = loadModules("/yang");
69 this.mapper = new ObjectMapper();
70 this.mapper.registerModule(new JsonOrgModule());
71 this.mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
74 public SchemaContext getSchemaContext() {
75 return this.schemaContext;
78 public SchemaService createMockSchemaService() {
79 return createMockSchemaService(null);
82 public SchemaService createMockSchemaService(SchemaContext mockContext) {
83 if (mockContext == null) {
84 mockContext = createMockSchemaContext();
87 final SchemaService mockSchemaService = mock(SchemaService.class);
88 when(mockSchemaService.getGlobalContext()).thenReturn(mockContext);
89 return mockSchemaService;
92 public SchemaContext createMockSchemaContext() {
93 final SchemaContext mockContext = mock(SchemaContext.class);
94 when(mockContext.getModules()).thenReturn(this.modules);
96 final ArgumentCaptor<String> moduleCapture = ArgumentCaptor.forClass(String.class);
97 final ArgumentCaptor<Date> dateCapture = ArgumentCaptor.forClass(Date.class);
98 final ArgumentCaptor<URI> namespaceCapture = ArgumentCaptor.forClass(URI.class);
99 when(mockContext.findModuleByName(moduleCapture.capture(), dateCapture.capture())).then(
100 new Answer<Module>() {
102 public Module answer(final InvocationOnMock invocation) throws Throwable {
103 final String module = moduleCapture.getValue();
104 final Date date = dateCapture.getValue();
105 for (final Module m : Collections.unmodifiableSet(DocGenTestHelper.this.modules)) {
106 if (m.getName().equals(module) && m.getRevision().equals(date)) {
113 when(mockContext.findModuleByNamespaceAndRevision(namespaceCapture.capture(), dateCapture.capture())).then(
114 new Answer<Module>() {
116 public Module answer(final InvocationOnMock invocation) throws Throwable {
117 final URI namespace = namespaceCapture.getValue();
118 final Date date = dateCapture.getValue();
119 for (final Module m : Collections.unmodifiableSet(DocGenTestHelper.this.modules)) {
120 if (m.getNamespace().equals(namespace) && m.getRevision().equals(date)) {
130 public UriInfo createMockUriInfo(final String urlPrefix) throws URISyntaxException {
131 final URI uri = new URI(urlPrefix);
133 final UriBuilder mockBuilder = mock(UriBuilder.class);
135 final ArgumentCaptor<String> subStringCapture = ArgumentCaptor.forClass(String.class);
136 when(mockBuilder.path(subStringCapture.capture())).thenReturn(mockBuilder);
137 when(mockBuilder.build()).then(new Answer<URI>() {
139 public URI answer(final InvocationOnMock invocation) throws Throwable {
140 return URI.create(uri + "/" + subStringCapture.getValue());
144 final UriInfo info = mock(UriInfo.class);
146 when(info.getRequestUriBuilder()).thenReturn(mockBuilder);
147 when(info.getBaseUri()).thenReturn(uri);