Merge "Use yangtools ParserStreamUtils.findSchemaNodeByNameAndNamespace() method"
[netconf.git] / restconf / sal-rest-docgen / src / test / java / org / opendaylight / controller / sal / rest / doc / impl / DocGenTestHelper.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.rest.doc.impl;
9
10 import static org.mockito.Mockito.mock;
11 import static org.mockito.Mockito.when;
12
13 import com.fasterxml.jackson.databind.ObjectMapper;
14 import com.fasterxml.jackson.databind.SerializationFeature;
15 import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule;
16 import java.io.File;
17 import java.io.FileNotFoundException;
18 import java.net.URI;
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;
25 import java.util.Set;
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;
36
37 public class DocGenTestHelper {
38
39     private Set<Module> modules;
40     private ObjectMapper mapper;
41     private SchemaContext schemaContext;
42
43     public Set<Module> loadModules(final String resourceDirectory)
44             throws URISyntaxException, FileNotFoundException, ReactorException {
45
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());
51         }
52         final List<File> files = new ArrayList<>();
53         for (final String fileName : fileList) {
54             files.add(new File(testDir, fileName));
55         }
56
57         this.schemaContext = YangParserTestUtils.parseYangSources(files);
58         return this.schemaContext.getModules();
59     }
60
61     public Collection<Module> getModules() {
62         return this.modules;
63     }
64
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);
70     }
71
72     public SchemaContext getSchemaContext() {
73         return this.schemaContext;
74     }
75
76     public SchemaService createMockSchemaService() {
77         return createMockSchemaService(null);
78     }
79
80     public SchemaService createMockSchemaService(SchemaContext mockContext) {
81         if (mockContext == null) {
82             mockContext = createMockSchemaContext();
83         }
84
85         final SchemaService mockSchemaService = mock(SchemaService.class);
86         when(mockSchemaService.getGlobalContext()).thenReturn(mockContext);
87         return mockSchemaService;
88     }
89
90     public SchemaContext createMockSchemaContext() {
91         final SchemaContext mockContext = mock(SchemaContext.class);
92         when(mockContext.getModules()).thenReturn(this.modules);
93
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>() {
99                     @Override
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)) {
105                                 return m;
106                             }
107                         }
108                         return null;
109                     }
110                 });
111         when(mockContext.findModuleByNamespaceAndRevision(namespaceCapture.capture(), dateCapture.capture())).then(
112                 new Answer<Module>() {
113                     @Override
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)) {
119                                 return m;
120                             }
121                         }
122                         return null;
123                     }
124                 });
125         return mockContext;
126     }
127
128     public UriInfo createMockUriInfo(final String urlPrefix) throws URISyntaxException {
129         final URI uri = new URI(urlPrefix);
130
131         final UriBuilder mockBuilder = mock(UriBuilder.class);
132
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>() {
136             @Override
137             public URI answer(final InvocationOnMock invocation) throws Throwable {
138                 return URI.create(uri + "/" + subStringCapture.getValue());
139             }
140         });
141
142         final UriInfo info = mock(UriInfo.class);
143
144         when(info.getRequestUriBuilder()).thenReturn(mockBuilder);
145         when(info.getBaseUri()).thenReturn(uri);
146         return info;
147     }
148
149 }