9cd7ae780de9462b8958df8cb17bd8b5321ba249
[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(SchemaContext mockContext) {
71         if (mockContext == null) {
72             mockContext = createMockSchemaContext();
73         }
74
75         final SchemaService mockSchemaService = mock(SchemaService.class);
76         when(mockSchemaService.getGlobalContext()).thenReturn(mockContext);
77         return mockSchemaService;
78     }
79
80     public SchemaContext createMockSchemaContext() {
81         final SchemaContext mockContext = mock(SchemaContext.class);
82         when(mockContext.getModules()).thenReturn(this.modules);
83
84         final ArgumentCaptor<String> moduleCapture = ArgumentCaptor.forClass(String.class);
85         final ArgumentCaptor<Optional> dateCapture = ArgumentCaptor.forClass(Optional.class);
86         final ArgumentCaptor<URI> namespaceCapture = ArgumentCaptor.forClass(URI.class);
87         when(mockContext.findModule(moduleCapture.capture(), dateCapture.capture())).then(
88             invocation -> {
89                 final String module = moduleCapture.getValue();
90                 final Optional<?> date = dateCapture.getValue();
91                 for (final Module m : Collections.unmodifiableSet(DocGenTestHelper.this.modules)) {
92                     if (m.getName().equals(module) && m.getRevision().equals(date)) {
93                         return Optional.of(m);
94                     }
95                 }
96                 return Optional.empty();
97             });
98         when(mockContext.findModule(namespaceCapture.capture(), dateCapture.capture())).then(
99             invocation -> {
100                 final URI namespace = namespaceCapture.getValue();
101                 final Optional<?> date = dateCapture.getValue();
102                 for (final Module m : Collections.unmodifiableSet(DocGenTestHelper.this.modules)) {
103                     if (m.getNamespace().equals(namespace) && m.getRevision().equals(date)) {
104                         return Optional.of(m);
105                     }
106                 }
107                 return Optional.empty();
108             });
109         return mockContext;
110     }
111
112     public UriInfo createMockUriInfo(final String urlPrefix) throws URISyntaxException {
113         final URI uri = new URI(urlPrefix);
114
115         final UriBuilder mockBuilder = mock(UriBuilder.class);
116
117         final ArgumentCaptor<String> subStringCapture = ArgumentCaptor.forClass(String.class);
118         when(mockBuilder.path(subStringCapture.capture())).thenReturn(mockBuilder);
119         when(mockBuilder.build()).then(invocation -> URI.create(uri + "/" + subStringCapture.getValue()));
120
121         final UriInfo info = mock(UriInfo.class);
122
123         when(info.getRequestUriBuilder()).thenReturn(mockBuilder);
124         when(info.getBaseUri()).thenReturn(uri);
125         return info;
126     }
127
128 }