Adjust to yangtools-2.0.0/odlparent-3.0.0 changes
[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 java.io.File;
16 import java.io.FileNotFoundException;
17 import java.net.URI;
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;
24 import java.util.Set;
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;
32
33 public class DocGenTestHelper {
34
35     private Set<Module> modules;
36     private ObjectMapper mapper;
37     private SchemaContext schemaContext;
38
39     public Set<Module> loadModules(final String resourceDirectory) throws URISyntaxException, FileNotFoundException {
40
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());
46         }
47         final List<File> files = new ArrayList<>();
48         for (final String fileName : fileList) {
49             files.add(new File(testDir, fileName));
50         }
51
52         this.schemaContext = YangParserTestUtils.parseYangFiles(files);
53         return this.schemaContext.getModules();
54     }
55
56     public Collection<Module> getModules() {
57         return this.modules;
58     }
59
60     public void setUp() throws Exception {
61         this.modules = loadModules("/yang");
62         this.mapper = new ObjectMapper();
63         this.mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
64     }
65
66     public SchemaContext getSchemaContext() {
67         return this.schemaContext;
68     }
69
70     public SchemaService createMockSchemaService() {
71         return createMockSchemaService(null);
72     }
73
74     public SchemaService createMockSchemaService(SchemaContext mockContext) {
75         if (mockContext == null) {
76             mockContext = createMockSchemaContext();
77         }
78
79         final SchemaService mockSchemaService = mock(SchemaService.class);
80         when(mockSchemaService.getGlobalContext()).thenReturn(mockContext);
81         return mockSchemaService;
82     }
83
84     public SchemaContext createMockSchemaContext() {
85         final SchemaContext mockContext = mock(SchemaContext.class);
86         when(mockContext.getModules()).thenReturn(this.modules);
87
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(
92             invocation -> {
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);
98                     }
99                 }
100                 return Optional.empty();
101             });
102         when(mockContext.findModule(namespaceCapture.capture(), dateCapture.capture())).then(
103             invocation -> {
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);
109                     }
110                 }
111                 return Optional.empty();
112             });
113         return mockContext;
114     }
115
116     public UriInfo createMockUriInfo(final String urlPrefix) throws URISyntaxException {
117         final URI uri = new URI(urlPrefix);
118
119         final UriBuilder mockBuilder = mock(UriBuilder.class);
120
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()));
124
125         final UriInfo info = mock(UriInfo.class);
126
127         when(info.getRequestUriBuilder()).thenReturn(mockBuilder);
128         when(info.getBaseUri()).thenReturn(uri);
129         return info;
130     }
131
132 }