/* * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.controller.sal.rest.doc.impl; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule; import java.io.File; import java.io.FileNotFoundException; import java.net.URI; import java.net.URISyntaxException; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.Set; import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriInfo; import org.mockito.ArgumentCaptor; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import org.opendaylight.controller.sal.core.api.model.SchemaService; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl; import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream; public class DocGenTestHelper { private Set modules; private ObjectMapper mapper; private SchemaContext schemaContext; public Set loadModules(final String resourceDirectory) throws FileNotFoundException, URISyntaxException, ReactorException { final URI resourceDirUri = getClass().getResource(resourceDirectory).toURI(); final File testDir = new File(resourceDirUri); final String[] fileList = testDir.list(); if (fileList == null) { throw new FileNotFoundException(resourceDirectory.toString()); } final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(); for (final String fileName : fileList) { final File file = new File(testDir, fileName); reactor.addSource(new YangStatementSourceImpl(new NamedFileInputStream(file, file.getPath()))); } this.schemaContext = reactor.buildEffective(); return this.schemaContext.getModules(); } public Collection getModules() { return this.modules; } public void setUp() throws Exception { this.modules = loadModules("/yang"); this.mapper = new ObjectMapper(); this.mapper.registerModule(new JsonOrgModule()); this.mapper.configure(SerializationFeature.INDENT_OUTPUT, true); } public SchemaContext getSchemaContext() { return this.schemaContext; } public SchemaService createMockSchemaService() { return createMockSchemaService(null); } public SchemaService createMockSchemaService(SchemaContext mockContext) { if (mockContext == null) { mockContext = createMockSchemaContext(); } final SchemaService mockSchemaService = mock(SchemaService.class); when(mockSchemaService.getGlobalContext()).thenReturn(mockContext); return mockSchemaService; } public SchemaContext createMockSchemaContext() { final SchemaContext mockContext = mock(SchemaContext.class); when(mockContext.getModules()).thenReturn(this.modules); final ArgumentCaptor moduleCapture = ArgumentCaptor.forClass(String.class); final ArgumentCaptor dateCapture = ArgumentCaptor.forClass(Date.class); final ArgumentCaptor namespaceCapture = ArgumentCaptor.forClass(URI.class); when(mockContext.findModuleByName(moduleCapture.capture(), dateCapture.capture())).then( new Answer() { @Override public Module answer(final InvocationOnMock invocation) throws Throwable { final String module = moduleCapture.getValue(); final Date date = dateCapture.getValue(); for (final Module m : Collections.unmodifiableSet(DocGenTestHelper.this.modules)) { if (m.getName().equals(module) && m.getRevision().equals(date)) { return m; } } return null; } }); when(mockContext.findModuleByNamespaceAndRevision(namespaceCapture.capture(), dateCapture.capture())).then( new Answer() { @Override public Module answer(final InvocationOnMock invocation) throws Throwable { final URI namespace = namespaceCapture.getValue(); final Date date = dateCapture.getValue(); for (final Module m : Collections.unmodifiableSet(DocGenTestHelper.this.modules)) { if (m.getNamespace().equals(namespace) && m.getRevision().equals(date)) { return m; } } return null; } }); return mockContext; } public UriInfo createMockUriInfo(final String urlPrefix) throws URISyntaxException { final URI uri = new URI(urlPrefix); final UriBuilder mockBuilder = mock(UriBuilder.class); final ArgumentCaptor subStringCapture = ArgumentCaptor.forClass(String.class); when(mockBuilder.path(subStringCapture.capture())).thenReturn(mockBuilder); when(mockBuilder.build()).then(new Answer() { @Override public URI answer(final InvocationOnMock invocation) throws Throwable { return URI.create(uri + "/" + subStringCapture.getValue()); } }); final UriInfo info = mock(UriInfo.class); when(info.getRequestUriBuilder()).thenReturn(mockBuilder); when(info.getBaseUri()).thenReturn(uri); return info; } }