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.
17 package org.onap.ccsdk.cds.sdclistener.handler;
19 import com.google.protobuf.ByteString;
20 import io.grpc.ManagedChannel;
21 import io.grpc.inprocess.InProcessChannelBuilder;
22 import io.grpc.inprocess.InProcessServerBuilder;
23 import io.grpc.stub.StreamObserver;
24 import io.grpc.testing.GrpcCleanupRule;
25 import org.apache.commons.io.FileUtils;
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.onap.ccsdk.cds.controllerblueprints.common.api.ActionIdentifiers;
31 import org.onap.ccsdk.cds.controllerblueprints.common.api.CommonHeader;
32 import org.onap.ccsdk.cds.controllerblueprints.common.api.Status;
33 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementOutput;
34 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementServiceGrpc.BluePrintManagementServiceImplBase;
35 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintUploadInput;
36 import org.onap.ccsdk.cds.controllerblueprints.management.api.FileChunk;
37 import org.onap.ccsdk.cds.controllerblueprints.management.api.UploadAction;
38 import org.onap.ccsdk.cds.sdclistener.client.SdcListenerAuthClientInterceptor;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.boot.context.properties.EnableConfigurationProperties;
41 import org.springframework.boot.test.context.SpringBootTest;
42 import org.springframework.test.context.junit4.SpringRunner;
45 import java.io.IOException;
46 import java.nio.file.Paths;
47 import java.util.UUID;
49 import static org.junit.Assert.assertEquals;
50 import static org.junit.Assert.assertTrue;
52 @RunWith(SpringRunner.class)
53 @EnableConfigurationProperties({BluePrintProcesssorHandler.class, SdcListenerAuthClientInterceptor.class})
54 @SpringBootTest(classes = {BluePrintProcessorHandlerTest.class})
55 public class BluePrintProcessorHandlerTest {
58 private BluePrintProcesssorHandler bluePrintProcesssorHandler;
61 private SdcListenerAuthClientInterceptor sdcListenerAuthClientInterceptor;
64 public GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
66 private static final String CBA_ARCHIVE = "src/test/resources/testcba.zip";
67 private static final String SUCCESS_MSG = "Successfully uploaded CBA";
68 private static final int SUCCESS_CODE = 200;
69 private ManagedChannel channel;
72 public void setUp() throws IOException {
73 final BluePrintManagementServiceImplBase serviceImplBase = new BluePrintManagementServiceImplBase() {
75 public void uploadBlueprint(BluePrintUploadInput request,
76 StreamObserver<BluePrintManagementOutput> responseObserver) {
77 responseObserver.onNext(getBluePrintManagementOutput());
78 responseObserver.onCompleted();
82 // Generate server name.
83 String serverName = InProcessServerBuilder.generateName();
85 // Create a server, add service, start, and register.
86 grpcCleanup.register(InProcessServerBuilder.forName(serverName).addService(serviceImplBase).directExecutor()
89 // Create a client channel.
90 channel = grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build());
94 public void testApplicationEndPointSucess() throws IOException {
96 BluePrintUploadInput request = generateRequest();
99 Status output = bluePrintProcesssorHandler.sendRequest(request, channel);
102 assertEquals(SUCCESS_CODE, output.getCode());
103 assertTrue(output.getMessage().contains(SUCCESS_MSG));
106 private BluePrintUploadInput generateRequest() throws IOException {
107 File file = Paths.get(CBA_ARCHIVE).toFile();
108 byte[] bytes = FileUtils.readFileToByteArray(file);
109 FileChunk fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(bytes)).build();
111 return BluePrintUploadInput.newBuilder()
112 .setCommonHeader(CommonHeader.newBuilder().setRequestId(UUID.randomUUID().toString())
113 .setSubRequestId(UUID.randomUUID().toString()).setOriginatorId("SDC-LISTENER").build())
114 .setActionIdentifiers(
115 ActionIdentifiers.newBuilder().setActionName(UploadAction.PUBLISH.toString()).build())
116 .setFileChunk(fileChunk).build();
119 private BluePrintManagementOutput getBluePrintManagementOutput() {
120 return BluePrintManagementOutput.newBuilder()
121 .setStatus(Status.newBuilder().setMessage(SUCCESS_MSG).setCode(200).build()).build();