2 * Copyright © 2019 Bell Canada
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.ccsdk.cds.sdclistener.handler;
18 import com.google.protobuf.ByteString;
19 import io.grpc.ManagedChannel;
20 import io.grpc.inprocess.InProcessChannelBuilder;
21 import io.grpc.inprocess.InProcessServerBuilder;
22 import io.grpc.stub.StreamObserver;
23 import io.grpc.testing.GrpcCleanupRule;
24 import org.apache.commons.io.FileUtils;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.onap.ccsdk.cds.controllerblueprints.common.api.ActionIdentifiers;
30 import org.onap.ccsdk.cds.controllerblueprints.common.api.CommonHeader;
31 import org.onap.ccsdk.cds.controllerblueprints.common.api.Status;
32 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementOutput;
33 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementServiceGrpc.BluePrintManagementServiceImplBase;
34 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintUploadInput;
35 import org.onap.ccsdk.cds.controllerblueprints.management.api.FileChunk;
36 import org.onap.ccsdk.cds.controllerblueprints.management.api.UploadAction;
37 import org.onap.ccsdk.cds.sdclistener.client.SdcListenerAuthClientInterceptor;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.boot.context.properties.EnableConfigurationProperties;
40 import org.springframework.boot.test.context.SpringBootTest;
41 import org.springframework.test.context.junit4.SpringRunner;
44 import java.io.IOException;
45 import java.nio.file.Paths;
46 import java.util.UUID;
48 import static org.junit.Assert.assertEquals;
49 import static org.junit.Assert.assertTrue;
51 @RunWith(SpringRunner.class)
52 @EnableConfigurationProperties({BluePrintProcesssorHandler.class, SdcListenerAuthClientInterceptor.class})
53 @SpringBootTest(classes = {BluePrintProcessorHandlerTest.class})
54 public class BluePrintProcessorHandlerTest {
57 private BluePrintProcesssorHandler bluePrintProcesssorHandler;
60 private SdcListenerAuthClientInterceptor sdcListenerAuthClientInterceptor;
63 public GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
65 private static final String CBA_ARCHIVE = "src/test/resources/testcba.zip";
66 private static final String SUCCESS_MSG = "Successfully uploaded CBA";
67 private static final int SUCCESS_CODE = 200;
68 private ManagedChannel channel;
71 public void setUp() throws IOException {
72 final BluePrintManagementServiceImplBase serviceImplBase = new BluePrintManagementServiceImplBase() {
74 public void uploadBlueprint(BluePrintUploadInput request,
75 StreamObserver<BluePrintManagementOutput> responseObserver) {
76 responseObserver.onNext(getBluePrintManagementOutput());
77 responseObserver.onCompleted();
81 // Generate server name.
82 String serverName = InProcessServerBuilder.generateName();
84 // Create a server, add service, start, and register.
86 InProcessServerBuilder.forName(serverName).addService(serviceImplBase).directExecutor().build().start());
88 // Create a client channel.
89 channel = grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build());
93 public void testApplicationEndPointSucess() throws IOException {
95 BluePrintUploadInput request = generateRequest();
98 Status output = bluePrintProcesssorHandler.sendRequest(request, channel);
101 assertEquals(SUCCESS_CODE, output.getCode());
102 assertTrue(output.getMessage().contains(SUCCESS_MSG));
105 private BluePrintUploadInput generateRequest() throws IOException {
106 File file = Paths.get(CBA_ARCHIVE).toFile();
107 byte[] bytes = FileUtils.readFileToByteArray(file);
108 FileChunk fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(bytes)).build();
111 return BluePrintUploadInput.newBuilder()
112 .setCommonHeader(CommonHeader.newBuilder()
113 .setRequestId(UUID.randomUUID().toString())
114 .setSubRequestId(UUID.randomUUID().toString())
115 .setOriginatorId("SDC-LISTENER")
117 .setActionIdentifiers(ActionIdentifiers.newBuilder()
118 .setActionName(UploadAction.PUBLISH.toString()).build())
119 .setFileChunk(fileChunk).build();
122 private BluePrintManagementOutput getBluePrintManagementOutput() {
123 return BluePrintManagementOutput.newBuilder()
124 .setStatus(Status.newBuilder().setMessage(SUCCESS_MSG).setCode(200).build())