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;
15 import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule;
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.Date;
24 import java.util.List;
26 import javax.ws.rs.core.UriBuilder;
27 import javax.ws.rs.core.UriInfo;
28 import org.mockito.ArgumentCaptor;
29 import org.mockito.invocation.InvocationOnMock;
30 import org.mockito.stubbing.Answer;
31 import org.opendaylight.controller.sal.core.api.model.SchemaService;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
35 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
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 URISyntaxException, FileNotFoundException, ReactorException {
46 final URI resourceDirUri = getClass().getResource(resourceDirectory).toURI();
47 final File testDir = new File(resourceDirUri);
48 final String[] fileList = testDir.list();
49 if (fileList == null) {
50 throw new FileNotFoundException(resourceDirectory.toString());
52 final List<File> files = new ArrayList<>();
53 for (final String fileName : fileList) {
54 files.add(new File(testDir, fileName));
57 this.schemaContext = YangParserTestUtils.parseYangSources(files);
58 return this.schemaContext.getModules();
61 public Collection<Module> getModules() {
65 public void setUp() throws Exception {
66 this.modules = loadModules("/yang");
67 this.mapper = new ObjectMapper();
68 this.mapper.registerModule(new JsonOrgModule());
69 this.mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
72 public SchemaContext getSchemaContext() {
73 return this.schemaContext;
76 public SchemaService createMockSchemaService() {
77 return createMockSchemaService(null);
80 public SchemaService createMockSchemaService(SchemaContext mockContext) {
81 if (mockContext == null) {
82 mockContext = createMockSchemaContext();
85 final SchemaService mockSchemaService = mock(SchemaService.class);
86 when(mockSchemaService.getGlobalContext()).thenReturn(mockContext);
87 return mockSchemaService;
90 public SchemaContext createMockSchemaContext() {
91 final SchemaContext mockContext = mock(SchemaContext.class);
92 when(mockContext.getModules()).thenReturn(this.modules);
94 final ArgumentCaptor<String> moduleCapture = ArgumentCaptor.forClass(String.class);
95 final ArgumentCaptor<Date> dateCapture = ArgumentCaptor.forClass(Date.class);
96 final ArgumentCaptor<URI> namespaceCapture = ArgumentCaptor.forClass(URI.class);
97 when(mockContext.findModuleByName(moduleCapture.capture(), dateCapture.capture())).then(
98 new Answer<Module>() {
100 public Module answer(final InvocationOnMock invocation) throws Throwable {
101 final String module = moduleCapture.getValue();
102 final Date date = dateCapture.getValue();
103 for (final Module m : Collections.unmodifiableSet(DocGenTestHelper.this.modules)) {
104 if (m.getName().equals(module) && m.getRevision().equals(date)) {
111 when(mockContext.findModuleByNamespaceAndRevision(namespaceCapture.capture(), dateCapture.capture())).then(
112 new Answer<Module>() {
114 public Module answer(final InvocationOnMock invocation) throws Throwable {
115 final URI namespace = namespaceCapture.getValue();
116 final Date date = dateCapture.getValue();
117 for (final Module m : Collections.unmodifiableSet(DocGenTestHelper.this.modules)) {
118 if (m.getNamespace().equals(namespace) && m.getRevision().equals(date)) {
128 public UriInfo createMockUriInfo(final String urlPrefix) throws URISyntaxException {
129 final URI uri = new URI(urlPrefix);
131 final UriBuilder mockBuilder = mock(UriBuilder.class);
133 final ArgumentCaptor<String> subStringCapture = ArgumentCaptor.forClass(String.class);
134 when(mockBuilder.path(subStringCapture.capture())).thenReturn(mockBuilder);
135 when(mockBuilder.build()).then(new Answer<URI>() {
137 public URI answer(final InvocationOnMock invocation) throws Throwable {
138 return URI.create(uri + "/" + subStringCapture.getValue());
142 final UriInfo info = mock(UriInfo.class);
144 when(info.getRequestUriBuilder()).thenReturn(mockBuilder);
145 when(info.getBaseUri()).thenReturn(uri);